#!/bin/bash

function getFlavour
{

	skipInstall=1
	lliurexVersion=$(lliurex-version)
	IFS=","
	read -a flavours <<< "$lliurexVersion"

	for item in "${flavours[0]}";
	do
		if [[ $item == *"server"* ]];then
			skipInstall=0
			break
		fi
	done

	echo $skipInstall

}

function installVBoxUsers {

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

	if [ "$TEST" == 'installed' ];then
		apt-get install -y cdc-mapper-vboxusers || true
	fi

}

function removeVBoxUsers {

	match=0
	if dpkg -s cdc-mapper-vboxusers &>/dev/null ; then
		apt-get remove -y cdc-mapper-vboxusers || true
		TEST=$( dpkg-query -s cdc-mapper-vboxusers 2> /dev/null| grep Status | cut -d " " -f 4 )

		if [ "$TEST" != 'installed' ];then
			match+=1
		fi
	else
		match+=1
	fi

	if [ "$match" -gt 0 ];then
		if [ -f /usr/share/cdc-mapper/groups/vboxusers.json ];then
			rm -f /usr/share/cdc-mapper/groups/vboxusers.json | true
			systemctl restart cdc-mapper.service || true
		fi
	fi

}

function getMsg {

	if [[ $LANG == *"valencia"* ]];then
		msgBox="Pot descarregar VirtualBox Extension Pack accedint a\nhttps://www.virtualbox.org/wiki/Downloads"
	elif [[ $LANG == *"es"* ]];then
		msgBox="Puede descargar VirtualBox Extension Pack accediendo a\nhttps://www.virtualbox.org/wiki/Downloads"
	elif [[ $LANG == *"en"* ]];then
		msgBox="You can download VirtualBox Extension Pack from\nhttps://www.virtualbox.org/wiki/Downloads"
	fi

	echo "$msgBox"		

}

ACTION="$1"
QEMU_ADITIONAL_PKGS="qemu-system-x86 virt-manager libvirt-clients libvirt-daemon-system bridge-utils"
VIRTUAL_ADITIONAL_PKGS="libqt6dbus6t64 libqt6help6 libqt6printsupport6t64 libqt6statemachine6 libqt6widgets6t64 libqt6xml6t64 qt6-scxml liblzf1 libtpms0 libsdl-ttf2.0-0"

shift
PACKAGE_LIST=$@

case $ACTION in

	preInstall)
		
		skipInstall=$(getFlavour)
		for ix in $PACKAGE_LIST
		do
			if [[ " $ix " == *" qemu "* ]];then
				if [ "$skipInstall" -eq 0 ];then
					echo "***Unable to install QEMU in server. Remove it for the selection and try again***"
					exit 1
				fi
			elif [[ "$ix" == "virtualbox-7.2" ]];then
				for jx in $VIRTUAL_ADITIONAL_PKGS
				do
					apt-get install -y $jx
					TEST=$( dpkg-query -s $jx 2> /dev/null| grep Status | cut -d " " -f 4 )
					if [ "$TEST" != 'installed' ];then
						exit 1
					fi
					
				done
			fi
		done
	;;

	postInstall)
		for ix in $PACKAGE_LIST
		do
			if [[ "$ix" == "virtualbox-7.2" ]];then
				installVBoxUsers "virtualbox-7.2"
				apt-get install -y llx-vbox-config || true
				msgBox=$(getMsg)
				echo ""
				echo "---------------------------------------------------"
				echo "You can download VirtualBox Extension Pack from https://www.virtualbox.org/wiki/Downloads"
				echo "---------------------------------------------------"
				echo ""
				kdialog --title "EPI" --msgbox "$msgBox" || true

			elif [[ " $ix " == *" qemu " ]];then
				for jx in $QEMU_ADITIONAL_PKGS
				do
					apt-get install -y $jx
					TEST=$( dpkg-query -s $jx 2> /dev/null| grep Status | cut -d " " -f 4 )
					if [ "$TEST" != 'installed' ];then
						exit 1
					fi
				done
				echo "****A restart is needed****"
			fi
		done
	;;

	remove)
		count=0
		for ix in $PACKAGE_LIST
		do
			
			if [[ "$ix" == "virtualbox-7.2" ]];then 
				apt-get purge -y $ix
				TEST=$( dpkg-query -s $ix 2> /dev/null| grep Status | cut -d " " -f 4 )
				if [ "$TEST" == 'installed' ];then
					count+=1
				else
					removeVBoxUsers
					apt-get purge -y llx-vbox-config || true
				fi
			elif [[ " $ix " == *" qemu "* ]];then 
				apt-get purge -y $ix
				TEST=$( dpkg-query -s $ix 2> /dev/null| grep Status | cut -d " " -f 4 )
				if [ "$TEST" == 'installed' ];then
					count+=1
				else
					for jx in $QEMU_ADITIONAL_PKGS
					do
						apt-get remove -y $jx
					done
				fi
			fi
		done

		if [ "$count" -gt 0 ];then
			exit 1
		fi
	;;
esac
exit 0	

