#!/bin/sh 
#
# lp interface for generic PostScript printers, serial or parallel
#
# $Revision: 1.16 $
# $Date: 1995/12/06 23:29:42 $
#
# Info for administration tools:
#
# NAME=Generic PostScript (Face Down Pages),DEVICE=SERIAL, OPTIONS=FACEUP=0
# NAME=Generic PostScript (Face Down Pages),DEVICE=CENTRONICS, OPTIONS=FACEUP=0
# NAME=Generic PostScript (Face Up Pages),DEVICE=SERIAL,OPTIONS=FACEUP=1
# NAME=Generic PostScript (Face Up Pages),DEVICE=CENTRONICS,OPTIONS=FACEUP=1
#
NAME=
TYPE=PostScript

# set locale for internationalization
LANG=C
export LANG

exec 2>> log

# Change the following to set up defaults
STTYPARAMS="-ixany ixon ixoff"
LOCALSTTY="-echo"
BAUDRATE=9600
GTMPDIR=/var/tmp
BANNER=1
# Whether pages come out of the printer face up or face down.
FACEUP=0

# If pages come out face down, don't reverse page order for input text.
if [ $FACEUP = 0 ]
then
	opts="$opts -U"
fi

LOGFILE=log
RAW=0
send=/usr/lib/print/lptops
VERBOSE=1

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


for j in $options
do      case $j in
        -verbose | verbose | -debug | debug)  VERBOSE=1;;
	-noverbose | noverbose) VERBOSE=0;;
	-banner | banner) BANNER=1;;
	-nobanner | nobanner | -h | h) BANNER=0;;
	-raw | raw)  	RAW=1;;
	-cooked | cooked) RAW=0;;
        *)      opts="$opts $j";;
        esac
done

#
# Initialize port parameters
#
if [ -t 1 ]
then
	stty sane $BAUDRATE $LOCALSTTY <&1
	if [ "$STTYPARAMS" != "" ]
	then
		stty $STTYPARAMS <&1
	fi
	if [ "$RAW" != 0 ]
	then
		stty -opost <&1
	fi
fi

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

SetTrap()
#
# This routine sets up the proper trap command.
# When a job is cancelled, the spooler sends us a SIGTERM.
# We could also get a SIGHUP or a SIGINT, so we'll catch those.
#
# It removes temporary/intermediate files created while filtering.
{
        trap '/bin/rm -f $GTMPDIR/b$seqid # CleanTempFiles
              exit' 1 2 15      # catch SIGHUP, SIGINT, and SIGTERM
}

SetTrap			# Set the cancellation trap


# Change this for prettier banners or different number of columns
x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
y="<=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=>"
echo "$seqid" >> $LOGFILE
if [ $BANNER != 0 ]
then
	touch $GTMPDIR/b$seqid
	echo "$x\n$x\n$x\n$x\n" >> $GTMPDIR/b$seqid
	/usr/bin/banner "$name" >>  $GTMPDIR/b$seqid
	echo "\n" >>  $GTMPDIR/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" >>  $GTMPDIR/b$seqid
	else
		echo "\n" >>  $GTMPDIR/b$seqid
	fi
	echo "Request id: $seqid    Printer: `basename $0`\n" >>  $GTMPDIR/b$seqid 
	date >> $GTMPDIR/b$seqid
	echo "\n" >>  $GTMPDIR/b$seqid
	if [ -n "$title" ]
	then
		/usr/bin/banner $title >> $GTMPDIR/b$seqid
	fi
#	echo "\014\c" >> $GTMPDIR/b$seqid
#Trailer on the banner page
	echo "$x\n$x\n$x\n$x\n" >> $GTMPDIR/b$seqid
fi

if [ $BANNER != 0 -a $FACEUP != 1 ]
then
	$send $GTMPDIR/b$seqid 2> /dev/null
fi

i=1
while [ $i -le $copies ]
do

	for file in $files
	do
		magic=`dd bs=1 count=2 if=$file 2> /dev/null`
 
                # Look at chars 2 and 3 in case first care is a control D
                # (Some PC software adds a leading Control D). 

                magic1=`dd bs=1 count=2 skip=1 if=$1 2> /dev/null`

		if [ "$magic" = "%!" ]  # PostScript, send to printer
		then
			cat "$file" 2>&1
                elif [ "$magic1" = "%!" ]  # PostScript, send to printer
                then
                        cat "$file" 2>&1
		else
                        # Convert to PosTScript and send 
			isend=/usr/lib/print/pprint
			# check to see if it is an image file
			set -- `dd bs=1 count=2 < $file 2> /dev/null | od -h`
			if [ "$2" = 01da -a -x $isend ]
			then
				$isend $file
			else
magic=
				magic="`/bin/file $file | /usr/bin/cut -d: -f2`"
			case "$magic" in 
				*script* | *ascii* | *command* | *English* | *ext* | *iso8859* )  
			$send $opts $file  2>/dev/null 
				;;
				* )
		    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	
			fi
		fi
	done
	i=`expr $i + 1`
done

if [ $BANNER != 0 -a $FACEUP != 0 ]
then
      	$send $GTMPDIR/b$seqid 2> /dev/null
fi

if [ $VERBOSE != 0 ]
then
		echo $printer: $seqid End - `date` >> $LOGFILE
fi

if [ $BANNER != 0 ]
then
	/bin/rm -f $GTMPDIR/b$seqid
fi

exit 0
