#!/bin/sh
#
# REQUIRES: At least two arguments:
#	printername:	the installed name of the printer 
#			to send the print jobs to.
#	printjobs:	the full path to the files to print.
#
# MODIFIES: Tag of printer active icon file and the contents
#	of /var/spool/lp/logfile if VERBOSE=1.
#
# EFFECTS:
# This is a program which invokes routeprint to submit the jobs
# indicated on the command line.  It also updates the status bits
# on the printer interface file tagline, in order to provide better
# user feedback on the status of the printer.
#
# This program is intended to be used in conjunction with the Indigo
# Magic dsktop.
#
# COMMENTS:
#
# Printer type is a system-wide defined token, e.g., ColorPostScript.
# Valid printer tags range from 0x10200-0x102FF, 66048-66287.
# This program does not manipulate the base tags, only the status bits.
# Only tagit and the ftr rules care what the base tag #s are.
#

# Send errors to the spooler logfile, defined below
LOGFILE=/var/spool/lp/log

# Whether verbose messages get logged to logfile, true if VERBOSE > 0.
VERBOSE=0

# Where printers live:
#
PRINTERPATH=/var/spool/lp/interface

# Tagging program:
#
TAGPROGRAM=/usr/lib/print/tagprinter

#
# Some defines to make tagging easier:
#
# The range of valid printer tags:
#
MINTAG=66048		   # 0x10200
MAXTAG=66287		   # 0x102FF
#
# The state modifiers for those type tags:
#
IDLE=0			   # 0x0
BUSY=1			   # 0x1
FAULTED=2		   # 0x2
UNKNOWN=3		   # 0x3
UNAVAILABLE=3		   # 0x3


#########################################################################
#
#	CONVENIENCE FUNCTIONS
#
#########################################################################


TagPrinter()
#
# REQUIRES: two arguments: 	printername to tag, 
#			        desired printer state (idle or busy)
#
# MODIFIES: Active icon file if conditions are appropriate.
#
# RETURNS: 0 if successful, non-0 if any segment failed.
#
# EFFECTS:
#
# This routine attempts to mark the printer busy.
# It will only tag the printer as busy if it is currently idle.
# This ensures that a faulted or unavailable status is not overridden,
# and that a busy status is not needlessly rewritten.
#
# The routine will return with an error status if the printer is not tagged.  
# The printer will not be tagged if it is not already tagged.
#
{

	# Grab command line arguments
	#
	target=$1	 	# the file to tag
	targetstate=$2	 	# the desired state to tag the printer
	
	# Set up our destination status bits:
	#
	case $targetstate in
	  idle|Idle|IDLE)   DESTSTATUS=$IDLE	  ;;
	  busy|Busy|BUSY)   DESTSTATUS=$BUSY 	  ;;
	  *)
	     su lp -c "echo $myname: ERROR: Invalid destination state $targetstate requested. >> $LOGFILE"
	     return 1
	     ;;
	esac

	# OK, we've done all the setup, let's do the real work:
	#
	
	# ALGORITHM:
	#
	# First, grab the tag, checking to make sure it's valid.
	# Next, strip off the status bits (low-order bits).
	# If the status bits say the printer is IDLE and we're to tag BUSY,
	#   retag the printer as BUSY, and return w/exit code of tag command.
	# If the status bits say the printer is BUSY and we're to tag IDLE,
	#   retag the printer as IDLE, and return w/exit code of tag command.
	# If the status bits say any other status, do nothing and return.
	# If the status bits are invalid, report a warning and return.
	#
	# Done.
	#

	tag=`$TAGPROGRAM $target`

	if [ $VERBOSE != 0 ]; then
	   su lp -c "echo Found tag $tag on printer $target >> $LOGFILE"
	fi

	if [ "$tag" -lt "$MINTAG" -o "$tag" -ge "$MAXTAG" ] ;
	then
	  su lp -c "echo $myname: Supposed printer $target is not tagged as a valid printer. >> $LOGFILE"
	  return 1
	fi
	
	STATUS=`expr $tag % 4`
	
	TYPE=`expr $tag - $STATUS`
	
	case $STATUS in
	  $IDLE)      	 	 
	     # This is the only case in which we modify the target file.
	     # We mark it busy iff it is idle and we're to mark busy.
	     # 
	     if [ $DESTSTATUS = $BUSY ];
	     then
	          if [ $VERBOSE != 0 ];
		  then
		  	su lp -c "echo $myname: Tagging printer $target as BUSY. >> $LOGFILE"
		  fi
	     
	          su lp -c "$TAGPROGRAM $target `expr $TYPE + $BUSY`"
	
		  # Finally, return same status code as the tag command
		  #    
		  return $?
	     fi
	     ;;
	
	  $BUSY)
	     if [ $DESTSTATUS = $IDLE ];
	     then
	          if [ $VERBOSE != 0 ];
		  then
		  	su lp -c "echo $myname: Tagging printer $target as IDLE. >> $LOGFILE"
		  fi
	     
	          su lp -c "$TAGPROGRAM $target `expr $TYPE + $IDLE`"
	
		  # Finally, return same status code as the tag command
		  #    
		  return $?
	     fi
	     ;;	
	
	  # Do nothing to other status cases: we don't touch these valid types.
	  # 
	  $FAULTED|$UNKNOWN|$UNAVAILABLE) 	
	     if [ $VERBOSE != 0 ];
	     then
		su lp -c "echo $myname: Printer $target in faulted state, not tagging. >> $LOGFILE"
	     fi
	     ;;
	
	  # Echo an error if the type is invalid.
	  #
	  *)	
		if [ $VERBOSE -ne 0 ];
		then
			su lp -c "echo $myname: WARNING: Invalid status read from printer $target: $STATUS >> $LOGFILE"
		fi
		;;
	esac
	
	# If we get here, no error occurred and we didn't retag anything.
	# return successfully
	#
	return 0 
}


