:
#	@(#) network 1.1 89/03/10 
#
#	Copyright (C) The Santa Cruz Operation, 1986,1987, 1988, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#
#!	Remote printing over UUCP, MICNET or XENIX-Net
#
#	Lp interface script for remote XENIX network printing.
#	This script can be used with UUCP, MICNET and XENIX-Net.
#
#	This script reads the file /usr/spool/lp/remote for lines
#	in the format:
#		name:	command
#	Where <name> is the name by which the printer is called locally,
#	and <command> is how lp(C) is to be run on the remote system.
#	Typically, the <command> would be either:
#		uux - lp -dprinter
#	or:
#		remote - lp -dprinter
#	Note that the System V spooler is assumed used on the remote machine.
#	In particular: 
#	(1)  All options (-o...) passed to this script can be
#		forwarded on in the same fashion, and
#	(2)  The option -ob will cause the remote machine to
#		suppress generating all banner pages.
#	(3)  The <command> reads from its standard input, and this
#		script can append on the additional flags described
#		above to the end of <command>.
#	Note that the IMAGEN laser printers generate their banner pages
#	using a different technique and that a special version of this
#	script is required.
#
#	Install this file as /usr/spool/lp/model/network.
#	Then, to define remote printer <name>, edit /usr/spool/lp/remote
#	as described above, and then, as superuser:
#	(1)  /usr/lib/lpshut
#	(2)  /usr/lib/lpadmin -pname -v/dev/null -mnetwork
#	(3)  /usr/lib/accept name
#	(4)  enable name
#	(5)  /usr/lib/lpsched
#	Remote printing on printer <name> should now be installed and
#	working.  Jobs can be sent to that printer by "lp -dname ...".

PATH=/bin:/usr/bin
mapping=/usr/spool/lp/remote

# get arguments
printer=`basename $0`
request=$1
name=$2
title=$3
copies=$4
options=$5
shift; shift; shift; shift; shift
files=$*

# set up options to pass to the remote printer spooler (as options for it)
banner=yes
lpflags=
for i in $options; do
	case $i in
	b)	banner=no ;;
	*)	lpflags="$lpflags -o$i" ;;
	esac
done

# map local printer name to machine and printer on that machine
[ -r $mapping ] || exit 2
set -- `grep -e "^$printer:" $mapping` || exit 3
shift
network=$*

# border around the banner
x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

(
	[ "$banner" = yes ] && {
		# 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`

		# 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

		# print the banner $nhead times
		while	[ "$nhead" -gt 0 ]
		do
			echo "$x\n"
			banner "$name"
			echo "$x\n"
			[ "$user" ] && echo "User: $user\n"
			echo "Request id: $request\n"
			echo "Printer: $printer\n"
			date
			echo "\nMachine: $sysid\n"
			[ "$title" ] && banner $title
			[ $nhead -ne 1 ] && echo "\f\c"
			nhead=`expr $nhead - 1`
		done
	}

	# send the file(s) to the standard out $copies times
	while	[ "$copies" -gt 0 ]
	do
		for file in $files
		do
			echo "\f\c"
			cat "$file" 2>&1
		done
		copies=`expr $copies - 1`
	done
) | $network -s -ob $lpflags
