#!/bin/bash
#
# Turboprint deinstallation script
#
# must have root permissions
#
# (c) 2000-2011 ZEDOnet GmbH

# get dirs


TP_BATCH=0
TP_NOGUI=0

for i do
        case "$i" in
        --batch )
        {
                echo "STARTED"
                TP_BATCH=1
        };;
        --nogui ) TP_NOGUI=1 ;;
        --user=* ) TP_USER="${i#*=}" ;;
        esac
done

if [ ! -e /etc/turboprint/system.cfg ] ; then
	echo "Configuration file not found!"
	echo "(etc/turboprint/system.cfg)"
	echo ""
	echo "TurboPrint not installed?"
	exit 1
fi

if [ $TP_BATCH -eq 0 -a $TP_NOGUI -eq 0 ] ; then
    if [ ! -z $DISPLAY ] ; then
        bin/turboprint --uninstall
        if [ $? -eq 0 ] ; then
                exit 0
        else
                echo "Could not start X-Windows installation tool."
                echo "make sure that X-Windows is running and accessible."
                echo ""
        fi
    fi
fi

eval $(cat /etc/turboprint/system.cfg)

echo
echo "Turboprint deinstallation"
echo "========================="
echo "Will delete the following directories:"
echo "$TPPATH_CONFIG"
echo "$TPPATH_SHARE"
echo "$TPPATH_FILTERS"
echo "$TPPATH_LOG/turboprint"
echo "$TPPATH_VAR/turboprint"
echo
echo "And the following files:"
echo "$TPPATH_BIN/tpprint"
echo "$TPPATH_BIN/tpsetup"
echo "$TPPATH_BIN/tpconfig"
echo "$TPPATH_BIN/turboprint"
echo "$TPPATH_MAN/man1/tpprint.1"
echo "$TPPATH_MAN/man1/tpsetup.1"
echo "$TPPATH_MAN/man1/tpconfig.1"
echo "$TPPATH_MAN/man7/tpfilter.7"
echo "$TPPATH_MAN/man7/turboprint.7"
if [ -e "$TPPATH_CUPSDRIVER/turboprint" ] ; then
	echo "$TPPATH_CUPSDRIVER/turboprint"
fi
echo

if [ $TP_BATCH -eq 0 ] ; then

	echo -n "Start deinstallation now? "
	read ANSWER

	if [ -z $ANSWER ] ; then
		exit 0
	fi
	if [ $ANSWER != "y" ] ; then
		exit 0
	fi
fi

# call script to remove dynamic data (also called by rpm %preun)

echo "Removing TurboPrint files..."
echo "PROGRESS 10"

lib/uninstall-pre --user="$TP_USER"

# call script to remove static files
echo "PROGRESS 50"

lib/uninstall-static --tgz

echo "Deinstallation complete"
echo "PROGRESS 100"
echo "RESULT 0"
exit 0
