#!/bin/bash
#
# install-post - TurboPrint installation script
#                non-static installation / %post part of rpm
#
# must have root permissions
#
# (c) 2000-2011 ZEDOnet
# 26.03.2008 TurboPrint 2 version
# 08.04.2011 changes for lintian / Ubuntu 11

##
# get TurboPrint configuration
##

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

# override help browser if set by command line

TP_RPM=0
TP_DEB=0
TP_LANGUAGE="en"
TP_GIMP=1
TP_BJNP=1
TP_STARTMENU=1
TP_DESKTOP=1
TP_GSZEDO=1

for i do
	case "$i" in
	--rpm ) TP_RPM=1 ;;
	--deb ) TP_RPM=1 ; TP_DEB=1 ;;
	--cups=* ) TP_CUPS=${i#*=} ;;
	--language=* ) TP_LANGUAGE=${i#*=} ;; 
	--demotype=* ) TP_DEMOTYPE=${i#*=} ;; 
	--charset=* ) TP_CHARSET=${i#*=} ;;
	--browser=* ) TPBIN_BROWSER="${i#*=}" ;;
	--user=* ) TP_USER="${i#*=}" ;;
	--display=* ) TP_DISPLAY="${i#*=}" ;;
	--gimp=* ) TP_GIMP="${i#*=}" ;;
	--bjnp=* ) TP_BJNP="${i#*=}" ;;
	--startmenu=* ) TP_STARTMENU="${i#*=}" ;;
	--desktop=* ) TP_DESKTOP="${i#*=}" ;;
	--gszedo=* ) TP_GSZEDO="${i#*=}" ;;
	esac
done

# prevent problems with upgrading / downgrading

if [ $TP_RPM -eq 1 ] ; then
	TPPATH_VAR=/var/spool
	TPPATH_FILTERS=/usr/lib/turboprint
fi

echo "#######################################################" >> "$LOGFILE"
echo "install-post started $(date)" >> "$LOGFILE"
echo "$(tpsetup --printreginfo)" >> "$LOGFILE"

echo "TP_RPM=$TP_RPM" >> "$LOGFILE"
echo "TP_CUPS=$TP_CUPS" >> "$LOGFILE"
echo "TP_USER=$TP_USER" >> "$LOGFILE"
echo "TP_DISPLAY=$TP_DISPLAY" >> "$LOGFILE"

case $TP_LANGUAGE in
de)
TP_INSTALLING_DAEMON="Installiere TurboPrint Daemon mit"
TP_RESTARTING_DAEMON="Neustart des TurboPrint Drucker-Port Daemon"
TP_CREATING_PPD="Generiere PPD Druckerbeschreibungs-Dateien..."
TP_DONE="fertig"
TP_UPDATING_PRINTERS="Aktualisiere bestehende TurboPrint Drucker-Einträge..."
TP_INSTALLING="Installiere" 
TP_INSTALLING_APPLET="Installiere Druckermonitor-Miniprogramm für"
TP_SELINUX="Installiere Modul für Security-Enhanced Linux"
;;
es)
TP_INSTALLING_DAEMON="Instalando el demonio TurboPrint con"
TP_RESTARTING_DAEMON="Reiniciando el demonio de puerto de impresora de TurboPrint"
TP_CREATING_PPD="Creando los archivos PPD de descripción de impresora..."
TP_DONE="hecho"
TP_UPDATING_PRINTERS="Actualizando las entradas de impresoras TurboPrint existentes..."
TP_INSTALLING="Instalando"
TP_INSTALLING_APPLET="Instalando el applet de monitorización de impresión para"
TP_SELINUX="Añadiendo el módulo para Security-Enhanced Linux"
;;
*)
TP_INSTALLING_DAEMON="Installing TurboPrint daemon with"
TP_RESTARTING_DAEMON="Restarting TurboPrint printer port daemon"
TP_CREATING_PPD="Creating PPD printer description files..."
TP_DONE="done"
TP_UPDATING_PRINTERS="Updating existing TurboPrint printer entries..."
TP_INSTALLING="Installing"
TP_INSTALLING_APPLET="Installing printer monitor applet for"
TP_SELINUX="Adding module for Security-Enhanced Linux"
;;
esac

if [ $TP_RPM -eq 1 ] ; then
	# additional setup if called during rpm installation
	# with RPM package now always CUPS required
	TP_CUPS=1
	#if [ -e "$TPPATH_CUPSLIB" -a -e "$TPPATH_CUPSDRIVER" ] ; then
	#	TP_CUPS=1
	#else
	#	TP_CUPS=0
	#fi
fi

# set file permissions

umask 0022
chmod 755 "$TPPATH_SHARE"
# mkdir -p "$TPPATH_DOC" # already done by package manager
chmod 755 "$TPPATH_DOC"

# copy docs

