#!/bin/bash
#
# install-post-user - TurboPrint installation script
#                     user specific tasks
#
# to be run with as the current user
#

##
# get TurboPrint configuration
##

eval $(cat /etc/turboprint/system.cfg)
LOGFILE="$TPPATH_LOG/turboprint/install.log"
TPPATH_LIB="$TPPATH_FILTERS"

echo "#######################################################" >> "$LOGFILE"
echo "uninstall-pre-user started $(date) user=$USER" >> "$LOGFILE"

##
# evaluate command line options
##

TP_GIMP=1
TP_STARTMENU=1
TP_DESKTOP=1

for i do
        case "$i" in
        --gimp=* ) TP_GIMP="${i#*=}" ;;
        --startmenu=* ) TP_STARTMENU="${i#*=}" ;;
        --desktop=* ) TP_DESKTOP="${i#*=}" ;;
        --user=* ) TP_USER="${i#*=}" ;;
        esac
done

##
# user specific tasks
##

##
# the following is also executed by xwindows xinit.rc
# and by systemd global user service (not reliabled)
# wayland has no global user startup script
#
# purpose: execute turboprint-startmonitor at user login
##
#source "$TPPATH_FILTERS/95turboprint_monitor" &> /dev/null

##
# start turboprint-monitor, turboprint-userdaemons
# create desktop icons
# start appindicator applet
##
#source "$TPPATH_FILTERS/turboprint-startmonitor" &> /dev/null

##
# remove gimp 3 plugin
##
# try regular install

if [ $TP_GIMP -eq 1 ] ; then

  if [ -e ~/.config/GIMP/3.0/plug-ins ] ; then
	echo >> $LOGFILE "Uninstalling GIMP3 plugin"
	rm -r ~/.config/GIMP/3.0/plug-ins/turboprint &>> $LOGFILE
  fi
# try SNAP
  if [ -e ~/snap/gimp/current/.config/GIMP/3.0/plug-ins ] ; then
	echo >> $LOGFILE "Uninstalling GIMP3 plugin SNAP"
	rm -r ~/snap/gimp/current/.config/GIMP/3.0/plug-ins/turboprint &>> $LOGFILE
  fi
fi

##
# remove kde 6 plugin
##
type kpackagetool6 &>> $LOGFILE
if [ $? -eq 0 ] ; then
	echo >> $LOGFILE "Uninstalling Plasma 6 applet"
	kpackagetool6 -t Plasma/Applet -r $TPPATH_LIB/kde6applet/com.zedonet.turboprint &>> $LOGFILE
fi

##
# remove user autostart entries
##
if [ -e ~/.config/autostart/turboprint-monitor.desktop ] ; then
	rm ~/.config/autostart/turboprint-monitor.desktop 
fi
if [ -e ~/.config/autostart-scripts/turboprint-startmonitor ] ; then
	rm ~/.config/autostart-scripts/turboprint-startmonitor 
fi

echo "uninstall-pre-user finished $(date)" >> "$LOGFILE"
echo "#######################################################" >> "$LOGFILE"

exit 0
