#!/bin/sh
# $Source: /misc/source_product/9.10/commands.rcs/usr/bin/lp/model/hp33440a,v $
# $Revision: 70.1 $

# Printer Command Language level 4 model

#=======================================================================#
# OPTIONS RECOGNIZED: ( all may be preceded by a "-" )			#
#									#
# Horizontal Pitch Selection						#
#	10		10 characters/inch (cpi)			#
#	12		12 cpi						#
#	c		compressed print pitch				#
#	e		expanded print pitch				#
#	fp#		set primary font pitch to # cpi			#
#	sfp#		set secondary font pitch to # cpi		#
#			    ( # may be floating point )			#
#			    ( e.g. 16.66 = compressed )			#
#	hsi#		set horizontal spacing increment (#/120's inch)	#
#			    ( e.g. hsi10 is the same as 12 cpi)		#
#			    ( See your printer reference manual)	#
#									#
# Print Quality Selection						#
#	q#		set primary print quality to #			#
#	sq#		set secondary print quality to #		#
#			    ( possible values for quality # )		#
#			    ( 0 = draft, 1 = near letter quality )	#
#			    ( 2 = letter quality )			#
#	nlq		set primary print quality to near letter quality#
#	lq		set primary print quality to letter quality	#
#	dq		set primary print quality to draft quality	#
#									#
# Print Spacing Selection						#
#	ps		set primary font to proportional spacing	#
#	sps		set secondary font to proportional spacing	#
#	fs		set primary font to fixed spacing		#
#	sfs		set secondary font to fixed spacing		#
#									#
# Vertical Pitch Selection						#
#	lpi#		print at # lines per inch			#
#			    ( default is 6 lpi)				#
#	vsi#		set vertical spacing increment to (#/48's in)	#
#			    (  e.g. vsi4 is the same as 12 lpi )	#
#			    ( See your printer reference manual)	#
#									#
# Font Selection							#
#	cs#$		set primary character set to #$			#
#	scs#$		set secondary character set to #$		#
#			    ( See your printer reference manual )	#
#			    ( for possible character sets. )		#
#			    ( e.g. cs8U sets Roman 8 character set )	#
#	r8		Roman 8 character set				#
#	k8		Kana 8 character set				#
#	style#		set primary font style to #			#
#	sstyle		set secondary font style to #			#
#			    ( possible values for style # )		#
#			    ( 0 = upright, 1 = italic, 2 = slant )	#
#	i | italics	set primary font style to italic		#
#	si | sitalics	set secondary font style to italic		#
#	upright		set primary font style to upright		#
#	supright	set secondary font style to upright		#
#	slant		set primary font style to slant			#
#	sslant		set secondary font style to slant		#
#	type#		set primary font typeface to #			#
#	stype#		set secondary font typeface to #		#
#			    ( possible values for typeface # )		#
#			    ( 0 = line printer, 1 = Pica, 2 = Elite )	#
#			    ( 3 = Courier, 4 = Helv, 5 = Tms Rmn    )	#
#			    ( 6 = Gothic, 7 = Script, 8 = Prestige  )	#
#			    ( 9 = Caslon, 10 = Orator		    )	#
#	weight#		set primary font stroke weight to #		#
#	sweight#	set secondary font stroke weight to #		#
#			    ( <0 = light, 0 = medium, >0 = bold )	#
#			    ( typical range is -7 to +7		)	#
#			    ( consult printer reference manual	)	#
#	b | bold	set font stroke weight to bold ( # = 1 )	#
#									#
# Page Length Selection							#
#	lnnn		set absolute length of page to nnn lines	#
#			    ( default is 66 )				#
#	tlnnn		set text length of page to nnn lines		#
#			    ( default is 60 in portrait mode, )		#
#			    ( 45 in landscape mode )			#
#									#
# Margin Selection							#
#	lm#		set left margin in column #			#
#	rm#		set right margin in column #			#
#			    ( left most column = 0 )			#
#									#
# Page Orientation: (Default portrait)					#
#	portrait	portrait mode orientation			#
#	landscape	landscape mode orientation			#
#									#
# Output filtering: (Default Cooked)					#
#	r  | raw	raw mode for plotting mode etc.			#
#	n  | nroff	set up to properly print output from nroff	#
#	pr		set up to properly print output from pr		#
#       nb		do not output banner page (to save paper)	#
#	rev		output pages in reverse order			#
#									#
# Paper Control: (Default single sided feed from paper cassette)	#
#	m | man		set up for manual sheet feed			#
#	d | double	do automatic double sided printing		#
#		* If your printer is not capable of duplex printing,	#
#		  use 'md' or 'mdouble' option.				#
#	md | mdouble	do manual double sided printing			#
#									#
# Dividing page: (Default normal)					#
#	half | 2	double page with half size printing		#
#	quarter | 4	four times page with quarter size printing	#
#		* Use of these options will negate the effects of	#
#		  the other options except for 'portrait', 'landscape',	#
#		  'm', 'man', 'nb', 'd' or 'double'.			#
# 									#
#=======================================================================#