if [ ! -e "$TPPATH_SHARE/doc" ] ; then
	mkdir -p "$TPPATH_SHARE/doc"
	cp -r $TPPATH_DOC/* "$TPPATH_SHARE/doc" # maintain old location if old system.cfg
fi

# use group of /etc/cups/printers.conf as TPDAEMON_GROUP

if [ -e /etc/cups/printers.conf ] ; then
	TPDAEMON_GROUP=$("$TPPATH_FILTERS/tpstdin" --getgroup /etc/cups/printers.conf)
	if [ $TPDAEMON_GROUP == "root" ] ; then
		TPDAEMON_GROUP=lp
	fi
	echo "TPDAEMON_USER=$TPDAEMON_USER" >> "$LOGFILE"
	echo "TPDAEMON_GROUP=$TPDAEMON_GROUP" >> "$LOGFILE"
	echo "TP_GIMP=$TP_GIMP" >> "$LOGFILE"
	echo "TP_BJNP=$TP_BJNP" >> "$LOGFILE"
	echo "TP_STARTMENU=$TP_STARTMENU" >> "$LOGFILE"
	echo "TP_DESKTOP=$TP_DESKTOP" >> "$LOGFILE"
	echo "TP_GSZEDO=$TP_GSZEDO" >> "$LOGFILE"
fi

# complete some paths (upgrade from TurboPrint 1.x)

if [ -z "$TPPATH_VAR" ] ; then
	TPPATH_VAR="/var"
fi
if [ -z $TPDAEMON_START ] ; then
	TPDAEMON_START=1
fi
if [ -z $TPDAEMON_USER ] ; then
	TPDAEMON_USER=lp
fi
if [ -z $TPDAEMON_GROUP ] ; then
	TPDAEMON_GROUP=lp
fi
if [ -z $TPDAEMON_PORT ] ; then
	TPDAEMON_PORT=5552
fi
if [ -z $TPDAEMON_SERVER ] ; then
	TPDAEMON_SERVER=1
fi
if [ -z "$TPPATH_CUPSLIB" ] ; then
	TPPATH_CUPSLIB="/usr/lib/cups"
fi
if [ -z "$TPPATH_CUPSLIB64" ] ; then
	TPPATH_CUPSLIB64="/usr/lib64/cups"
fi
if [ -z "$TPPATH_CUPSDRIVER" ] ; then
	TPPATH_CUPSDRIVER="/usr/share/cups/model"
fi
if [ -z $TPUSE_GSZEDO ] ; then
	TPUSE_GSZEDO=1
fi
if [ -z $TPCONVERT_PDF ] ; then
	TPCONVERT_PDF=0
fi

# write system.cfg

echo > "$TPPATH_CONFIG/system.cfg" "TP_LANGUAGE=$TP_LANGUAGE"
echo >> "$TPPATH_CONFIG/system.cfg" "TP_CHARSET=$TP_CHARSET"
#echo >> "$TPPATH_CONFIG/system.cfg" "TP_INSTALLATIONSTATE=$TP_RPM"
echo >> "$TPPATH_CONFIG/system.cfg" "TP_CUPS=$TP_CUPS"
echo >> "$TPPATH_CONFIG/system.cfg" "TP_UNIT=$TP_UNIT"
echo >> "$TPPATH_CONFIG/system.cfg" "TP_DEMOTYPE=$TP_DEMOTYPE"
echo >> "$TPPATH_CONFIG/system.cfg" "TPBIN_BROWSER=$TPBIN_BROWSER"
echo >> "$TPPATH_CONFIG/system.cfg" "TPFILE_PRINTCAP=$TPFILE_PRINTCAP"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_CONFIG=$TPPATH_CONFIG"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_SHARE=$TPPATH_SHARE"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_SPOOL=$TPPATH_SPOOL"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_BIN=$TPPATH_BIN"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_FILTERS=$TPPATH_FILTERS"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_DOC=$TPPATH_DOC"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_LOG=$TPPATH_LOG"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_VAR=$TPPATH_VAR"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_TEMP=$TPPATH_TEMP"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_MAN=$TPPATH_MAN"
#echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_CUPSDRIVER=$TPPATH_CUPSDRIVER"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_CUPSSETTINGS=$TPPATH_CUPSSETTINGS"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_CUPSLIB=$TPPATH_CUPSLIB"
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_CUPSLIB64=$TPPATH_CUPSLIB64"
echo >> "$TPPATH_CONFIG/system.cfg" "TPOWN_SPOOLDIR=$TPOWN_SPOOLDIR"
echo >> "$TPPATH_CONFIG/system.cfg" "TPMOD_SPOOLDIR=$TPMOD_SPOOLDIR"
echo >> "$TPPATH_CONFIG/system.cfg" "TPOWN_SPOOLFILE=$TPOWN_SPOOLFILE"
echo >> "$TPPATH_CONFIG/system.cfg" "TPMOD_SPOOLFILE=$TPMOD_SPOOLFILE"
echo >> "$TPPATH_CONFIG/system.cfg" "TPDAEMON_START=$TPDAEMON_START"
echo >> "$TPPATH_CONFIG/system.cfg" "TPDAEMON_USER=$TPDAEMON_USER"
echo >> "$TPPATH_CONFIG/system.cfg" "TPDAEMON_GROUP=$TPDAEMON_GROUP"
echo >> "$TPPATH_CONFIG/system.cfg" "TPDAEMON_PORT=$TPDAEMON_PORT"
echo >> "$TPPATH_CONFIG/system.cfg" "TPDAEMON_SERVER=$TPDAEMON_SERVER"
echo >> "$TPPATH_CONFIG/system.cfg" "TPUSE_GSZEDO=$TPUSE_GSZEDO"
echo >> "$TPPATH_CONFIG/system.cfg" "TPCONVERT_PDF=$TPCONVERT_PDF"

# write turboprint-global.cfg - move to system.cfg?

if [ ! -e "$TPPATH_CONFIG/turboprint-global.cfg" ] ; then
	echo > "$TPPATH_CONFIG/turboprint-global.cfg" "[PrintFab_Settings]"
	echo >> "$TPPATH_CONFIG/turboprint-global.cfg" 'UserTemp=".turboprint/temp"'
	echo >> "$TPPATH_CONFIG/turboprint-global.cfg" "Hotfolder=\"TurboPrint/Hotfolder\""
fi

# create turboprint.cfg with correct permissons

touch "$TPPATH_CONFIG/turboprint.cfg"
chmod 644 "$TPPATH_CONFIG/turboprint.cfg"
chown -R root "$TPPATH_CONFIG"
chgrp -R root "$TPPATH_CONFIG"

# create profile list if not already present

TPPATH_PROFILE="$TPPATH_SHARE/profiles/profiles.lst"
if [ ! -e "$TPPATH_PROFILE" ] ; then
	echo "[Turboprint_Profile_List]" > "$TPPATH_PROFILE"
fi
chmod 644 "$TPPATH_PROFILE"

# add preinstalled colorspace profiles

for i in "$TPPATH_SHARE/installprofiles/"*.icc ; do
	tpsetup --addcolorspace "$i" >> "$LOGFILE"
done

# create pagesize list if not already present

TPPATH_PAGESIZE="$TPPATH_SHARE/profiles/pagesizes.lst"
if [ ! -e "$TPPATH_PAGESIZE" ] ; then
	echo "[Turboprint_Pagesize_List]" > "$TPPATH_PAGESIZE"
fi
chmod 644 "$TPPATH_PROFILE"

# install DynaPDF library

if [ -e "/usr/lib" ] ; then
	install --mode=755 "$TPPATH_FILTERS/libdynapdf.so" "/usr/lib"
fi
if [ -e "/usr/lib64" ] ; then
	install --mode=755 "$TPPATH_FILTERS/libdynapdf.so" "/usr/lib64"
fi

# change log file owner & group

chown lp "$TPPATH_LOG/turboprint"
chgrp lp "$TPPATH_LOG/turboprint"
chmod 777 "$TPPATH_LOG/turboprint"
chown lp "$TPPATH_LOG/turboprint/turboprint_cups.log"
chgrp lp "$TPPATH_LOG/turboprint/turboprint_cups.log"
chmod 664 "$TPPATH_LOG/turboprint/turboprint_cups.log"
chmod 666 "$TPPATH_LOG/turboprint/turboprint_monitor.log"
chmod 666 "$TPPATH_LOG/turboprint/turboprint_composer.log"

# change var file owner & group

chown lp "$TPPATH_VAR/turboprint"
chgrp lp "$TPPATH_VAR/turboprint"
chmod 775 "$TPPATH_VAR/turboprint"
chown lp "$TPPATH_VAR/turboprint/ink"
chgrp lp "$TPPATH_VAR/turboprint/ink"
chmod 775 "$TPPATH_VAR/turboprint/ink"
chown $TPDAEMON_USER "$TPPATH_VAR/turboprint/tpstatus"
chgrp $TPDAEMON_GROUP "$TPPATH_VAR/turboprint/tpstatus"

# set suid bit for pipeutility

chmod 755 "$TPPATH_FILTERS/pipeutility"
chmod +sx "$TPPATH_FILTERS/pipeutility"
cp "$TPPATH_FILTERS/pipeutility" "$TPPATH_SHARE/lib" >> $LOGFILE
chown root "$TPPATH_SHARE/lib/pipeutility" 
chgrp root "$TPPATH_SHARE/lib/pipeutility" 
chmod +sx "$TPPATH_SHARE/lib/pipeutility" >> $LOGFILE

# set executable bit for tpstdin

chmod +x "$TPPATH_FILTERS/tpstdin"

##
# add selinux module
##

if [ -e /usr/sbin/semodule ] ; then
	echo $TP_SELINUX
	checkmodule -M -m -o "$TPPATH_FILTERS/turboprint.mod" "$TPPATH_FILTERS/turboprint.te"
	semodule_package -o "$TPPATH_FILTERS/turboprint.pp" -m "$TPPATH_FILTERS/turboprint.mod"
	/usr/sbin/semodule -i "$TPPATH_FILTERS/turboprint.pp" &> /dev/null
	echo $TP_DONE
fi

##
# install turboprint daemon service (start daemon at boot time)
# using systemd or systemv rc scripts
##

# detect if systemd is running
TP_SYSTEMD=0
if [ -e /etc/systemd/system ] ; then
	systemctl -q is-active cups &> /dev/null
	if [ $? -eq 0 ] ; then
		TP_SYSTEMD=1
	fi
	systemctl -q is-active org.cups.cupsd &> /dev/null
	if [ $? -eq 0 ] ; then
		TP_SYSTEMD=2
	fi
fi

# detect systemv directory
TP_INIT="/etc/init.d"
if [ ! -d "$TP_INIT" -a -d /etc/rc.d ] ; then
	TP_INIT="/etc/rc.d"
fi

if [ $TP_SYSTEMD -ne 0 ] ; then
	echo "$TP_INSTALLING_DAEMON systemd"
	echo "installing TurboPrint daemon with systemd" >> $LOGFILE
	# systemd
	# uninstall systemv scripts
	type update-rc.d &> /dev/null && update-rc.d tpdaemon remove &> /dev/null
	type update-rc.d &> /dev/null || insserv -r tpdaemon &> /dev/null
	rm /etc/init.d/tpdaemon &> /dev/null
	rm /etc/rc2.d/S12tpdaemon &> /dev/null
	rm /etc/rc3.d/S12tpdaemon &> /dev/null
	rm /etc/rc5.d/S12tpdaemon &> /dev/null

	# install systemd unit
	if [ $TP_SYSTEMD -eq 2 ] ; then
		install --mode=644 $TPPATH_FILTERS/tpdaemon.service.org /etc/systemd/system/tpdaemon.service
	else
		install --mode=644 $TPPATH_FILTERS/tpdaemon.service /etc/systemd/system
	fi
	# install systemd user unit
	install --mode=644 $TPPATH_FILTERS/turboprint.user.service /etc/systemd/user
	systemctl daemon-reload
	systemctl enable tpdaemon.service
	systemctl --global enable turboprint.user.service
	
else
	echo "$TP_INSTALLING_DAEMON systemv init"
	echo "installing TurboPrint daemon with systemv-init TP_INIT=$TP_INIT" >> $LOGFILE
	# systemv init
	install --mode=755 "$TPPATH_FILTERS/tpdaemon" "$TP_INIT"
	ln -s "$TP_INIT/tpdaemon" /etc/rc2.d/S12tpdaemon &> /dev/null
	ln -s "$TP_INIT/tpdaemon" /etc/rc3.d/S12tpdaemon &> /dev/null
	ln -s "$TP_INIT/tpdaemon" /etc/rc5.d/S12tpdaemon &> /dev/null
	##
	# on SuSE the daemon must be added with insserv
	# on Ubuntu insserv exists but may not be used
	# (on Ubuntu update-rc.d must be used instead)
	##
	type update-rc.d &> /dev/null && update-rc.d tpdaemon defaults &> /dev/null
	type update-rc.d &> /dev/null || insserv tpdaemon &> /dev/null
fi

##
# install X-Windows rc script (start turboprint-monitor when user logs in)
##

if [ -e /etc/X11/xinit/xinitrc.d ] ; then
  install --mode=755 "$TPPATH_FILTERS/95turboprint_monitor" /etc/X11/xinit/xinitrc.d
fi
if [ -e /etc/X11/Xsession.d ] ; then
  install --mode=755 "$TPPATH_FILTERS/95turboprint_monitor" /etc/X11/Xsession.d
fi

# (re)start tpdaemon now
"$TPPATH_FILTERS/tpdaemon" restart &> /dev/null

# (re)start turboprint-monitor now - TODO: start as user, not root

killall turboprint-monitor &> /dev/null
killall turboprint-userdaemon &> /dev/null
killall tpapplet.py &> /dev/null

# moved to install-post-user
#echo "$TP_RESTARTING_DAEMON"
#echo "Restarting tprintdaemon as USER=$TP_USER DISPLAY=$TP_DISPLAY" >> "$LOGFILE"

#if [ ! -z $TP_USER -a ! -z $TP_DISPLAY ] ; then
#	export DISPLAY=$TP_DISPLAY
#	su $TP_USER -c -m "source $TPPATH_FILTERS/95turboprint_monitor" &> /dev/null
	#echo "Restarting turboprint-userdaemon" >> $LOGFILE
	#su $TP_USER -c -m "turboprint-userdaemon &"
	#echo "Restarting turboprint-monitor" >> $LOGFILE
	#su $TP_USER -c -m "turboprint-monitor &"
#fi

##
# generate PPD files; add to CUPS if present
##

mkdir -p "$RBR$TPPATH_SHARE/ppd"
chmod 755 "$RBR$TPPATH_SHARE/ppd"
echo "$TP_CREATING_PPD"
tpsetup --writeppdfiles=$TP_LANGUAGE "$TPPATH_SHARE/ppd/" >> "$LOGFILE"
echo "$TP_DONE"

if [ $TP_CUPS -eq 1 ] ; then
	# different lib path on gentoo Linux
	if [ -e "/usr/libexec/cups/filter" ] ; then
		TPPATH_CUPSLIB="/usr/libexec/cups"
	fi
	if [ -e "$TPPATH_CUPSLIB/filter" ] ; then
		install --mode=755 "$TPPATH_FILTERS/anytoturboprint" "$TPPATH_CUPSLIB/filter"
		install --mode=755 "$TPPATH_FILTERS/commandtoturboprint" "$TPPATH_CUPSLIB/filter"
	fi
	if [ -e "$TPPATH_CUPSLIB/backend" ] ; then
		install --mode=755 "$TPPATH_FILTERS/tpu" "$TPPATH_CUPSLIB/backend"
	fi
	if [ -e "$TPPATH_CUPSLIB64/filter" ] ; then
		install --mode=755 "$TPPATH_FILTERS/pstoturboprint" "$TPPATH_CUPSLIB64/filter"
		install --mode=755 "$TPPATH_FILTERS/commandtoturboprint" "$TPPATH_CUPSLIB64/filter"
	fi
	if [ -e "$TPPATH_CUPSLIB64/backend" ] ; then
		install --mode=755 "$TPPATH_FILTERS/tpu" "$TPPATH_CUPSLIB64/backend"
	fi
	# different cups driver path on Ubuntu
	if [ -e "/usr/share/ppd/hpijs" ] ; then
		TPPATH_CUPSDRIVER="/usr/share/ppd"
	fi
	mkdir -p "$TPPATH_CUPSDRIVER/turboprint"
	chmod 755 "$TPPATH_CUPSDRIVER/turboprint"
	cp "$TPPATH_SHARE/ppd/"* "$TPPATH_CUPSDRIVER/turboprint"
	chmod 644 "$TPPATH_CUPSDRIVER/turboprint/"*
fi
echo >> "$TPPATH_CONFIG/system.cfg" "TPPATH_CUPSDRIVER=$TPPATH_CUPSDRIVER"

# call setup to update LPD config files / CUPS PPD files
echo "$TP_UPDATING_PRINTERS"
if [ $TP_DEB -a -e "$TPPATH_CONFIG/turboprint.bak" ] ; then
	# restore turboprint.cfg for Debian upgrade mode
        mv "$TPPATH_CONFIG/turboprint.bak" "$TPPATH_CONFIG/turboprint.cfg"
	tpsetup --restore
else
	tpsetup --update
fi
if [ $? -eq 11 ] ; then
	# TurboPrint printers installed => update, no fresh install
	echo >> "$TPPATH_CONFIG/system.cfg" "TP_INSTALLATIONSTATE=0"
else
	echo >> "$TPPATH_CONFIG/system.cfg" "TP_INSTALLATIONSTATE=$TP_RPM"
fi
echo "$TP_DONE"

# restart CUPS scheduler

#if [ $TP_CUPS -eq 1 ] ; then
#	echo "Restarting CUPS printing system..."
#	/etc/init.d/cups restart &> /dev/null
#	/etc/init.d/cupsys restart &> /dev/null
#	echo ""
#fi

# remove dump files

if [ -e "$TPPATH_SHARE/dump/dj970aligncontrol.prn" ] ; then
	rm -r "$TPPATH_SHARE/dump/dj970aligncontrol.prn"
fi

# remove tmp files

rm /tmp/tusblog &> /dev/null

##
# icons & applets
##

# add TurboPrint icon bitmap file

type xdg-icon-resource &> /dev/null
if [ $? -eq 0 ] ; then
	echo "$TP_INSTALLING icon images"
	xdg-icon-resource install --size 128 "$TPPATH_SHARE/img/turboprint-applet-128.png" turboprint-applet
	xdg-icon-resource install --size 128 "$TPPATH_SHARE/img/turboprint-icon-128.png" turboprint-icon
	xdg-icon-resource install --context mimetypes --size 128 "$TPPATH_SHARE/img/turboprint-key-128.png" application-turboprint-key
	xdg-icon-resource install --context mimetypes --size 128 "$TPPATH_SHARE/img/turboprint-profile-128.png" application-turboprint-profile
	xdg-icon-resource install --size 128 "$TPPATH_SHARE/img/turboprint-monitor-128.png" turboprint-monitor
	xdg-icon-resource install --size 128 "$TPPATH_SHARE/img/turboprint-composer-128.png" turboprint-composer
	xdg-icon-resource install --size 48 "$TPPATH_SHARE/img/turboprint-applet.png" turboprint-applet
	xdg-icon-resource install --size 48 "$TPPATH_SHARE/img/turboprint-icon.png" turboprint-icon
	xdg-icon-resource install --context mimetypes --size 48 "$TPPATH_SHARE/img/turboprint-key.png" application-turboprint-key
	xdg-icon-resource install --context mimetypes --size 48 "$TPPATH_SHARE/img/turboprint-profile.png" application-turboprint-profile
	xdg-icon-resource install --size 48 "$TPPATH_SHARE/img/turboprint-monitor.png" turboprint-monitor
	xdg-icon-resource install --size 48 "$TPPATH_SHARE/img/turboprint-composer.png" turboprint-composer
else
    if [ -e /usr/share/pixmaps ] ; then
	install --mode=644 "$TPPATH_SHARE/img/turboprint-applet.png" /usr/share/pixmaps
	install --mode=644 "$TPPATH_SHARE/img/turboprint-icon.png" /usr/share/pixmaps
	install --mode=644 "$TPPATH_SHARE/img/turboprint-monitor.png" /usr/share/pixmaps
    fi
fi

##
# create desktop icon entries
##

mkdir /etc/turboprint/icons &> /dev/null
chmod 755 /etc/turboprint/icons &> /dev/null
rm /etc/turboprint/icons/turboprint.desktop &> /dev/null

TP_ENTRY="$TPPATH_CONFIG/icons/turboprint-control.desktop"
echo > "$TP_ENTRY" "[Desktop Entry]"
echo >> "$TP_ENTRY" "Type=Application"
echo >> "$TP_ENTRY" "Exec=turboprint"
echo >> "$TP_ENTRY" "Icon=turboprint-icon"
echo >> "$TP_ENTRY" "StartupNotify=true"
echo >> "$TP_ENTRY" "DocPath=$TPPATH_DOC/TurboPrint_Manual.pdf"
echo >> "$TP_ENTRY" "Comment=Setup & configure TurboPrint printers"
echo >> "$TP_ENTRY" "Terminal=0"
echo >> "$TP_ENTRY" "Name=TurboPrint Control"
echo >> "$TP_ENTRY" "Categories=System;Settings;"
chmod 755 "$TP_ENTRY"

TP_ENTRY="$TPPATH_CONFIG/icons/turboprint-composer.desktop"
echo > "$TP_ENTRY" "[Desktop Entry]"
echo >> "$TP_ENTRY" "Type=Application"
echo >> "$TP_ENTRY" "Exec=turboprint-composer"
echo >> "$TP_ENTRY" "Icon=turboprint-composer"
echo >> "$TP_ENTRY" "StartupNotify=true"
echo >> "$TP_ENTRY" "DocPath=$TPPATH_DOC/TurboPrint_Manual.pdf"
echo >> "$TP_ENTRY" "Comment=Print images and documents with TurboPrint printer"
echo >> "$TP_ENTRY" "Terminal=0"
echo >> "$TP_ENTRY" "Name=TurboPrint Composer"
echo >> "$TP_ENTRY" "Categories=Graphics;Photography;Printing;"
chmod 755 "$TP_ENTRY"

TP_ENTRY="$TPPATH_CONFIG/icons/turboprint-monitor.desktop"
echo > "$TP_ENTRY" "[Desktop Entry]"
echo >> "$TP_ENTRY" "Type=Application"
echo >> "$TP_ENTRY" "Exec=turboprint-monitor"
echo >> "$TP_ENTRY" "Icon=turboprint-monitor"
echo >> "$TP_ENTRY" "StartupNotify=false"
echo >> "$TP_ENTRY" "DocPath=$TPPATH_DOC/TurboPrint_Manual.pdf"
echo >> "$TP_ENTRY" "Comment=Monitor TurboPrint printers"
echo >> "$TP_ENTRY" "Terminal=0"
echo >> "$TP_ENTRY" "Name=TurboPrint Monitor"
echo >> "$TP_ENTRY" "Categories=System;Settings;"
chmod 755 "$TP_ENTRY"

# remove TurboPrint 2beta components

rm /usr/lib/cups/backend/tusb &> /dev/null
rm /usr/lib64/cups/backend/tusb &> /dev/null

# remove TurboPrint 1.x applications

rm "$TPPATH_BIN/xtpconfig" &> /dev/null
rm "$TPPATH_BIN/xtpsetup" &> /dev/null

# add to KDE/Gnome application menu

rm /usr/share/applications/turboprint.desktop &> /dev/null

if [ $TP_STARTMENU -eq 1 ] ; then

	type xdg-desktop-menu &> /dev/null
	if [ $? -eq 0 ] ; then
		echo "$TP_INSTALLING turboprint menu entry"
		xdg-desktop-menu install "/etc/turboprint/icons/turboprint-control.desktop"
		xdg-desktop-menu install "/etc/turboprint/icons/turboprint-monitor.desktop"
		xdg-desktop-menu install "/etc/turboprint/icons/turboprint-composer.desktop"
	else
    		if [ -e /usr/share/applications ] ; then
			cp /etc/turboprint/icons/* /usr/share/applications
    		fi
	fi
fi

# Desktop icons

if [ $TP_DESKTOP -eq 1 ] ; then

    # flag wether icons should be installed for new users
    echo "" > /etc/turboprint/copyicons

    if [ $TP_USER ] ; then
        USER_HOME="/home/$TP_USER"

	# remove TurboPrint 1.x desktop icons

	rm "$USER_HOME/KDesktop/xtpsetup.desktop" &> /dev/null
	rm "$USER_HOME/KDesktop/xtpconfig.desktop" &> /dev/null
	rm "$USER_HOME/Desktop/xtpsetup.desktop" &> /dev/null
	rm "$USER_HOME/Desktop/xtpconfig.desktop" &> /dev/null
	rm "$USER_HOME/Desktop/turboprint.desktop" &> /dev/null

	# add new icons to current user's desktop

	type xdg-desktop-icon &> /dev/null
	if [ $? -eq 0 ] ; then
		echo "$TP_INSTALLING icons for user $TP_USER"
		su "$TP_USER" -c -m "xdg-desktop-icon install /etc/turboprint/icons/turboprint-control.desktop" &> /dev/null
		su "$TP_USER" -c -m "xdg-desktop-icon install /etc/turboprint/icons/turboprint-monitor.desktop" &> /dev/null

		# mime type for turboprint key file

		type xdg-mime &> /dev/null
		if [ $? -eq 0 ] ; then
			su "$TP_USER" -c -m "xdg-mime install '$TPPATH_SHARE/img/turboprint-tpkey.xml'"
			su "$TP_USER" -c -m "xdg-mime install '$TPPATH_SHARE/img/turboprint-profile.xml'"
			if [ -e "$USER_HOME/.local/share" ] ; then
				su "$TP_USER" -c -m "mkdir -p '$USER_HOME/.local/share/applications'"
				su "$TP_USER" -c -m "touch '$USER_HOME/.local/share/applications/mimeapps.list'"
			fi
			su "$TP_USER" -c -m "xdg-mime default turboprint-control.desktop application/turboprint-key"
			su "$TP_USER" -c -m "xdg-mime default turboprint-control.desktop application/turboprint-profile"
		fi

	else
	    if [ -e "$USER_HOME/KDesktop" ] ; then
		cp /etc/turboprint/icons/* "$USER_HOME/KDesktop"
		chown "$TP_USER" "$USER_HOME/KDesktop/turboprint"*.desktop
		chmod +x "$USER_HOME/KDesktop/turboprint"*.desktop
	    fi
	    if [ -e "$USER_HOME/Desktop" ] ; then
		cp /etc/turboprint/icons/* "$USER_HOME/Desktop"
		chown "$TP_USER" "$USER_HOME/Desktop/turboprint"*.desktop
		chmod +x "$USER_HOME/Desktop/turboprint"*.desktop
	    fi
	fi
    else
	# add new icons to all users' desktops

	for i in /home/*; do
		USER="${i##*/}"
		type xdg-desktop-icon &> /dev/null
		if [ $? -eq 0 ] ; then
			echo "$TP_INSTALLING icons for user $USER"
			su "$USER" -c -m "xdg-desktop-icon install /etc/turboprint/icons/turboprint-control.desktop" &> /dev/null
			su "$USER" -c -m "xdg-desktop-icon install /etc/turboprint/icons/turboprint-monitor.desktop" &> /dev/null
			type xdg-mime &> /dev/null
			if [ $? -eq 0 ] ; then
			    su "$USER" -c -m "xdg-mime install '$TPPATH_SHARE/img/turboprint-tpkey.xml'" &> /dev/null
			    if [ -e "$i/.local/share" ] ; then
				su "$USER" -c -m "mkdir -p '$i/.local/share/applications'" &> /dev/null
				su "$USER" -c -m "touch '$i/.local/share/applications/mimeapps.list'" &> /dev/null
			    fi
			fi
			su "$USER" -c -m "xdg-mime default turboprint-control.desktop application/turboprint-key" &> /dev/null
			su "$USER" -c -m "xdg-mime default turboprint-control.desktop application/turboprint-profile" &> /dev/null
		else
       		    if [ -e "$i/KDesktop" ] ; then
			rm "$i/KDesktop/xtpconfig.desktop" &> /dev/null
			rm "$i/KDesktop/xtpsetup.desktop" &> /dev/null
			rm "$i/KDesktop/turboprint.desktop" &> /dev/null
                	cp /etc/turboprint/icons/* "$i/KDesktop"
			chown "$USER" "$i/KDesktop/turboprint"*.desktop &> /dev/null
			chmod +x "$i/KDesktop/turboprint"*.desktop &> /dev/null
        	    fi
        	    if [ -e "$i/Desktop" ] ; then
			rm "$i/Desktop/xtpconfig.desktop" &> /dev/null
			rm "$i/Desktop/xtpsetup.desktop" &> /dev/null
			rm "$i/Desktop/turboprint.desktop" &> /dev/null
                	cp /etc/turboprint/icons/* "$i/Desktop"
			chown "$USER" "$i/Desktop/turboprint"*.desktop &> /dev/null
			chmod +x "$i/Desktop/turboprint"*.desktop &> /dev/null
        	    fi
		fi
	done
    fi
else
    rm /etc/turboprint/copyicons &> /dev/null
fi

###
# install KDE applet
###
if [ -e /usr/share/apps/kicker/applets -a -e /usr/lib/kde3 ] ; then
	echo "$TP_INSTALLING_APPLET KDE"
	install --mode=644 "$TPPATH_FILTERS/kde3applet/tpkdeapplet.desktop" /usr/share/apps/kicker/applets
	install --mode=755 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.so" /usr/lib/kde3
	install --mode=644 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.la" /usr/lib/kde3
fi

if [ -e /opt/kde3/lib/kde3 -a -e /opt/kde3/share/apps/kicker/applets ] ; then
	echo "$TP_INSTALLING_APPLET KDE S"
	install --mode=644 "$TPPATH_FILTERS/kde3applet/tpkdeapplet.desktop" /opt/kde3/share/apps/kicker/applets
	install --mode=755 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.so" /opt/kde3/lib/kde3
	install --mode=644 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.la" /opt/kde3/lib/kde3
fi

if [ -e /usr/share/apps/kicker/applets -a -e /usr/lib64/kde3 ] ; then
	echo "$TP_INSTALLING_APPLET KDE 64bit"
	install --mode=644 "$TPPATH_FILTERS/kde3applet/tpkdeapplet.desktop" /usr/share/apps/kicker/applets
	install --mode=755 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.so" /usr/lib64/kde3
	install --mode=644 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.la" /usr/lib64/kde3
fi

if [ -e /opt/kde3/lib64/kde3 -a -e /opt/kde3/share/apps/kicker/applets ] ; then
	echo "$TP_INSTALLING_APPLET KDE S 64bit"
	install --mode=644 "$TPPATH_FILTERS/kde3applet/tpkdeapplet.desktop" /opt/kde3/share/apps/kicker/applets
	install --mode=755 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.so" /opt/kde3/lib64/kde3
	install --mode=644 "$TPPATH_FILTERS/kde3applet/libtpkdeapplet.la" /opt/kde3/lib64/kde3
fi

if [ -e /usr/share/kde4/services ] ; then
	# determine KDE version
	KDE_VERSION="42"
	kde4-config -v | grep "KDE 4.1" &> /dev/null && KDE_VERSION="41"
	echo "$TP_INSTALLING_APPLET KDE $KDE_VERSION"
	install --mode=644 "$TPPATH_FILTERS/kde${KDE_VERSION}applet/plasma-applet-turboprint.desktop" /usr/share/kde4/services
	if [ -e /usr/lib/kde4 ] ; then
		install --mode=755 "$TPPATH_FILTERS/kde${KDE_VERSION}applet/plasma_applet_turboprint.so" /usr/lib/kde4
	fi
	if [ -e /usr/lib64/kde4 ] ; then
		install --mode=755 "$TPPATH_FILTERS/kde${KDE_VERSION}applet/plasma_applet_turboprint.so" /usr/lib64/kde4
	fi
	if [ $TP_USER ] ; then
		su $TP_USER -c -m "kbuildsycoca4" &> /dev/null
	fi
fi

type kbuildsycoca5 &> /dev/null
if [ $? -eq 0 ] ; then
	echo "$TP_INSTALLING_APPLET KDE 5"
	plasmapkg2 -g -t plasmoid -i "$TPPATH_FILTERS/kde5applet"
        if [ $TP_USER ] ; then
                su $TP_USER -c -m "kbuildsycoca5" &> /dev/null
        fi
	cp -r $TPPATH_FILTERS/kde5applet/contents/locale/* /usr/share/locale
fi

###
# install Gnome applet
###
if [ -e /usr/lib/bonobo/servers ] ; then
	echo "$TP_INSTALLING_APPLET Gnome"
	install --mode=644 "$TPPATH_FILTERS/gnomeapplet/tpmonapplet.server" /usr/lib/bonobo/servers
	install --mode=755 "$TPPATH_FILTERS/gnomeapplet/tpgnomeapplet" /usr/bin
fi
if [ -e /usr/lib64/bonobo/servers ] ; then
	echo "$TP_INSTALLING_APPLET Gnome 64bit"
	install --mode=644 "$TPPATH_FILTERS/gnomeapplet/tpmonapplet.server" /usr/lib64/bonobo/servers
	install --mode=755 "$TPPATH_FILTERS/gnomeapplet/tpgnomeapplet" /usr/bin
fi

if [ -e /opt/gnome/lib/bonobo/servers ] ; then
	echo "$TP_INSTALLING_APPLET Gnome S"
	install --mode=644 "$TPPATH_FILTERS/gnomeapplet/tpmonapplet.server" /opt/gnome/lib/bonobo/servers
	install --mode=755 "$TPPATH_FILTERS/gnomeapplet/tpgnomeapplet" /opt/gnome/bin
fi
if [ -e /opt/gnome/lib64/bonobo/servers ] ; then
	echo "$TP_INSTALLING_APPLET Gnome S 64bit"
	install --mode=644 "$TPPATH_FILTERS/gnomeapplet/tpmonapplet.server" /opt/gnome/lib64/bonobo/servers
	install --mode=755 "$TPPATH_FILTERS/gnomeapplet/tpgnomeapplet" /opt/gnome/bin
fi

##
# install GIMP Print Plugin
##
if [ $TP_GIMP -eq 1 ] ; then
  if [ -e /opt/gnome/lib/gimp/2.0/plug-ins ] ; then
	echo "$TP_INSTALLING Gimp TurboPrint Plugin S"
	install --mode=755 "$TPPATH_FILTERS/gimpplugin/gpturboprint" /opt/gnome/lib/gimp/2.0/plug-ins
  else
    if [ -e /usr/lib/gimp/2.0/plug-ins ] ; then
	echo "$TP_INSTALLING Gimp TurboPrint Plugin"
	install --mode=755 "$TPPATH_FILTERS/gimpplugin/gpturboprint" /usr/lib/gimp/2.0/plug-ins
    fi
  fi
  if [ -e /opt/gnome/lib64/gimp/2.0/plug-ins ] ; then
	echo "$TP_INSTALLING Gimp TurboPrint Plugin S 64bit"
	install --mode=755 "$TPPATH_FILTERS/gimpplugin/gpturboprint" /opt/gnome/lib64/gimp/2.0/plug-ins
  else
    if [ -e /usr/lib64/gimp/2.0/plug-ins ] ; then
	echo "$TP_INSTALLING Gimp TurboPrint Plugin 64bit"
	install --mode=755 "$TPPATH_FILTERS/gimpplugin/gpturboprint" /usr/lib64/gimp/2.0/plug-ins
    fi
  fi
  mkdir -p /usr/local/share/locale/de/LC_MESSAGES
  chmod 755 /usr/local/share/locale/de/LC_MESSAGES
  mkdir -p /usr/local/share/gpturboprint/1.1.11/xml
  chmod -R 755 /usr/local/share/gpturboprint
  install --mode=644 "$TPPATH_FILTERS/gimpplugin/de.gmo" /usr/local/share/locale/de/LC_MESSAGES/gpturboprint.mo
  install --mode=644 "$TPPATH_FILTERS/gimpplugin/papers.xml" /usr/local/share/gpturboprint/1.1.11/xml
  install --mode=644 "$TPPATH_FILTERS/gimpplugin/printers.xml" /usr/local/share/gpturboprint/1.1.11/xml
fi

##
# install BJNP Canon Network Printer backend
##
if [ $TP_BJNP -eq 1 ] ; then
  if [ -e "$TPPATH_CUPSLIB/backend/bjnp" ] ; then
	BJRESULT=$("$TPPATH_CUPSLIB/backend/bjnp" -v 2>&1 | grep BJNP)
  fi
  if [ -e "$TPPATH_CUPSLIB64/backend/bjnp" ] ; then
	BJRESULT=$("$TPPATH_CUPSLIB64/backend/bjnp" -v 2>&1 | grep BJNP)
  fi
  if [ -n "$BJRESULT" ] ; then
	BJRESULT=${BJRESULT##*version}
	BJMAJOR=${BJRESULT%%.*}
	BJMINOR=${BJRESULT#*.}
	BJMAJOR=${BJMAJOR##*[^0-9]}
	BJMAJOR=${BJMAJOR%%[^0-9]*}
	BJMINOR=${BJMINOR%%[^0-9]*}
	BJMINOR=${BJMINOR##*[^0-9]}
	BJMAJOR=$[ $BJMAJOR + 0 ]
	BJMINOR=$[ $BJMINOR + 0 ]
	if [ $BJMAJOR -ge 1 ] ; then
		TP_BJNP=0
	fi
	#if [ $BJMAJOR -eq 0 -a $BJMINOR -ge 4 ] ; then
	#	TP_BJNP=0
	#fi

  fi
  if [ $TP_BJNP -eq 1 ] ; then
	echo "$TP_INSTALLING bjnp network backend for canon printers"
  	if [ -e "$TPPATH_CUPSLIB/backend" ] ; then
		tar -xpzf "$TPPATH_SHARE/gplsoftware/bjnpzedo.tar.gz" --no-same-owner -C "$TPPATH_CUPSLIB/backend" bjnp
	fi
  	if [ -e "$TPPATH_CUPSLIB64/backend" ] ; then
		tar -xpzf "$TPPATH_SHARE/gplsoftware/bjnpzedo.tar.gz" --no-same-owner -C "$TPPATH_CUPSLIB64/backend" bjnp
		#install --mode=755 "$TPPATH_FILTERS/bjnp" "$TPPATH_CUPSLIB64/backend"
	fi
  fi
fi

##
# install Zedonet version of Ghostscript
##
if [ $TP_GSZEDO -eq 1 ] ; then
	echo "$TP_INSTALLING Ghostscript version for TurboPrint"
	tar -xpzf "$TPPATH_SHARE/gplsoftware/gszedo.tar.gz" --no-same-owner -C "$TPPATH_BIN" gszedo
fi

##
# finished
##

echo "install-post finished $(date)" >> "$LOGFILE"
echo "#######################################################" >> "$LOGFILE"
##
# allow script 'install-post-user' access to logfile
##
chmod ugo+rw $LOGFILE

# additional installation steps that can't be executed from deb/rpm package
if [ $TP_RPM -eq 0 ] ; then
	"$TPPATH_FILTERS/install-post-rpm"
fi

# finished

exit 0
