#!/bin/sh 
#
# This script is intended to be used to print to a windows host using
# SAMBA. It has being tested using LPRNG as the printing mechanism (as
# that's what I use).
#
# If the windows host is not available, a mail is sent warning the user
# who submitted the job that the printer is not working correctly. The
# script tries several times to see if the host is up before sending 
# the job over to the host.
#
# Written by Paul Cochrane (paul@tortc.tuht.scot.nhs.uk)  29/8/2002
# This script is very loosely based on the original smbprint script.
# Free to all for use, modification, distribution and general criticism
# but I would like a mention if you base your script on this script.
#
#
# An example printcap entry is as such:
#
# pr_paul|Paul's Laserjet 4+:\
#        :mx#0:sh:sd=/var/spool/lpd/pr_paul:lp=/dev/null:\
#        if=/usr/local/samba/bin/smbprint.wait gillian hplj4
#
# The 2 parameters in the if entry are the name of the windows machine
# and the name of the printer share.
#
#
# Variables:
#---------------------------------------------------------------------
# This value is what is added to the sending user name for the email
# notification of the host being down.

maildomainname="tortc.tuht.scot.nhs.uk"

# These values determine how many times we check for the host being 
# available and how long to pause between each check (e.g. to allow 
# for machine to boot. The default values (10 & 6) means each time
# this script is executed, the print job will run for around 10 mins.
# My lpd server retries each job 3 times by default so the entire
# job should be queued for around 30 mins if the host is unavailable.

maxRetries=10
pausefor=60

# The output of the filter is written to this file
logfile=/tmp/smb-print.log

# Path to smbclient
smbclient=/usr/local/samba/bin/smbclient

# Printing to an XP host: XP needs a username and password to access
# the shared printers. These two variables are the username and 
# password that's passed to the machine when requesting a print job.
# This could be a security issue on your network so create a user 
# with no privileges apart from being able to logon & print.
printuser=printuser
printuserpassword=printnow

# This variable simple turns on/off the displaying of the parameters
# that lpd passes to the filter
debug=0

# Initialise the log file
#------------------------
echo ""									>>$logfile
echo "-- Job Started -- $0 -------"					>>$logfile
echo " - Print server $1, Printer $2   `date`" 				>>$logfile
if [ $debug -ne 0 ]
then
    echo "Param 3: $3"							>>$logfile
    echo "Param 4: $4"							>>$logfile
    echo "Param 5: $5"							>>$logfile
    echo "Param 6: $6"							>>$logfile
    echo "Param 7: $7"							>>$logfile
    echo "Param 8: $8"							>>$logfile
    echo "Param 9: $9"							>>$logfile
    echo "Param 10: $10"						>>$logfile
    echo "Param 11: $11"						>>$logfile
    echo "Param 12: $12"						>>$logfile
    echo "Param 13: $13"						>>$logfile
fi

# Get the email address of the job sender
#----------------------------------------
if [ `echo $3 | cut -b1-2` = "-A" ]
then
    mailaddress="`echo $3 | cut -f1 -d@ | cut -b3-`@$maildomainname"
    echo " - Submitted by $mailaddress"					>>$logfile
    sendWarning=1
else
    echo " - Local machine submission - probably a retry by lpd..."	>>$logfile
    sendWarning=0
fi

# Store the print job in a temp file
#-----------------------------------
tmpFile=/tmp/$1.$2.`date`
cat >$tmpFile

# Ensure that host is listening for the print job
#------------------------------------------------
echo " - Checking host $1 is awake and listening..." 			>>$logfile
hostawake=-1
retry=0
while [ $hostawake -ne 0 ]
do
  $smbclient -L $1 -N >/dev/null 2>&1
  if [ $? -eq 0 ] 
  then
	echo "     $1 seems to be alert and ready."			>>$logfile 
	hostawake=0
  else	
	retry=`expr $retry + 1`
  	if [ $retry -eq $maxRetries ] 
	then
	   echo "   $1 is not awake - print job may be lost. Exiting"	>>$logfile
	   rm "$tmpFile"
	   echo "-- Job Finished ------------------------------------------------------">>$logfile
	   exit 1
  	fi
	if [ $retry -eq 2 ]
	then
	    if [ $sendWarning -eq 1 ] 
	    then
	   	echo "      Mailing the user notifying of the printing problem."	>>$logfile
	   	echo "The document you asked to print cannot be printed. The chances are that the machine $1 is turned off or has crashed. Please check that the machine is turned on and functional. This job should print when the machine becomes available." | /bin/mail -s "Printer Unavailable - $1" $mailaddress
	    fi
	fi
	echo "   Attempt $retry: $1 seems to be unavailable - waiting for $pausefor seconds...">>$logfile
     	sleep $pausefor
  fi
done

# Print out the document
#-----------------------
hostOS=`$smbclient -L $1 -N | grep Server= | cut -d[ -f3 | cut -d] -f1`
if [ "$hostOS" = "Windows 5.1" ]
then
    echo " - Printing the document....XP machine"				>>$logfile
    $smbclient "//$1/$2" $printuserpassword -c "print -" -U$printuser -P	>>$logfile <"$tmpFile"
else
    echo " - Printing the document...."						>>$logfile
    $smbclient "//$1/$2" -c "print -" -N -P 					>>$logfile <"$tmpFile"
fi
rm "$tmpFile"
echo "-- Job Finished ------------------------------------------------------">>$logfile

exit 0