# USER CAUTION: This model is linked to several other models and should
# not be modified directly. If any changes are desired, including the
# enhancements described below, you should first make a copy of this
# model under a new name. Changes can then be made to the copy and the
# copy used as the model for the desired printer.

# POSSIBLE USER ENHANCEMENT:  This model does not output pages in reverse
# order by default. To make reversing the default, insert a '#'  before
# the first line below and remove the leading '#' from the second line.
# Note that the "rev" option can be used to reverse pages of individual
# jobs. Be sure to read the above USER CAUTION before modifying this model. 

reverse=""		# Use this line if page reversing IS NOT desired
#reverse="yes"		# Use this line if page reversing IS desired

# POSSIBLE USER ENHANCEMENT: By default the laserjet printer will
# print 60 lines of text per page. It is possible to get 66 lines of
# text on the printable portion of the page when using the letter size
# (8.5 by 11 inches) paper cartridge or 70 lines per page when using
# the A4 size (297 by 210 mm) paper cartridge. The instructions for
# modifying this model to get either of these capabilities by default
# are given below under the heading "POSSIBLE USER ENHANCEMENT".
# NOTE: Use of the 'vsi', 'lpi', 'pr', 'nroff', 'l', 'orientation'
# 'half' or 'quarter' options will negate the effects of this enhancement.
# Be sure to read the above USER CAUTION before modifying this model. 


PATH="/bin:/usr/bin:/usr/lib"
export PATH

# set up redirection of stderr
log=/usr/spool/lp/log
exec 2>>$log

if [ -z "$LANG" ]
then
	LANG="C"
	export LANG
fi

# Save the arguments to the model
printer=`basename $0`
reqid=$1
user=$2
title=$3
copies=$4


# Definitions of functions used within this script

do_banner()
{
	# Print the standard header
	x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
	echo "\r\c"
	echo "$x\n$x\n$x\n$x\n"
	banner `echo $user`
	echo "\n"
	user=`pwget -n $user | line | cut -d: -f5`
	if [ -n "$user" ]
	then
		echo "User: $user\n"
	else
		echo "\n"
	fi
	echo "Request id: $reqid    Printer: `basename $0`\n"
	date
	echo "\n"
	if [ -n "$title" ]
	then
		banner "$title" 
	fi
	echo "\f\c"
}


# Set up interface
if [ -t 1 ]
then
	stty raw 9600 -parenb cs8 ixon -istrip clocal <&1 2>/dev/null
else
	slp -r 2>/dev/null
fi

# Handle disable and cancel traps.
trap "echo 'Terminated: $reqid'  >> $log; trap 15; kill -15 0; exit 0 " 15

# Set up printer default modes
echo "\033E\c"		# Reset Printer
echo "\033)0B\c"	# Secondary character set line draw
echo "\033&k2G\c"	# Set line termination mode.


# Initialize option variables to default values
length="def"
pitch="def"
spitch="def"
hsi="def"
quality="def"
squality="def"
spacing="def"
sspacing="def"
lpi="def"
vsi="def"
charset="def"
scharset="def"
style="def"
sstyle="def"
typeface="def"
stypeface="def"
weight="def"
sweight="def"
pagelen="def"
tpagelen="def"
lmargin="def"
rmargin="def"
outputmode="cooked"
banner="yes"
orientation="def"
pagemode="def"
nroff=""
italics=""
dividing=""
duplex=""


# Determine which options have been invoked

