#!/bin/sh
#This shell for printer Hotplug .
#Author Zero 06-05-31
echo "Begin hotplug shell">/tmp/logForPrinter

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/hddapp/sbin/
log=/tmp/logForPrinter
PrinterName=""
Product=""
PPD=""
cd /sys/bus/usb/drivers/usblp
sleep 2

tempDir=$(ls -l | grep "device" | awk '{print $11}')
cd $tempDir
echo "tempDir=$tempDir" >> $log
cd ..
PrinterInfoDir=$(pwd)
echo "PrinterInfoDir=$PrinterInfoDir" >> $log
PrinterFactory=$(cat manufacturer)

#For UI read the Manufacture
/bin/touch /tmp/Manufacture 
if [ ! -z "$PrinterFactory" ]; then
    echo "$PrinterFactory" > /tmp/Manufacture
else
    exit 1;
fi

echo "$(cat manufacturer)" >> $log
echo "PrinterFactory=$PrinterFactory" >> $log
Product=$(cat product)
PrinterName="usb-printer"

#For UI read the Model
/bin/touch /tmp/Model
echo "$Product" > /tmp/Model
echo "Product=$Product" >> $log
echo "PrinterName=$PrinterName" >> $log

DriverURI=$(lpinfo -v | grep "direct usb" | sed -e 's;direct ;;g')

#To set the destnation for host pc ,if don't set the host can't print
lpadmin -d $PrinterName
#set the printer queue
lpadmin -p $PrinterName -E -v $DriverURI

/etc/rc.d/S80cupsd.sh stop
/etc/rc.d/S80cupsd.sh start
echo "lpstat -p" >> $log
#For UI read the Status	
/bin/touch /tmp/Status
lpstat -p > /tmp/Status

/bin/touch /etc/hotplug/usb/$PrinterName.rmv     
RMV_SHELL=/etc/hotplug/usb/$PrinterName.rmv
echo "#!/bin/sh" > $RMV_SHELL
echo "/usr/hddapp/sbin/lpadmin -x $PrinterName >/dev/null 2>&1" >> $RMV_SHELL
echo "rm -f /tmp/Manufacture >/dev/null 2>&1"  >> $RMV_SHELL
echo "rm -f /tmp/Model >/dev/null 2>&1" >> $RMV_SHELL
echo "rm -f /tmp/Status>/dev/null 2>&1" >> $RMV_SHELL
ln -s $RMV_SHELL $REMOVER  2>/dev/null
chmod a+x $RMV_SHELL $REMOVER



