#!/bin/bash

ACTION="$1"

shift

PACKAGE_LIST=$@

# START Define variables
DESTDOWNLOAD="/var/cache/epi-downloads"
KEME_DEB="keme-amd64.deb"
KEME_URL="https://sourceforge.net/projects/keme/files/KEME-Contabilidad/4.0.0.0/keme_4.0.0.0_Ubuntu22.04_64bits.deb/download"
KEME_MD5="9ffdb05b71561a7a9643f334a2827f09"
# END Define Variables



case $ACTION in

	getStatus)
			
		#START KEME
			if [[ " ${PACKAGE_LIST[*]} " == *" keme "* ]]; then
				if dpkg -s keme-contabilidad-6 >/dev/null 2>&1 ;then
					echo 0
				else
					echo 1
				fi
			elif [[ " ${PACKAGE_LIST[*]} " == *" odoo "* ]]; then
				if [ -n "$(which docker)" ]; then
					if [ -n "$(docker images -q postgres:10)" -a -n "$(docker images -q odoo:latest)" -a -f "/usr/bin/odoorun" -a -f "/usr/share/polkit-1/actions/net.lliurex.odoorun.policy"  ]; then
						echo 0
					else
						echo 1
					fi
				else
					echo 1
				fi
			else
        	    echo "Not found"
      		fi
		#END KEME
	;;


	download)
		#START KEME Script
		if [[ " ${PACKAGE_LIST[*]} " == *" keme "* ]]; then
			echo "Downloading KEME ... please wait"
			cd ${DESTDOWNLOAD}
			if [ -s ${DESTDOWNLOAD}"/"${KEME_DEB} ]
			then
				rm -f ${DESTDOWNLOAD}"/"${KEME_DEB}
			fi
			
			wget -O ${KEME_DEB} ${KEME_URL}
			MD5=$(md5sum "${DESTDOWNLOAD}/${KEME_DEB}"|cut -d' ' -f1)
			if [ "x${KEME_MD5}" = "x${MD5}" -a -s "${DESTDOWNLOAD}/${KEME_DEB}" ];then
				echo "KEME installed successfully"
			else	
				echo "Error donwloading KEME project.... please contact with LliureX Team in www.lliurex.net"
				exit 1
			fi 
		fi
		#END KEME Script
	;;

	preInstall)

		#START KEME
			if [[ " ${PACKAGE_LIST[*]} " == *" keme "* ]]; then
				echo "****PreInstalling keme plugins... please wait****"
				apt install gdebi-core -y
				echo " * * Finished * * "
			fi
		#END KEME

	;;

	installPackage)

		#START KEME
			if [[ " ${PACKAGE_LIST[*]} " == *" keme "* ]]; then
				# # Dependency is threw into zero-fp-administrativo dependencies
				# echo "****Installing KEME dependencies... please wait****"
				# apt install libqt5sql5-psql -y
				echo "****Installing KEME Suite... please wait****"
				gdebi ${DESTDOWNLOAD}"/"${KEME_DEB} -n
				echo " * * Finished * * "
			fi
		#END KEME

		#START ODOO
			if [[ " ${PACKAGE_LIST[*]} " == *" odoo "* ]]; then
				echo "****Installing Odoo 13 & Postgres dockers... please wait****"
				docker image pull postgres:10 
				docker image pull odoo
				inst="$(docker ps -a -q -f ancestor=odoo)"
				if [ -n "${inst}" ]; then
				    docker rm -f "${inst}"
				fi
				inst="$(docker ps -a -q -f ancestor=postgres:10)"
				if [ -n "${inst}" ]; then
				    docker rm -f "${inst}"
				fi
				cp /usr/share/zero-fp-administrativo/odoo.desktop /usr/share/applications/
				cp /usr/share/zero-fp-administrativo/odoo.png /usr/share/icons
				cp /usr/share/zero-fp-administrativo/odoorun /usr/bin/odoorun
				cp /usr/share/zero-fp-administrativo/net.lliurex.odoorun.policy /usr/share/polkit-1/actions/
				chmod +x /usr/bin/odoorun
				systemctl restart polkit
				echo " * * Finished * * "
				echo " **********************************************************"
				echo " * ODOO INSTALLED: MANAGE WITH COMMANDLINE TOOL "odoorun"   *"
				echo " **********************************************************"

			fi
		#END ODOO	
			
	;;

	remove)

		for ix in $PACKAGE_LIST	

		do
			#START KEME
			if [[ " $ix " == *" keme "* ]]; then
				if dpkg -s keme-contabilidad-6 >/dev/null 2>&1 ;then
					echo "****Unstalling KEME Suite... please wait****"
					apt-get remove -y keme-contabilidad-6
					echo " * * Finished * * "
				fi
			#END KEME
			#START ODOO
			elif [[ " $ix " == *" odoo "* ]]; then
				echo "****Uninstalling Odoo 13 & Postgres dockers... please wait****"
				inst="$(docker ps -a -q -f ancestor=odoo)"
				if [ -n "${inst}" ]; then
				    docker rm -f "${inst}"
				fi
				inst="$(docker ps -a -q -f ancestor=postgres:10)"
				if [ -n "${inst}" ]; then
				    docker rm -f "${inst}"
				fi
				imgs="$(docker images -q odoo:latest)"
				if [ -n "${imgs}" ];then
				    docker rmi "${imgs}"
				fi
				imgs="$(docker images -q postgres:10)"
				if [ -n "${imgs}" ];then
				    docker rmi "${imgs}"
				fi
				rm -f /usr/share/applications/odoo.desktop
				rm -f /usr/share/icons/odoo.png
				rm -f /usr/bin/odoorun
				rm -f /usr/share/polkit-1/actions/net.lliurex.odoorun.policy
				systemctl restart polkit
				echo " * * Finished * * "
			#END ODOO	
			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