for i in $5
do
	case "$i" in

	-10 | 10) 	# font pitch set to 10.00 cpi
		pitch="10";;

	-12 | 12) 	# font pitch set to 12.00 cpi
		pitch="12";;

	-c | c) 	# font pitch set to compressed print 
		pitch="c";;

	-e | e) 	# font pitch set to expanded print
		pitch="e";;

			# set primary font pitch to value following fp
	-fp[0-9]* | fp[0-9]* | -fp.[0-9]* | fp.[0-9]*) 
		pitch="`echo "$i" | sed 's/^[-]*fp//'`";;

			# set secondary font pitch to value following sfp
	-sfp[0-9]* | sfp[0-9]* | -sfp.[0-9]* | sfp.[0-9]*) 
		spitch="`echo "$i" | sed 's/^[-]*sfp//'`";;

			# set horizontal spacing increment to #/120 in.
	-hsi[0-9]* | hsi[0-9]*)
		hsi="`echo "$i" | sed 's/^[-]*hsi//'`";;

			# set primary font quality to 0,1, or 2
	-q[0-2] | q[0-2])
		quality="`echo "$i" | sed 's/^[-]*q//'`";;

			# set secondary font quality to 0,1, or 2
	-sq[0-2] | sq[0-2])
		squality="`echo "$i" | sed 's/^[-]*sq//'`";;

	-nlq | nlq)		# set font quality to near letter quality
		quality=1
		squality=1;;

	-lq | lq)	# set font quality to letter quality
		quality=2
		squality=2;;

	-dq | dq)	# set font quality to draft quality
		quality=0
		squality=0;;

	-ps | ps)	# set primary font to proportional spacing
		spacing=prop;;

	-sps | sps)	# set secondary font to proportional spacing
		sspacing=prop;;

	-fs | fs)	# set primary font to fixed spacing
		spacing=fixed;;

	-sfs | sfs)	# set  secondary font to fixed spacing
		sspacing=fixed;;

			# set lines per inch to value following lpi
	-lpi[0-9]* | lpi[0-9]*)	
		lpi="`echo "$i" | sed 's/^[-]*lpi//'`";;	

			# set vertical spacing increment to #/48 inch
	-vsi[0-9]* | vsi[0-9]*)
		vsi="`echo "$i" | sed 's/^[-]*vsi//'`";;

			# set primary character set to value following cs
	-cs[0-9]* | cs[0-9]*)
		charset="`echo "$i" | sed 's/^[-]*cs//'`";;

			# set secondary character set to value following scs
	-scs[0-9]* | scs[0-9]*)
		scharset="`echo "$i" | sed 's/^[-]*scs//'`";;

	-r8 | r8) 	# primary character set is Roman8 
		charset="8U";;

	-k8 | k8) 	# primary character set is Kana8 
		charset="8K";;

			# set font style accordingly
	-style[0-2] | style[0-2])
		style="`echo "$i" | sed 's/^[-]*style//'`";;

	-sstyle[0-2] | sstyle[0-2])
		sstyle="`echo "$i" | sed 's/^[-]*sstyle//'`";;

	-i | i | -italics | italics)
		style=italic;;

	-si | si | -sitalics | sitalics)
		sstyle=italic;;

	-upright | upright)
		style=upright;;

	-supright | supright)
		sstyle=upright;;

	-slant | slant)
		style=slant;;

	-sslant | sslant)
		sstyle=slant;;

			# set primary typeface to value after type
	-type[0-9]* | type[0-9]*)
		typeface="`echo "$i" | sed 's/^[-]*type//'`";;
	
			# set secondary typeface to value after stype
	-stype[0-9]* | stype[0-9]*)
		stypeface="`echo "$i" | sed 's/^[-]*stype//'`";;

			# set primary font weight to +- #
	-weight[0-9]* | weight[0-9]* | -weight-[0-9]* | weight-[0-9]*)
		weight="`echo "$i" | sed 's/^[-]*weight//'`";;

			# set secondary font weight to +- #
	-sweight[0-9]* | sweight[0-9]* | -sweight-[0-9]* | sweight-[0-9]*)
		sweight="`echo "$i" | sed 's/^[-]*sweight//'`";;

			# set font weight to bold
	-b | b | -bold | bold)
		weight=1
		sweight=1;;

			#set absolute length of page to the value following l
	-l[0-9]* | l[0-9]*) 
		pagelen="`echo "$i" | sed 's/^[-]*l//'`";;

			#set text length of page to the value following tl
	-tl[0-9]* | tl[0-9]*) 	
		tpagelen="`echo "$i" | sed 's/^[-]*tl//'`";;

			# set left margin in column given by value after lm
	-lm[0-9]* | lm[0-9]*)	
		lmargin="`echo "$i" | sed 's/^[-]*lm//'`";;

			# set right margin in column given by value after rm
	-rm[0-9]* | rm[0-9]*)	
		rmargin="`echo "$i" | sed 's/^[-]*rm//'`";;

			# raw mode for binary output to printer
	-r | r | -raw | raw) 	
		outputmode="raw";;

	nb | -nb) 	# Do not output banner page
		banner="";;

	-rev | rev)		# output pages in reverse order
		reverse="yes";;

	-portrait | portrait) # select portrait orientation
		orientation="portrait";;

	-landscape | landscape) # select landscape orientation
		orientation="landscape";;

	-d | d | -double | double)	# set up for automatic double sided copy
		duplex="yes";;

	-md | md | -mdouble | mdouble)	# set up for manual double sided copy
		outputmode="mdouble";;

	-m | m | -man | man) # set up printer for manual sheet feed
		pagemode="man";;

			# set up printer to properly print output from nroff	
	-n | n | -nroff | nroff) 
		nroff="-n"
		length="-l66";;
			
	-pr | pr)	# set up printer to properly print output from pr
		nroff="-p"
		length="";;

			# dividing page, double page with half size printing
	-half | half | -2 | 2)
		dividing="-h";;

			# dividing page, four times page with quarter size
	-quarter | quarter | -4 | 4)
		dividing="-q";;

	esac
