#!/bin/bash

ACTION="$1"

shift

PACKAGE_LIST=$@



case $ACTION in

	preInstall)

		#START openshot-qt
			if [[ " ${PACKAGE_LIST[*]} " == *" openshot-qt "* ]]; then
				echo " * * Start Pre-Installing python3-openshot * * "
				apt update
				apt install python3-openshot -y
				echo " * * Pre-Installed python3-openshot * * "
			fi

			if [[ " ${PACKAGE_LIST[*]} " == *" selene "* ]]; then
				echo " * * Downloading Selene dependences... * * "
				apt update
				apt install libgee-0.8-2 fdkaac lame libaudio2 libdirectfb-1.7-7 libenca0 libfdk-aac1 libffms2-4 libgpac4 libmediainfo0v5 libvorbisidec1 libzen0v5 mediainfo mkvtoolnix mplayer vorbis-tools x264 -y
				echo " * * Pre-Installed selene dependences * * "
			fi
		#END openshot-qt
		
	;;

	postInstall)
	
		#START libdvd-package
			if [[ " ${PACKAGE_LIST[*]} " == *" libdvd-pkg "* ]]; then
				echo " * * Start Post-Installing libdvd-pkg * * "
				dpkg-reconfigure -f noninteractive libdvd-pkg
				echo " * * Finished Post-Installing libdvd-pkg * * "
			fi
		#END libdvd-package
			
	;;	

	remove)

		for ix in $PACKAGE_LIST	

		do
			if [[ "$ix" == "org.shotcut.Shotcut" ]]; then
				flatpak uninstall $ix -y
				TEST=$(flatpak list | grep shotcut | cut -d " " -f 1)

				if [[ ! -z "$TEST" ]];then
					exit 1
				fi
			fi
			if [[ "$ix" == "org.audacityteam.Audacity" ]]; then
				flatpak uninstall $ix -y
				TEST=$(flatpak list | grep audacity | cut -d " " -f 1)

				if [[ ! -z "$TEST" ]];then
					exit 1
				fi
			fi
			if [[ "$ix" == "com.github.vkohaupt.vokoscreenNG" ]]; then
				flatpak uninstall $ix -y
				TEST=$(flatpak list | grep vokoscreen | cut -d " " -f 1)

				if [[ ! -z "$TEST" ]];then
					exit 1
				fi
			else
				apt-get remove -y $ix

				TEST=$( dpkg-query -s  $ix 2> /dev/null| grep Status | cut -d " " -f 4 )

				if [ "$TEST" == 'installed' ];then

					exit 1

				fi
			fi
		done		

	;;	

esac

exit 0
