#!/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 "install-post-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

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

if [ $TP_GIMP -eq 1 ] ; then

  if [ -e ~/.config/GIMP/3.0/plug-ins ] ; then
	echo >> $LOGFILE "Installing GIMP3 plugin"
	mkdir ~/.config/GIMP/3.0/plug-ins/turboprint &>> $LOGFILE
	cp $TPPATH_LIB/gimp3plugin/turboprint.py ~/.config/GIMP/3.0/plug-ins/turboprint &>> $LOGFILE
  fi
  if [ -e ~/.config/GIMP/3.2/plug-ins ] ; then
	echo >> $LOGFILE "Installing GIMP3 plugin"
	mkdir ~/.config/GIMP/3.2/plug-ins/turboprint &>> $LOGFILE
	cp $TPPATH_LIB/gimp3plugin/turboprint.py ~/.config/GIMP/3.2/plug-ins/turboprint &>> $LOGFILE
  fi
# try SNAP
  if [ -e ~/snap/gimp/current/.config/GIMP/3.0/plug-ins ] ; then
	echo >> $LOGFILE "Installing GIMP3 plugin SNAP"
	mkdir ~/snap/gimp/current/.config/GIMP/3.0/plug-ins/turboprint &>> $LOGFILE
	cp $TPPATH_LIB/gimp3plugin/turboprint.py ~/snap/gimp/current/.config/GIMP/3.0/plug-ins/turboprint &>> $LOGFILE
  fi
  if [ -e ~/snap/gimp/current/.config/GIMP/3.2/plug-ins ] ; then
	echo >> $LOGFILE "Installing GIMP3 plugin SNAP"
	mkdir ~/snap/gimp/current/.config/GIMP/3.2/plug-ins/turboprint &>> $LOGFILE
	cp $TPPATH_LIB/gimp3plugin/turboprint.py ~/snap/gimp/current/.config/GIMP/3.2/plug-ins/turboprint &>> $LOGFILE
  fi
fi

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

##
# user startmenu icons/apps
# already done by turboprint-startmonitor
##

echo "install-post-user finished $(date)" >> "$LOGFILE"
echo "#######################################################" >> "$LOGFILE"

exit 0
