#!/bin/sh
# @(#) $Revision: 70.1 $

# lp interface for hp2225a line printer
#
# Options recognized:
#
# Print Pitch Selection: (Default 12 cpi)
#	e | -e 		expanded print mode (6.0 cpi)
#	10 | -10 	expanded compressed print mode (10.7 cpi)
#	12 | -12	normal print mode (12.0 cpi)
#	c | -c		compressed print mode (21.3 cpi)
#
# Stroke Weight (Default Medium)
#	b | -b | bold	use bold stroke weight
#
# Output filtering: (Default Cooked)
#	r | -r | raw	use raw mode for binary output to printer
#
# Other:
#	nb | -nb	do not output banner page (to save paper)
#
# Default: normal print mode
#

# IMPORTANT  NOTICE:  this printer model has been  designed to handle both
# text (cooked) and raw output.  Under normal  circumstances, the standard
# output  is  redirected  to the  device  specified  by the -v  option  of
# lpadmin.  The   /usr/spool/lp/member   directory  contains  files  which
# describe  the  device  file  locations.  The raw  device  file  name  is
# computed from the cooked device file name by adding a leading "r" to the
# device  file name of the cooked  device.  IT IS ASSUMED  that the device
# file names differs only by a leading r.

# redefine stderr:
log=/usr/spool/lp/log
exec 2>>$log

# Save the arguments to the model

printer=`basename $0`
reqid=$1
user=$2
title=$3
copies=$4

# Test for the raw printer device file

devicefile=/usr/spool/lp/member/`basename $0`
cookeddevice=`cat $devicefile | line`
rawdevice=`dirname $cookeddevice`/r`basename $cookeddevice`

if [ ! -c $rawdevice ]
then
	disable -r"can't locate raw device file $rawdevice" $printer
	exit 1
fi

# Handle disable and cancel traps.

trap "echo 'Terminated: $reqid'  >> $log; trap 15; kill -15 0; exit 0 " 15

# Determine which options have been invoked

pitch="p3"
bold="no"
outputmode="cooked"
banner="yes"

for i in $5
do
	case "$i" in
	-e | e) # expanded print (6.0 cpi)
		pitch="p1";;
	-10 | 10) # expanded compressed print (10.7 cpi)
		pitch="p2";;
	-12 | 12) # normal print (12.0 cpi)
		pitch="p3";;
	-c | c) # compressed print (21.3 cpi)
		pitch="p4";;

	-b | b | bold) # bold
		bold="yes";;

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

	-nb | nb) # Do not print banner page
		banner="";;
	esac
done

# Assume that the rest of the arguments are files

shift; shift; shift; shift; shift
files="$*"

# Print the standard header

if [ -n "$banner" ]
then
	x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
	echo "\014\r\c"
	echo "$x\n$x\n$x\n$x\n"
	banner `echo $user | tr [a-z] [A-Z]`
	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: $printer\n"
	date
	echo "\n"
	if [ -n "$title" ]
	then
		banner "$title" 
	fi
	echo "\014\r\c"
fi

# Print the spooled files

i=1
while [ $i -le $copies ]
do
	for file in $files
	do
		case "$pitch" in
			p1)	echo "\033&k1S\r\c";;
			p2)	echo "\033&k3S\r\c";;
			p3)	echo "\033&k0S\r\c";;
			p4)	echo "\033&k2S\r\c";;
		esac

		case "$bold" in
			no)	echo "\033(s0B\033)s0B\r\c";;
			yes)	echo "\033(s1B\033)s1B\r\c";;
		esac

		case $outputmode in
			raw)		cat "$file" >$rawdevice 2>/tmp/raw$$
					cat /tmp/raw$$
					rm /tmp/raw$$
					echo "\014\r\c";;
			cooked)		cat "$file" 2>&1
					echo "\014\r\c";;
		esac
#		echo "\033E\r\c"	# hard reset -- between spool files
		echo "\033&k0S\r\c"	# reset pitches
		echo "\033(s0B\033)s0B\r\c"	# reset stroke weights
		echo "\033&d@\r\c"	# disable auto-underline
		echo "\033&l6D\r\c"	# reset printer to 6 lines per inch
	done
	i=`expr $i + 1`
done

exit 0
