#!/bin/sh
#
# lp interface for dumb line printers, serial or parallel
#
# $Revision: 1.16 $
# $Date: 1994/10/18 01:08:12 $
#
# Configurable parameters:
#
# BANNER	If non-zero, print a 132 column banner before each job
# TRAILER	If non-zero, print a page separator with some form feeds at
#		the end of the job.
# RAW		If non-zero, don't do any post-processing like newline to
#		newline/carriage return conversion.
# STTYPARAMS	Add any non-default stty options here.
# BAUDRATE	Sets the baud rate for tty printers (set by printer installers)
#
# The BANNER, TRAILER, and RAW flags may be turned on or off for a given
# print job by using the "-o" option of the lp command.

# Append the stderr output of the filters and drivers to error log file:
#
logfile=/var/spool/lp/log               # lp spooler error log file
exec 2>> $logfile

# Uncomment the following line for debugging info in the logfile
# set -x

# Info for printer administration tools:
#
# NAME=Generic Printer (Text Only),DEVICE=SERIAL
# NAME=Generic Printer (Text Only),DEVICE=CENTRONICS
#
NAME=
TYPE=Dumb

# Change the following to set up defaults for this printer.
# (Some of these are set by the printer administration tools.)
#
STTYPARAMS=
BAUDRATE=9600
BANNER=1
TRAILER=0
RAW=0

# Grab command line arguments
#
printer=`basename $0`                           # the printer's name
seqid=$1                                        # job sequence id number
username=$2                                     # user name
title="$3"                                      # job title  
copies=$4                                       # number of copies
options="$5"                                    # -o options
shift 5                                         # discard args up to filenames
files="$*"                                      # input file(s)

# Parse printer-specific options specified with lp(1) -o option
#
set -- $options ""
while test $# -ne 0
do
   case $1 in
        notrailer | -notrailer)		TRAILER=0 ;;
	trailer   | -trailer)		TRAILER=1 ;;
	banner    | -banner)		BANNER=1  ;;
	nobanner  | -nobanner | h | -h)	BANNER=0  ;;
	raw       | -raw)
           # RAW means "Don't do any post processing on this data."
	   # Works well when sending bitmaps & things to intelligent printers.
	   # Banner pages may not work: may want to disable them when raw set.
					RAW=1     ;;
	cooked    | -cooked)		RAW=0     ;;
	*) # Save any extra options and pass on to filter.
	   if [ -n "$1" ]; then
              opts="$opts $1"
	   fi
	   ;;
   esac
   shift
done

#
# Initialize port parameters
#
filter="cat $opts"
if [ -t 1 ]; then
   stty sane $BAUDRATE <&1
   if [ "$STTYPARAMS" != "" ]; then
      stty $STTYPARAMS <&1
   fi
   if [ "$RAW" != 0 ]; then
      stty -opost <&1
   fi
else
   if [ "$RAW" = 0 ]; then
      # The plp filter supports the sane, onlcr, opost, and tab3
      # stty options.
      filter="/usr/lib/print/plp sane $STTYPARAMS $opts"
   fi
fi

if [ "$BANNER" != 0 ]; then
   # Change this for prettier banners or different number of columns
   x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"

   echo "$x$x$x$x\n\n\n" 					| $filter
   banner $username 						| $filter
   echo "\n\n\n" 						| $filter
   user=`grep "^$username:" /etc/passwd | line | cut -d: -f5`
   if [ "$user" = "" ]; then
      user=`(ypmatch $username passwd | line | cut -d: -f5) 2> /dev/null`
   fi
   if [ -n "$user" ]; then
      echo "User: $user" 					| $filter
   fi
   echo "\n" 							| $filter
   echo "Request id: $seqid    Printer: `basename $0`\n" 	| $filter
   date 							| $filter
   echo "\n" 							| $filter
   if [ -n "$title" ]; then
      banner $title 						| $filter
   fi
   echo "$x$x$x$x\n\n\n" 					| $filter
   echo "\014\c" 						| $filter
fi

i=1
while [ "$i" -le "$copies" ]
do
   for file in $files
   do
      cat "$file" | $filter 2>&1
      if [ "$RAW" = 0 ]
      then
         echo "\014\c" | $filter
      fi
   done
   i=`expr $i + 1`
done

#
# Print a couple rows of XXX followed by some newlines
#
if [ "$TRAILER" != 0 ]
then
   x="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
   echo "$x$x$x$x\n\n\n" 					| $filter
   echo "\014\c" 						| $filter
fi

exit 0
