#!/bin/bash 

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

################################################################################
# Initial
################################################################################
BUILD_CPU=`uname -m`

#raster filter path
INSTALL_PATH="/usr/local/share/godex/printer"
#cups usb-quirks path
QUIRKS_PATH="/usr/share/cups/usb/org.cups.usb-quirks"
#Printer VID PID
VID_PID="0x195f 0x0001"

FILTER_PATH=""
FILTER_PATH_SEARCH=""

MODEL_PATH=""
MODEL_PATH_SEARCH=""

################################################################################
# start
################################################################################
echo
echo "	start <GoDEX Printer Driver on ($BUILD_CPU)> install......"
echo
echo "[Checking]"

#Default will be used ppd path
MODEL_PATH_SEARCH="$MODEL_PATH_SEARCH /usr/share/cups/model"
MODEL_PATH_SEARCH="$MODEL_PATH_SEARCH /usr/local/share/cups/model"

#Default will be used filter path
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"

if [ $BUILD_CPU == "x86_64" ]; then
	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"
fi
echo "."

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

if [ $FILTER_PATH == "" ] || [ $MODEL_PATH == "" ]; then
	echo "  Cannot found CUPS filter or model path"
	exit
fi

FILTER_PROGRAMS="filters/$BUILD_CPU/rastertoezpl"

echo ".."

################################################################################
# check and execute uninstall shell script
################################################################################
if test -f $FILTER_PATH/rastertoezpl; then
	echo "	execute uninstall shell script now......"
	if !($INSTALL_PATH/uninstall-driver); then
		echo "	Uninstall old <BP Printer Driver> Failed"
		echo "	Install Driver Failed"
		echo
		exit 1
  	fi
fi
echo "..."

################################################################################
# Installing
################################################################################
echo "[Installing]"

################################################################################
# set own, grp and permissions
################################################################################
chmod 644 ./ppd/*.ppd
for FILTER in $FILTER_PROGRAMS; do
	chmod 755 ./$FILTER
done
echo "."

################################################################################
# make install dir
################################################################################
mkdir -p $MODEL_PATH/GoDEX/
chown -R root:root $MODEL_PATH/GoDEX/
chmod -R 755 $MODEL_PATH/GoDEX/

mkdir -p $INSTALL_PATH/
chmod -R 755 $INSTALL_PATH/
echo ".."

################################################################################
# copy files
################################################################################
for FILTER in $FILTER_PROGRAMS; do
	cp ./$FILTER $FILTER_PATH/
done
cp ./ppd/*.ppd $MODEL_PATH/GoDEX/
cp ./uninstall-driver $INSTALL_PATH/

echo "..."

################################################################################
# Set cups usb-quirks file
################################################################################
echo "[Setting usb-quirks file]"

# Find VID from CUPS USB Quirks File
DataInFile=`sudo grep "$VID_PID" "$QUIRKS_PATH"` 

# WriteFile
#echo "Result :"
case $DataInFile in
        $VID_PID*)
		echo
                echo "	$VID_PID Found"
             	echo
        ;;
        *)
		echo "	$VID_PID Not Found"
             	echo
                echo "	Write VID PID Data"
                echo >> $QUIRKS_PATH
                echo "	# GoDEX Printer:"
                echo "# GoDEX Printer:" >> $QUIRKS_PATH
                echo "	$VID_PID unidir no-reattach soft-reset"
                echo "$VID_PID unidir no-reattach soft-reset" >> $QUIRKS_PATH
        ;;
esac

echo "	CUPS USB Quirks File End"
echo
echo "[Finish....]"

