done

# Assume that the rest of the arguments are files
shift; shift; shift; shift; shift
files="$*"

# print the banner if no banner not specified and reversing is not specified
if [ -n "$banner" -a -z "$reverse" ]
then
	do_banner
	echo "\033E\c"		# Reset Printer
	echo "\033)0B\c"	# Secondary character set line draw
	echo "\033&k2G\c"	# Set line termination mode.
fi


# If raw mode and RS232 interface used turn off output processing
if [ "$outputmode" = "raw" -a -t 1 ]
then
	stty raw 9600 -opost -parenb cs8 ixon -istrip clocal tab0 <&1 2>/dev/null
	echo "\033&k0G\c"	# Reset line termination mode
fi


# download font if dividing is "half" or "quarter"
if [ -n "$dividing" ]
then
	reverse=""
	pitch="def"
	spitch="def"
	hsi="def"
	quality="def"
	squality="def"
	spacing="def"
	sspacing="def"
	lpi="def"
	vsi="def"
	charset="def"
	scharset="def"
	style="def"
	sstyle="def"
	typeface="def"
	stypeface="def"
	weight="def"
	sweight="def"
	pagelen="def"
	tpagelen="def"
	lmargin="def"
	rmargin="def"
	nroff=""
	italics=""

	reverse=""
	length="def"
	outputmode="divide"

	case "$orientation" in
		portrait | def)
			orientation="portrait"
			case "$dividing" in
				-h);;
				-q)	fontdl /usr/spool/lp/fonts/lp4p;;
			esac;;
		landscape)
			case "$dividing" in
				-h)	fontdl /usr/spool/lp/fonts/lp2l;;
				-q)	fontdl -l /usr/spool/lp/fonts/lp4l;;
			esac;;
	esac
fi


# Print the spooled files
i=1
while [ $i -le $copies ]
do
	for file in $files
	do
		if [ -n "$duplex" ]
		then
			echo "\033&l1S\c"
		fi

		case "$orientation" in
			def);;
			portrait)	echo "\033&l0O\c";;
			landscape)	echo "\033&l1O\c"
					length="-l45";;
		esac

		case "$vsi" in
			def);;
			*)	echo "\033&l${vsi}C\c";;
		esac

		case "$lpi" in
			def);;
			*)	echo "\033&l${lpi}D\c";;
		esac

		case "$pagelen" in
			def)	if [ "$length" = "def" ]
				then
					length="-l60"
				fi
