#!/bin/bash


ACTION="$1"

if [ ${LANG:0:2} == "ca" ]
then
	MSG="El jellyfin està instal·lat. S'ha afegit un enllaç a la pàgina d'inici del navegador.\nExecute firefox per a terminar la configuració."
elif [ ${LANG:0:2} == "es" ]
then
	MSG="Se ha instalado Jellyfin. Se ha añadido un enlace a la página de inicio del navegador.\nEjecute firefox para terminar la configuración."
else
	MSG="Jellyfin is now installed. A link has been added to browser's homepage.\nOpen firefox and finish the configuration from there."
fi

LINK_PATH=/usr/share/zero-lliurex-jellyfin/link/jellyfin.json
LINK_PATH_DEST=/var/lib/lliurex-www/links

shift
PACKAGE_LIST=$@

case $ACTION in
	postInstall)
		systemctl enable jellyfin
		systemctl start jellyfin
	
		cp ${LINK_PATH} ${LINK_PATH_DEST}

		if [ -n "$DISPLAY" ]
		then
			kdialog --msgbox "$MSG" 
		else
			echo "$MSG"
		fi
	;;
	remove)
		systemctl stop jellyfin
		systemctl disable jellyfin
		for ix in $PACKAGE_LIST	
		do
			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
			
			if [ -e ${LINK_PATH_DEST}"/jellyfin.json" ];then
				rm -f ${LINK_PATH_DEST}"/jellyfin.json"
			fi

		done	
				
	;;	
esac
exit 0
