:
#	@(#) postscript 23.3 91/10/07 
#
#	Copyright (C) 1989-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#	lp interface for postscript printer

#several print modes are available
#raw:	will print out a raw postscript image, if you have a "postscript"
#	file then use this option
#port:	will translate a text file to a postscript image and print it
#	on a postscript printer in portrait mode
#land:	will translate a text file to a postscript image and print it
#	on a postscript printer in landscape mode

#stty exta clocal hupcl cs8 cread icanon ixon icrnl 0<&1
#stty exta -opost onlcr tab3 ixon -echo ixany -ixoff -parity -hupcl 0<&1
#stty 9600 clocal hupcl cs8 cread icanon ixon ixoff icrnl 0<&1
stty ixon ixoff 0<&1

printer=`basename $0`
request=$1
name=$2
title=$3
copies=$4
options=$5


#Set up some global variables.
: ${SPOOLDIR:=/usr/spool/lp}
: ${LOCALPATH:=${SPOOLDIR}/bin}

#Set up the default filter.
if [  -x "${LOCALPATH}/lp.cat" ]
then
	LPCAT="${LOCALPATH}/lp.cat 0"
else
	LPCAT="cat"
fi

#If we are not using a filter, use the default one.
if [ -z "${FILTER}" ]
then
	FILTER="${LPCAT}"
fi

if [ -x "${LOCALPATH}/drain.output" ]
then
	DRAIN="${LOCALPATH}/drain.output 1"
else
	DRAIN=
fi

#find the printmode
banner=yes
MODE=port
RAW="off"
if [ -n "$5" ];then
	for i in $5
	do
		case "$i" in
        	raw | RAW | g | G )  RAW="on"
		;;
        	land | LAND | l | L )  MODE=land
		;;
        	land2 | LAND2 | land2)  MODE=land2
		;;
        	port | PORT | port)  MODE=port
		;;
		b) banner=no
		;;
        	esac
	done
fi
export MODE

filter=/usr/spool/lp/bin/text2post

echo "\004\c"						# M003

if [ $RAW = "on" ];then
		
	[ "$banner" = yes ] && {			# M000
		# nhead gets the value of BANNERS or 1 by default
		nhead=`sed -n 's/^BANNERS=//p' /etc/default/lpd`
		[ "$nhead" -ge 0 -a "$nhead" -le 5 ] || nhead=1

		[ "$nhead" -gt 0 ] && {
			# get the local system id
			if test -r /etc/systemid; then
				sysid=`sed 1q /etc/systemid`
			else
				sysid=`uname -n`
			fi
		
			# user = fifth field of /etc/passwd
			user=`sed -n "s/^$name:.*:.*:.*:\(.*\):.*:.*$/\1/p" /etc/passwd`
			# todays date
			tdate=`date`
			
		while [ "$nhead" -gt 0 ]; do
				
			echo "%!"
			echo "/i{72 mul}def"
			echo "/m{moveto}def"
			echo "/s{show}def"
			echo "/o{true charpath stroke}def"
			echo "/rct{moveto dup neg 3 1 roll 0 exch rlineto"
			echo "  0 rlineto 0 exch rlineto closepath}def"
			echo "6.5 i 1.1 i 0.95 i 8.9 i rct 0.0 setgray stroke"
			echo "6.5 i 1.1 i 0.95 i 8.9 i rct 0.8 setgray fill"
			echo "/Helvetica-Bold findfont 45 scalefont setfont"
			echo "1.0 setgray ($name) 1.0 i 9.5 i m s"
			echo "0.0 setgray ($name) 1.0 i 9.5 i m o"
			echo "/Helvetica findfont 20 scalefont setfont"
			echo "($user) 1.0 i 9.0 i m s"
			echo "(Request id: $request) 1.0 i 8.5 i m s"
			echo "(Copies: $copies) 1.0 i 8.0 i m s"
			echo "(Options: $options) 1.0 i 7.5 i m s"
			echo "(Machine: $sysid) 1.0 i 7.0 i m s"
			echo "(Date: $tdate) 1.0 i 6.5 i m s"
			[ "$title" ] && {
				echo "(Title: $title) 1.0 i 6.0 i m s"
			}
			echo "showpage"
			
			nhead=`expr $nhead -1`
		done  
		}
	}		
	shift; shift; shift; shift; shift;
	files="$*"

	if [ $copies ];then
		numcopies=$copies
	else
		numcopies=1
	fi
	i=1
	while [ $i -le $numcopies ]
	do
		for file in $files
		do
			0<${file} eval ${FILTER} 2>&1
		#	echo "\014"            	        # M002
		done
		i=`expr $i + 1`
	done 
	
else
	
		[ "$banner" = yes ] && {		# M000	
	
		# nhead gets the value of BANNERS or 1 by default
		nhead=`sed -n 's/^BANNERS=//p' /etc/default/lpd`
		[ "$nhead" -ge 0 -a "$nhead" -le 5 ] || nhead=1

		[ "$nhead" -gt 0 ] && {
			# get the local system id
			if test -r /etc/systemid; then
				sysid=`sed 1q /etc/systemid`
			else
				sysid=`uname -n`
			fi
		
			# user = fifth field of /etc/passwd
			user=`sed -n "s/^$name:.*:.*:.*:\(.*\):.*:.*$/\1/p" /etc/passwd`
			# todays date
			tdate=`date`
			
		while [ "$nhead" -gt 0 ]; do
				
			echo "%!"
			echo "/i{72 mul}def"
			echo "/m{moveto}def"
			echo "/s{show}def"
			echo "/o{true charpath stroke}def"
			echo "/rct{moveto dup neg 3 1 roll 0 exch rlineto"
			echo "  0 rlineto 0 exch rlineto closepath}def"
			echo "6.5 i 1.1 i 0.95 i 8.9 i rct 0.0 setgray stroke"
			echo "6.5 i 1.1 i 0.95 i 8.9 i rct 0.8 setgray fill"
			echo "/Helvetica-Bold findfont 45 scalefont setfont"
			echo "1.0 setgray ($name) 1.0 i 9.5 i m s"
			echo "0.0 setgray ($name) 1.0 i 9.5 i m o"
			echo "/Helvetica findfont 20 scalefont setfont"
			echo "($user) 1.0 i 9.0 i m s"
			echo "(Request id: $request) 1.0 i 8.5 i m s"
			echo "(Copies: $copies) 1.0 i 8.0 i m s"
			echo "(Options: $options) 1.0 i 7.5 i m s"
			echo "(Machine: $sysid) 1.0 i 7.0 i m s"
			echo "(Date: $tdate) 1.0 i 6.5 i m s"
			[ "$title" ] && {
				echo "(Title: $title) 1.0 i 6.0 i m s"
			}
			echo "showpage"
			
			nhead=`expr $nhead - 1`
			done 
		}
	}
			shift; shift; shift; shift; shift
			files="$*"
	(
			if [ $copies ];then
				numcopies=$copies
			else
				numcopies=1
			fi
			i=1
			FF=no
			while [ $i -le $numcopies ]
			do
				for file in $files
				 do
		#this junk is to keep an extra
		#page from being printed with multiple files
					if [ "$FF" = "no" ]	# M001 begin
					then
						FF=yes
					else
						echo "\014"
					fi			# M001 end
						0<${file} eval ${FILTER} 2>&1
				done
				i=`expr $i + 1`
			done 
	) | $filter 
fi

echo "\004\c"							# M003
#Draining characters might be necessary.
${DRAIN}

exit 0
