#!/bin/sh

echo
echo "Installing Posiflex CUPS Driver Files"
echo

if [ "`whoami`" != "root" ]
then
    echo "This script requires root privilege."
	echo
    exit 1
fi

if [ ! -e /dev/ttyACM0 ]
then
	echo "NOTE: If PP8000U with USB interface is used, "
	echo "please power on the printer and connect it to the system"
	echo "then continue the installation."
fi

echo
echo "Press Q and Enter to quit, or just an Enter to continue..."
read _ans
case $_ans in
	q|Q) echo "about..."
	exit
esac

if [ ! -z $DESTDIR ]
then
    echo "DESTDIR set to $DESTDIR"
    echo ""
fi

#echo " --- $DESTDIR --- "

SERVERROOT=$(grep '^ServerRoot' /etc/cups/cupsd.conf | awk '{print $2}')

if [ -z $FILTERDIR ] || [ -z $PPDDIR ]
then

    if [ -z $FILTERDIR ]
    then
        SERVERBIN=$(grep '^ServerBin' /etc/cups/cupsd.conf | awk '{print $2}')

        if [ -z $SERVERBIN ]
        then
            FILTERDIR=usr/lib/cups/filter
        elif [ ${SERVERBIN:0:1} = "/" ]
        then
            FILTERDIR=$SERVERBIN/filter
        else
            FILTERDIR=$SERVERROOT/$SERVERBIN/filter
        fi
    fi

    echo ""

    if [ -z $PPDDIR ]
    then
        DATADIR=$(grep '^DataDir' /etc/cups/cupsd.conf | awk '{print $2}')

        if [ -z $DATADIR ]
        then
            PPDDIR=usr/share/cups/model
        elif [ ${DATADIR:0:1} = "/" ]
        then
            PPDDIR=$DATADIR/model
        else
            PPDDIR=$SERVERROOT/$DATADIR/model
        fi
    fi
fi


if [ -z $1 ]
then
	echo "Copy Posiflex CUPS filter file to $DESTDIR/$FILTERDIR"
	mkdir -p $DESTDIR/$FILTERDIR
	chmod +x rastertopp7
	cp rastertopp7 $DESTDIR/$FILTERDIR
fi


if [ -z $1 ]
then
	echo "Copy Posiflex PPD file to $DESTDIR/$PPDDIR"
	mkdir -p $DESTDIR/$PPDDIR
	cp pp7.ppd $DESTDIR/$PPDDIR
	#mkdir -p $DESTDIR/$PPDDIR/Custom
	#cp pp7.ppd $DESTDIR/$PPDDIR/Custom
	echo
fi

if [ -e /dev/ttyACM0 -a -e /etc/init.d/rc.local ]
then
	if [ -z "`tail /etc/init.d/rc.local|grep "ttyACM0"`" ]
	then
		#stty -F/dev/ttyS0 38400 crtscts
		stty -F/dev/ttyACM0 115200 crtscts -parenb -cstopb cs8
		chmod 666 /dev/ttyACM0

		echo if [ -e /dev/ttyACM0 ] >> /etc/init.d/rc.local
		echo then >> /etc/init.d/rc.local
		#echo stty -F/dev/ttyS0 38400 crtscts >> /etc/init.d/rc.local
		echo stty -F/dev/ttyACM0 115200 crtscts >> /etc/init.d/rc.local
		echo chmod 666 /dev/ttyACM0  >> /etc/init.d/rc.local
		echo fi >> /etc/init.d/rc.local
	fi
fi

if [ -e /dev/ttyACM0 -a -e /etc/rc3.d/S99local ]
then
	if [ -z "`tail /etc/rc3.d/S99local|grep "ttyACM0"`" ]
	then
		#stty -F/dev/ttyS0 38400 crtscts
		stty -F/dev/ttyACM0 115200 crtscts -parenb -cstopb cs8
		chmod 666 /dev/ttyACM0

		echo if [ -e /dev/ttyACM0 ] >> /etc/rc3.d/S99local
		echo then >> /etc/rc3.d/S99local
		#echo stty -F/dev/ttyS0 38400 crtscts >> /etc/rc3.d/S99local
		echo stty -F/dev/ttyACM0 115200 crtscts >> /etc/rc3.d/S99local
		echo chmod 666 /dev/ttyACM0  >> /etc/rc3.d/S99local
		echo fi >> /etc/rc3.d/S99local
	fi
fi

if [ -e /dev/ttyACM0 -a -e /etc/rc3.d/S99rc.local ]
then
	if [ -z "`tail /etc/rc3.d/S99rc.local|grep "ttyACM0"`" ]
	then
		#stty -F/dev/ttyS0 38400 crtscts
		stty -F/dev/ttyACM0 115200 crtscts -parenb -cstopb cs8
		chmod 666 /dev/ttyACM0

		echo if [ -e /dev/ttyACM0 ] >> /etc/rc3.d/S99rc.local
		echo then >> /etc/rc3.d/S99rc.local
		#echo stty -F/dev/ttyS0 38400 crtscts >> /etc/rc3.d/S99rc.local
		echo stty -F/dev/ttyACM0 115200 crtscts >> /etc/rc3.d/S99rc.local
		echo chmod 666 /dev/ttyACM0  >> /etc/rc3.d/S99rc.local
		echo fi >> /etc/rc3.d/S99rc.local
	fi
fi

if [ -z $RPMBUILD ]
then
    if [ -x /sbin/restart ]
    then
		sudo /sbin/restart cups
    elif [ -x /etc/init.d/cups ]
    then
        /etc/init.d/cups stop
        /etc/init.d/cups start
    elif [ -x /etc/rc.d/init.d/cups ]
    then
        /etc/rc.d/init.d/cups stop
        /etc/rc.d/init.d/cups start
    elif [ -x /etc/software/init.d/cups ]
    then
        /etc/software/init.d/cups stop
        /etc/software/init.d/cups start
    elif [ -x /sbin/init.d/cups ]
    then
        /sbin/init.d/cups stop
        /sbin/init.d/cups start
    elif [ -x /etc/software/init.d/cupsys ]
    then
        /etc/software/init.d/cupsys stop
        /etc/software/init.d/cupsys start
    elif [ -x /etc/rc.d/init.d/cupsys ]
    then
        /etc/rc.d/init.d/cupsys stop
        /etc/rc.d/init.d/cupsys start
    elif [ -x /etc/init.d/cupsys ]
    then
        /etc/init.d/cupsys stop
        /etc/init.d/cupsys start
    elif [ -x /sbin/init.d/cupsys ]
    then
        /sbin/init.d/cupsys stop
        /sbin/init.d/cupsys start
    else
        echo "Could not restart CUPS"
		echo
		exit 1
    fi
    echo ""
fi

echo "File installation is finished, please go to the address "
echo "http://localhost:631 with a browser to set up the printer."

if [ ! -e /dev/ttyACM0 ]
then
	echo
	echo "NOTE: If PP8000U with USB interface is used, "
	echo "please power on the printer and connect it to the system"
	echo "then run installpp again."
fi

echo

