#!/bin/sh
# CAP 6.0 LaserWriter interface for SGI IRIX 4.x.
# version 4b 
# Modelled after the IRIX lp printer interfaces.
# rks 6/13/94 (smith@cbrsgi.med.harvard.edu)
#
# Info for lp administration tools:
NAME="LaserWriter IIg"
TYPE=CAP

exec 2>> log

# Change the following to set up defaults (see the README file)
tmpdir=/usr/tmp
banner=0
verbose=1
logfile=log
imgland=1
txtland=0
wrap=1
fold=/usr/bin/fold
foldoptsp="-80"
foldoptsl="-110"
xmaxsize=8.11
ymaxsize=10.79
bitsperpixel=4
screendens=98
fltr1=/usr/lib/print/lptops 
fltr1opts="-U"
fltr2=/usr/sbin/tops
fltr2optsl="-m $ymaxsize $xmaxsize -b $bitsperpixel -l $screendens"
fltr2fixl="%!\n0 792 translate\n-90 rotate"
fltr2optsp="-m $xmaxsize $ymaxsize -b $bitsperpixel -l $screendens"
fltr2fixp=""
fltr3=/usr/sbin/imgcopy
fltr3opts="-fsgi"
send=/usr/local/cap/papif
sendopts="-P `basename $0`"

# Grab command line arguments
printer=`basename $0`
seqid=$1
name=$2
title="$3"
copies=$4
options="$5"
shift; shift; shift; shift; shift
files="$*"

if [ -z "$title" ] ; then
	title=`basename $1`
fi

if [ ! -w "$logfile" ] ; then
	disable -r"can't access log file $logfile" $printer 1>/dev/console 2>&1
	exit 1
fi

set -- $options
while [ 0 -lt $# ]
do
	case $1 in
		-verb* | verb*)  verbose=1;;
		-noverbose | noverbose) verbose=0;;
		-remove | remove) remcmd="$remcmd -remove";; 
		-erase | erase)  erase=$1;;
		-banner | banner) banner=1;;
		-nobanner | nobanner) banner=0;;
		-raw | raw) RAW=1;;
		-cooked | cooked) RAW=0;;
		-nowrap | nowrap) wrap=0;;
		-wrap | wrap) wrap=1; foldoptsl="-$2"; foldoptsp="-$2"; shift;;
		-port* | port*) txtland=0; imgland=0;;
		-H | -h | -land* | H | h | land*) txtland=1; imgland=1; fltr1opts="$fltr1opts -H";;
		*) fltr1opts="$fltr1opts $1";;
	esac
	shift
done

set -- $files
for r in $remcmd
do
	shift
	delfiles="$delfiles /usr/tmp/$1"
done

if [ $verbose != 0 ]; then
	echo $printer: $seqid $name "$title" Start - `date` >> $logfile
fi

if [ $txtland = 1 ]; then
	foldopts=$foldoptsl
else
	foldopts=$foldoptsp
fi

if [ $imgland = 1 ]; then
	fltr2opts=$fltr2optsl
	fltr2fix=$fltr2fixl		
else
	fltr2opts=$fltr2optsp
	fltr2fix=$fltr2fixp
fi

if [ $wrap = 0 ]; then
	fold=/bin/cat
	foldopts=""
fi

# Make the banner page
if [ $banner != 0 ]; then
	x="********************************************************************************"
	echo "$x\n$x\n$x\n$x\n" > $tmpdir/b$seqid
	/usr/bin/banner "$name" >>  $tmpdir/b$seqid
	echo "\n" >>  $tmpdir/b$seqid
	user=`grep "^$name:" /etc/passwd | line | cut -d: -f5`
	
	if [ "$user" = "" ]; then
		user=`(ypmatch $name passwd | line | cut -d: -f5) 2> /dev/null`
	fi
	
	if [ -n "$user" ]; then
		echo "User: $user\n" >>  $tmpdir/b$seqid
	else
		echo "\n" >>  $tmpdir/b$seqid
	fi
	
	if [ -n "$title" ]; then
		echo "Title: $title\n" >> $tmpdir/b$seqid
	fi
		
	echo "Request id: $seqid\n" >>  $tmpdir/b$seqid 
	echo "Printer: `basename $0`\n" >>  $tmpdir/b$seqid 
	date >> $tmpdir/b$seqid
	echo "\n" >>  $tmpdir/b$seqid
	/usr/bin/banner `hostname` >> $tmpdir/b$seqid
	echo "$x\n$x\n$x\n$x\n" >> $tmpdir/b$seqid
