#!/bin/bash
#
# batch file to uninstall printer driver
#

################################################################################
#
# Make sure only root can run our script
#
if [ "$(id -u)" != "0" ]; then
   echo "    Uninstall MUST be run as root" 1>&2
   exit 1
fi

#BUILD_CPU=i686
BUILD_CPU=`uname -m`
INSTALL_PATH="/usr/local/share/godex/printer"

################################################################################
#
# echo informations
#

echo
echo "	Warning...!"
echo "	<GoDEX Printer Driver on ($BUILD_CPU)> uninstall"
echo
echo "	Please use <printconf-gui> or <printconf-tui> to remove all printers"
echo "	based on the driver and then close the program!"
echo -n "	Input 'y' to continue:"
read inputval
if test "$inputval" != "y"
then
  echo "	Uninstall be canceled"
  echo
  exit 1
fi

################################################################################
#
# check cup
#
FILTER_PATH_SEARCH=""
MODEL_PATH_SEARCH=""

MODEL_PATH_SEARCH="$MODEL_PATH_SEARCH /usr/share/cups/model"
MODEL_PATH_SEARCH="$MODEL_PATH_SEARCH /usr/local/share/cups/model"

case $TARGET_CPU in
	x86_64)
		FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/lib64/cups/filter"
		FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/libexec64/cups/filter"
		FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/local/lib64/cups/filter"
		FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/local/libexec64/cups/filter"

		MODEL_PATH_SEARCH="$MODEL_PATH_SEARCH /usr/share/ppd"
	;;
esac

FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/lib/cups/filter"
FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/libexec/cups/filter"
FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/local/lib/cups/filter"
FILTER_PATH_SEARCH="$FILTER_PATH_SEARCH /usr/local/libexec/cups/filter"

################################################################################
# Set cups usb-quirks file
################################################################################
echo "[Setting usb-quirks file]"
#cups usb-quirks path
QUIRKS_PATH="/usr/share/cups/usb/org.cups.usb-quirks"
#Printer VID PID
VID_PID="0x195f 0x0001"

# Find GoDEX String from CUPS USB Quirks File
GoDEXName="# GoDEX Printer:"
GoDEXInFile=`sudo grep "$GoDEXName" "$QUIRKS_PATH"` 
case $GoDEXInFile in
        $GoDEXName*)
		echo
                echo "	$GoDEXName Found"
             	echo
             	echo "	Delete GoDEX Data"
             	sed -i '/# GoDEX Printer:/d' $QUIRKS_PATH
             	sed -i '/0x195f 0x0001/d' $QUIRKS_PATH
        ;;
        *)
		echo "	$GoDEXName Not Found"
             	echo
        ;;
esac

################################################################################
#
# find install dir
#
FILTER_PATH=""
for DIR in $FILTER_PATH_SEARCH; do
	if test -d $DIR
	then
		FILTER_PATH=$DIR
		break
	fi
done
MODEL_PATH=""
for DIR in $MODEL_PATH_SEARCH; do
	if test -d $DIR
	then
		MODEL_PATH=$DIR
		break
	fi
done

FILTER_PROGRAMS="rastertoezpl"


################################################################################
#
# echo informations
#
echo "	Remove files......"

if test "x$MODEL_PATH" != "x"
then
	rm -rf $MODEL_PATH/GoDEX/
fi
if test "x$FILTER_PATH" != "x"
then
	for FILTER in $FILTER_PROGRAMS; do
		rm -rf $FILTER_PATH/$FILTER
	done
fi
rm -rf $INSTALL_PATH/uninstall-driver

echo "	Restart spooler - CUPS"
################################################################################
#
# restart 
#
if test -f /etc/init.d/cups
then
	/etc/init.d/cups restart
else
	if test -f /etc/init.d/cupsys
  	then
		/etc/init.d/cupsys restart
	fi
fi

################################################################################
#
# echo informations
#

echo "	Uninstall GoDEX driver completed"
echo

exit 0