#
# POSSIBLE LASERJET ENHANCEMENT:
# To set the default output on the laserjet printer to be 66 lines
# per page and 80 characters per line with a 4 character left
# margin and a 2 line top margin remove the leading '#' from the
# following 7 lines.
#				if [ "$vsi" = "def" -a "$lpi" = "def" -a "$orientation" != "landscape" -a "$nroff" = "" -a "$dividing" = "" ]
#				then
#					echo "\033&l66p2e7.6c66F\c"
#					echo "\033&a4L\c"
#					echo "\033&k11H\c"
#					length="-l66"
#				fi
# If you are using A4 size paper (297 x 210 mm) and desire 70 lines per
# page instead of 66 along with the other characteristics described
# above replace the three occurrences of '66' in the above lines with
# '70' and remove the leading '#' as described. 
				;;
			*)	echo "\033&l${pagelen}P\c"
				length="-l$pagelen";;
		esac

		case "$tpagelen" in
			def);;
			*)	echo "\033&l${tpagelen}F\c"
				length="-l$tpagelen";;
		esac

		case "$pitch" in 
			def);;
			c)	echo "\033&k2S\c";;
			e)	echo "\033&k1S\c";;
			*)	echo "\033(s${pitch}H\c";;
		esac

		case "$spitch" in 
			def);;
			*)	echo "\033)s${spitch}H\c";;
		esac

		case "$hsi" in
			def);;
			*)	echo "\033&k${hsi}H\c";;
		esac

		case "$quality" in
			def);;
			*)	echo "\033(s${quality}Q\c";;
		esac

		case "$squality" in
			def);;
			*)	echo "\033)s${squality}Q\c";;
		esac

		case "$spacing" in
			def);;
			prop)	echo "\033(s1P\c";;
			fixed)	echo "\033(s0P\c";;
		esac
		
		case "$sspacing" in
			def);;
			prop)	echo "\033)s1P\c";;
			fixed)	echo "\033)s0P\c";;
		esac
		
		case "$charset" in
			def);;
			*)	echo "\033(${charset}\c";;
		esac

		case "$scharset" in
			def);;
			*)	echo "\033)${scharset}\c";;
		esac

		case "$style" in
			def);;
			italic) echo "\033(s1S\c"
				italics="-i";;
			slant)	echo "\033(s2S\c";;
			*)	echo "\033(s${style}S\c";;
		esac
		
		case "$sstyle" in
			def);;
			italic) echo "\033)s1S\c";;
			slant)	echo "\033)s2S\c";;
			*)	echo "\033)s${sstyle}S\c";;
		esac

		case "$typeface" in
			def);;
			*)	echo "\033(s${typeface}T\c";;
		esac

		case "$stypeface" in
			def);;
			*)	echo "\033)s${stypeface}T\c";;
		esac
		
		case "$weight" in
			def);;
			*)	echo "\033(s${weight}B\c";;
		esac

		case "$sweight" in
			def);;
			*)	echo "\033)s${sweight}B\c";;
		esac

		case "$lmargin" in
			def);;
			*)	echo "\033&a${lmargin}L\c";;
		esac
	
		case "$rmargin" in
			def);;
			*)	echo "\033&a${rmargin}M\c";;
		esac

		case "$pagemode" in
			def);;
			man)	echo "\033&l2H\c";;
		esac

		if [ -n "$reverse" ]
		then
		    case "$outputmode" in
			    raw)    cat "$file" 2>/tmp/sh$$
				    if [ -s /tmp/sh$$ ]
				    then
				        cat /tmp/sh$$	# output any errors
				    fi
				    rm /tmp/sh$$;;
			    cooked) echo "\r\c"
				    lprpp $italics $length $nroff < "$file" 2>&1 | reverse $length;;
			    mdouble) lprpp $italics $length $nroff -o < "$file" 2>&1 | reverse $length
				    echo "\033&l2H\c"
				    lprpp $italics $length $nroff -e < "$file" 2>&1;;
		    esac
		else
		    case "$outputmode" in
			    raw)    cat "$file" 2>/tmp/sh$$
				    if [ -s /tmp/sh$$ ]
				    then
				        cat /tmp/sh$$	# output any errors
				    fi
				    rm /tmp/sh$$;;
			    cooked) echo "\r\c"
				    lprpp $italics $length $nroff < "$file" 2>&1;;
			    mdouble) lprpp $italics $length $nroff -o < "$file" 2>&1
				    echo "\033&l2H\c"
				    lprpp $italics $length $nroff -e < "$file" 2>&1;;
			    divide) case "$orientation" in
				    portrait)
					 divpage -p "$dividing" "$file" 2>&1;;
				    landscape)
					 divpage -l "$dividing" "$file" 2>&1;;
				    esac;;
		    esac
		fi
		echo "\033E\c"		# Reset Printer
		echo "\033)0B\c"	# Secondary character set line draw
		echo "\033&k2G\c"	# Set line termination mode.
	done
	i=`expr $i + 1`
done

if [ -n "$banner" -a -n "$reverse" ]
then
	do_banner
fi

# RS-232 interface insure all buffers are flushed to printer
if [ -t 1 ]
then
	stty raw 9600 -parenb cs8 ixon -istrip clocal <&1 2>/dev/null
fi

exit 0