fi

# Filter and print the files.
i=1
while [ $i -le $copies ]
do
	# Print the banner page for each copy, using papif
	if [ $banner != 0 ]; then
		$fltr1 $tmpdir/b$seqid > $tmpdir/b$seqid.ps
		cat $tmpdir/b$seqid.ps | $send $sendopts
	fi
	
	# Set a trap to remove tmp files if user sends cancel command
	trap '
		if [ -f $tmpdir/b$seqid ]; then 				
			/bin/rm -f $tmpdir/b$seqid
		fi

		if [ -f $tmpdir/b$seqid.ps ]; then 				
			/bin/rm -f $tmpdir/b$seqid.ps
		fi

		if [ -f $tmpdir/$tmpfile.ps ]; then 				
			/bin/rm -f $tmpdir/$tmpfile.ps
		fi

		if [ -f $tmpdir/$tmpfile.rgb ]; then 				
			/bin/rm -f $tmpdir/$tmpfile.rgb
		fi
	exit' 1 2 15

	# See what kind of file it is; if it's not PostScript, filter it.
	for file in $files
	do
		tmpfile=`basename $file`
		magic1=`dd bs=1 count=2 if=$file 2> /dev/null`
		if [ "$magic1" = "%!" ]; then
			# It's PostScript
			cat $file > $tmpdir/$tmpfile.ps
		else
			magic2="`/bin/file $file`"
			case "$magic2" in
				*ascii* | *command* | *English* | *ext* | *script* )  
					# It's text
					$fold $foldopts $file | $fltr1 $fltr1opts > $tmpdir/$tmpfile.ps;;
				
				*)
					magic3=`/usr/sbin/imginfo $file | /bin/grep format | awk '{printf $3}'`
					# Check whether it's an image file
					case $magic3 in
						'SGI')
							# It's an RGB image
							echo $fltr2fix > $tmpdir/$tmpfile.ps
							$fltr2 $file $fltr2opts >> $tmpdir/$tmpfile.ps;;
							
						'TIFF' | 'FIT')
							# It's a TIFF or FIT image
							echo $fltr2fix > $tmpdir/$tmpfile.ps
							$fltr3 $fltr3opts $file $tmpdir/$tmpfile.rgb 
							$fltr2 $tmpdir/$tmpfile.rgb $fltr2opts >> $tmpdir/$tmpfile.ps;;
							
						*)
							echo "$printer: Unknown type of file(bad magic)  $file skipped..." >> $logfile
							echo "$printer: Unknown type of file(bad magic)  $file skipped..." >&2
							exit 1;;
					esac;;
			esac
		fi	
	
		# Send file to printer, using papif
		cat $tmpdir/$tmpfile.ps | $send $sendopts
		
		if [ -f $tmpdir/$tmpfile.rgb ]; then 				
			/bin/rm -f $tmpdir/$tmpfile.rgb
		fi

		if [ -f $tmpdir/$tmpfile.ps ]; then
			/bin/rm -f $tmpdir/$tmpfile.ps
		fi
		
	done
	i=`expr $i + 1`
done

if [ $verbose != 0 ]; then
	echo $printer: $seqid End - `date` >> $logfile
fi

if [ $banner != 0 ]; then
	/bin/rm -f $tmpdir/b$seqid
	/bin/rm -f $tmpdir/b$seqid.ps
fi

if [ -n "$erase" ]; then
	/bin/rm -f $files
fi

exit 0
