#	@(#)	1.3 2/25/86
# PC 6300 PLUS lp interface for HP LaserJet (HP2686A) printer 
# using the 92286A or 92286L font cartridge.
#	Male-to-Female DB25 NULL-MODEM cable required.
#
#
# Except in 'raw' mode, all output to the printer is filtered
#	to replace tabs with spaces, using tab stops at every eighth
#	character.
#
# Additionally, the following options have been added:
#	[-]raw[+]			Use RAW mode; default filter is 'cat'.
#					The '+' suffix suppresses job 
#					separation (TOF) both before and after.
#
#	[-]filtered
#	[-]cooked			Use filtered mode regardless of
#					whether logical printer was configured
#					as RAW.
#
#	portrait | -portrait		Select portrait mode
#	landscape | -landscape		Select landscape mode
#
#	courier | -courier		Select Courier font
#	lp | -lp			Select lp font
#
#	medium | -medium		Select medium font weight
#	bold | -bold			Select bold font weight
#	light | -light			Select light font weight
#
#	upright | -upright		Select upright font style
#	italic | -italic		Select italic font style
#
# Default: portrait, courier, medium, upright
#
# FILES:	/usr/lib/customfilter	(parallel)
#		/usr/lib/customfilterS	(serial)
#
#	If either of these files exist and are executable
# output will be filtered through it, instead of through 'pr'.
# This allows special handling of non-standard printers through
# user installable custom filters.
#

x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
printer=`basename $0`
copies=$4

# determine whether printer is RAW or filtered
case "$printer" in
	*R )
		print_mode=raw
		;;
	* )
		print_mode=filtered
esac

# process args passed from 'lp' (e.g. -oraw).
orient=
font=
weight=
style=
for i in $5
do
	case "$i" in
		-raw | raw )
			print_mode=raw
			;;
		-raw+ | raw+ )
			print_mode=raw+
			;;
		-filtered | filtered | -cooked | cooked )
			print_mode=filtered
			;;
	        -lp | lp) # Line printer font
	                font=-lp
	                ;;
	        -courier | courier) # Courier font
	                font=-courier
	                ;;
	        -medium | medium) # medium weight
	                weight=-medium
	                ;;
	        -bold | bold) # bold weight
	                weight=-bold
	                ;;
	        -light | light) # light weight
	                weight=-light
	                ;;
	        -landscape | landscape) # Landscape orientation
	                orient=-landscape
	                ;;
	        -portrait | portrait) # Portrait orientation
	                orient=-portrait
	                ;;
	        -upright | upright) # Font style
	                style=-upright
	                ;;
	        -italic | italic) # Font style
	                style=-italic
	                ;;
	esac
done

case "$printer" in
	*S | *SR )	# serial printer
		# LaserJet SW1 set as follows:
		#   1	2   3	4   5	6   7	8
		# OFF  ON OFF  ON OFF OFF OFF  ON
		case "$print_mode" in
			raw* )
			  stty -parity 9600 -hupcl -cstopb cread clocal loblk\
 		 	  ignbrk -brkint ignpar -inpck istrip ixon -ixany\
		 	  -opost\
 		 	  0<&1
				;;
			* )
			  stty -parity 9600 -hupcl -cstopb cread clocal loblk\
 		 	  ignbrk -brkint ignpar -inpck istrip ixon -ixany\
		 	  opost -olcuc onlcr -ocrnl -onocr -onlret -ofill\
		 	  cr0 nl0 tab3 bs0 vt0 ff0\
 		 	  0<&1
			  if [ -x /usr/lib/customfilterS ] ; then
				myownfilter=/usr/lib/customfilterS
			  fi
				;;
		esac
		;;
	* )	# parallel printer
		case "$print_mode" in
			raw* )
				;;
			* )
				if [ -x /usr/lib/customfilter ] ; then
					myownfilter=/usr/lib/customfilter
				fi
		esac
		;;
esac

if [ "$print_mode" != "raw+" ]
then
	echo "\0033E\c"
fi

if [ X${BANNER:+yes} = Xyes -a "$print_mode" != "raw+" ]
then
	echo "\0033&l0H\c"
	echo "$x\n$x\n$x\n$x\n"
	banner "$2"
	echo "\n"
	user=`grep "^$2:" /etc/passwd | line | cut -d: -f5`
	if [ -n "$user" ]
	then
		echo "User: $user\n"
	else
		echo "\n"
	fi
	echo "Request id: $1    Printer: $printer\n"
	date
	echo "\n"
	if [ -n "$3" ]
	then
		banner $3
	fi
	echo "Print Options:  \n"
	echo "          PrintMode=$print_mode\n"
	echo "          Copies="$copies"\n"
fi  
if [ "$print_mode" != "raw+" -a "$print_mode" != "raw" ] ; then
	
	echo "\033(8U"        # Roman-8 Symbol Set
	
	if [ X$orient = X-landscape ]
	then
	        echo "\033&l1O\c"
	else
	        echo "\033&l0O\c"
	fi
	
	if [ X$font = X-lp ]
	then
	        echo "\033(s0T\c"
	        echo "\033)s3T\c"
	        echo "\033(s16.6H\c"
	        echo "\033)s16.6H\c"
	        echo "\033(s8.5V\c"
	        echo "\033)s8.5V\c"
	else
		font=-courier
	        echo "\033(s3T\c"
	        echo "\033)s0T\c"
	fi
	
	if [ X$weight = X-bold ]
	then
	        echo "\033(s1B\c"
	        echo "\033)s0B\c"
	elif [ X$weight = X-light ]
	then
	        echo "\033(s-1B\c"
	        echo "\033)s0B\c"
	else
	        echo "\033(s0B\c"
	        echo "\033)s1B\c"
	fi
	
	if [ X$font = X-courier ]
	then
	  if [ X$style = X-italic ]
	  then
	        echo "\033(s1S\c"
	        echo "\033)s0S\c"
	  else
	        echo "\033(s0S\c"
	        echo "\033)s1S\c"
	  fi
	fi
	echo "\0033&l0H\c"
	echo "\0033&l66p2e7.6c66F\c"
fi

if [ "$print_mode" = "raw" ]
then
	echo "\0033&l0H\c"
fi

shift; shift; shift; shift; shift
files="$*"
i=1
while [ $i -le $copies ]
do
	for file in $files
	do
		case "$print_mode" in
			raw* )
				cat "$file" 2>&1
				;;
			* )
				if [ -n "$myownfilter" ] ; then
					cat "$file" | $myownfilter
				else
					pr -te8 "$file" 2>&1
				fi
		esac
		if [ "$print_mode" != "raw+" ] ; then
			echo "\0033&l0H\c"
			if [ "$print_mode" != "raw" ] ; then
				echo "\0033&l66p2e7.6c66F\c"
			fi
		fi
	done
	i=`expr $i + 1`
done
exit 0
