#!/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=x86_64
INSTALL_PATH="/usr/local/share/tscbarcode/printer"

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

echo
echo "    Warning...!"
echo "    <BarCode Printer Driver ($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=""

#
BACKEND_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"
		
                BACKEND_PATH_SEARCH="$BACKEND_PATH_SEARCH /usr/lib/cups/backend"
		
	;;
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"

BACKEND_PATH_SEARCH="$BACKEND_PATH_SEARCH /usr/lib/cups/backend"

################################################################################
#
# 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

BACKEND_PATH=""
for DIR in $BACKEND_PATH_SEARCH; do
	if test -d $DIR
	then
		BACKEND_PATH=$DIR
		break
	fi
done

FILTER_PROGRAMS="rastertobarcodetspl"


################################################################################
#
# echo informations
#
echo "    remove files......"

if test "x$MODEL_PATH" != "x"
then
	rm -rf $MODEL_PATH/tscbarcode/*.ppd
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
rm -rf $INSTALL_PATH/thermalprinterui
rm -rf $INSTALL_PATH/thermalprinterut
rm -rf $INSTALL_PATH/thermalprinterui.png

if test -f /usr/share/applications/barcodeprintersetting.desktop
then
  rm -rf /usr/share/applications/barcodeprintersetting.desktop
fi


rm -rf $BACKEND_PATH/tscusb
rm -rf $BACKEND_PATH/tscsocket
rm -rf $BACKEND_PATH/tscserial
rm -rf $BACKEND_PATH/tsclpd

rm -rf $INSTALL_PATH/tsc_test_11.prn
rm -rf $INSTALL_PATH/crontest
rm -rf $INSTALL_PATH/teststatus


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 driver completed"
echo

exit 0


