#!/usr/bin/sh
# @(#) $Revision: 72.2 $

# lp interface for the hp2276 deskjet printer
#
# Options recognized:

# Print Pitch Selection: (Default 10 cpi)
#	10 | -10	pica print mode (10.00 cpi)
#	12 | -12	elite expanded print mode (12.00 cpi)
#	c  | -c		compressed print mode (16.36 cpi)

# Output filtering: (Default Cooked)
#	r  | raw	raw mode for plotting mode etc.
#	lnnn		set page length to nnn (only in cooked mode)

#
# Other:
#       nb		do not output banner page (to save paper)
#
# Default: normal print mode
# 

# Definitions of functions used within this script

do_banner()
{
	echo "\033(8U\033(s0p10h12v0s0b3T\c"

	# Print the standard header
	x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
	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: `basename $0`\n"
	date
	echo "\n"
	banner "$title" 
	echo "\f\c"
}

# POSSIBLE  ENHANCEMENTS:  This model reverses pages by default
# To turn reversing off, remove the '#' before the below
# line and insert a '#' from the line after it.

#reverse=""     # Use this line if you don't want page reversing
reverse="yes" # Use this line if you do    want page reversing

# POSSIBLE  ENHANCEMENTS:  This model  assumes  that the letter  size (8.5
# inches by 11  inches)  paper  cartridge  is used with 66 lines per page.
# The A4 size (210 millimeters by 297 millimeters)  paper cartridge may be
# used with 70 lines per page.  This is done by finding  those lines which
# indicate "70" lines,  uncommenting  them, and commenting out the line or
# lines which are used for "66" lines.

# POSSIBLE  UPGRADE:  This model  provides some options  which may only be
# supported with certain printer hardware  options.  There may be hardware
# upgrades  available to support these options.  If these options are used
# without the required  hardware, the result should be acceptable  even if
# not exactly what was expected.

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

# redefine stderr:
log=/var/adm/lp/log
exec 2>>$log

printer=`basename $0`

# Set up RS-232 interface if this is a tty port
if tty -s <&1
then
	stty 9600 opost onlcr -parenb cs8 ixon -istrip clocal tab3 <&1
fi

# Set printer for number copies requested
reqid=$1
user=$2
title=$3
copies=$4
echo "\033&l1X\c"

# 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.

# Determine which options have been invoked

pitch=""
font="default"
outputmode="cooked"
pagelen=""
banner="yes"

for i in $5
do
	case "$i" in
	-10 | 10) # normal print (10.00 cpi)
		pitch="p2";;
	-12 | 12) # normal print (12.00 cpi)
		pitch="p3";;
	-c | c) # compressed print (16.36 cpi)
		pitch="p4";;

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

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

	l[0-9]*) #set the page length to nnn
		pagelen="-$i";;
	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, turn off output processing and set for no tab expansion.

case $outputmode in
#	Set up RS-232 interface if this is a tty port
	raw) if tty -s <&1
then
		stty raw 9600 -parenb cs8 ixon -istrip clocal tab0 <&1
fi;;
esac
# Print the spooled files
i=1
while [ $i -le $copies ]
do
	for file in $files
	do
		case "$font" in 
			none);;

			default)echo "\033&l68p2e7.48c66F\c"  # Set 66 lines/page
				echo "\033&a0R\c"	     # 10.9 chars/inch
				echo "\033&a4L\c"	     # .4" Left Margin
				echo "\033&k11H\c"	     # 10.9 chars/inch
				if [ -z "$pagelen" ]
				then
					pagelen=-l66
#					pagelen=-l70
				fi;;
		esac

		case "$pitch" in
			p2)	echo "\033(s10H\033)s10H\c";;
			p3)	echo "\033(s12H\033)s12H\c";;
			p4)	echo "\033(s16.7H\033)s16.7H\c";
		esac

		if [ -n "$reverse" ]
		then
		    case $outputmode in
			    raw)    cat $file 2>&1;;
			    cooked) lprpp $pagelen <$file 2>&1 |
					    reverse $pagelen;;
		    esac
		else
		    case $outputmode in
			    raw)    cat $file 2>&1;;
			    cooked) lprpp $pagelen <$file 2>&1;;
		    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

# Set up RS-232 interface if this is a tty port.
# This causes a delay so all the data is printed.
if tty -s <&1
then
	stty raw 9600 -parenb cs8 ixon -istrip clocal <&1
fi

exit 0