#
# Check command line arguments
#
myname=`basename $0`

# Check number of command line arguments
#
if [ $# -lt 2 ]   # if not proper number of arguments,
then     	  # exit with an error
   su lp -c "echo $myname: ERROR: No files specified in print request. >> $LOGFILE"
   su lp -c "echo $myname: Usage: $myname PrinterName FilesToPrint >> $LOGFILE"
   exit 1
fi

# Grab command line arguments
#
printername=$1	 	# the file to tag
shift 1			# get rid of first args
filestoprint="$*"       # grab rest of cmd line args

# Check existence of target printer
#
if [ ! -s $PRINTERPATH/$target ]	 # if file doesn't exist
then
    su lp -c "echo $myname: ERROR: Target printer $target does not exist! >> $LOGFILE"
    return 1
fi
	
# Mark the printer busy iff it is idle
#
TagPrinter $printername busy

# Check whether TagPrinter failed.
#
if [ $? -ne 0 -a $VERBOSE != 0 ];
then # something went wrong, report an error.
     su lp -c "echo $myname: Error while tagging printer. >> $LOGFILE"
fi	

# Submit the jobs as best we can
#
routeprint -g -p $printername $filestoprint

# Catch the exit status of routeprint: did it work?
#
exitstatus=$?

# If something failed, "unbusy" the printer and exit.
#
if [ $exitstatus -ne 0 ] ; 
then # something went wrong
	if [ $VERBOSE != 0 ];
	then # something went wrong, report an error.
		su lp -c "echo $myname: Error in routeprint, tagging printer idle. >> $LOGFILE"
	fi		

	TagPrinter $printername idle ; 

	# Check whether TagPrinter failed.
	#
	if [ $? -ne 0 -a $VERBOSE != 0 ];
	then # something went wrong, report an error.
		su lp -c "echo $myname: Error while tagging printer. >> $LOGFILE"
	fi	
fi     		     

exit $exitstatus

