#!/bin/sh

#
# Copyright 1993 Network Computing Devices, Inc.
#
# Permission to use, copy, modify, distribute, and sell this software and
# its documentation for any purpose is hereby granted without fee, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name Network Computing Devices, Inc. not be
# used in advertising or publicity pertaining to distribution of this 
# software without specific, written prior permission.
# 
# THIS SOFTWARE IS PROVIDED `AS-IS'.  NETWORK COMPUTING DEVICES, INC.,
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
# LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL NETWORK
# COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
# OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
# WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# $NCDId: @(#)bsd_if.sh,v 1.3 1994/08/04 00:20:04 dct Exp $
#
# By convention the name of the input filter (if=) should be the same as
# the name of the printer entry in /etc/printcap and should be linked
# to this shell script.  For example, the printcap entry might contain
# something like:
#
#	printername:\
#		:if=/usr/local/bin/ncdprint/printername:\
#

READN=ncdreadn
CHR=ncdchr
ECHO=ncdecho

# Remember who we are.
IAM=$0

# Add the directory containing this script to the front of the PATH.
dir=`expr $0'/' : '\(/\)[^/]*/$' \| $0'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \| .`
PATH=${dir}:$PATH
export PATH

# Get the basename of this script.  This will be the name used to look
# things up in the printcap file and the default name for the NCD
# terminal with the printer connected.
printer_name=`expr $0'/' : '/\([^/]*\)/$' \| $0'/' : '.*[^/]//*\([^/][^/]*\)//*$' \| $0`

# Define spooler dictated exit code values.
EXIT_OK=0
EXIT_REPRINT=1
EXIT_DISCARD=2

# Initialize parameter values.
options=""
debug=false
host=unknown
name=unknown

# Process arguments.
set XXX `getopt dh:i:l:n:w: $*`
if [ $? != 0 ]
then
    echo $IAM: Usage: $IAM ... >&2
    exit $EXIT_USAGE
fi
shift # shift off XXX
for i in $*
do
    case "$i" in
	-d)
	    debug=true
#	    options="-d ${options}"
	    set -x
	    shift
	    ;;
	-h)
	    host="$2"
	    options="${options} $1 $2"
	    shift; shift
	    ;;
	-n)
	    name="$2"
	    options="${options} $1 $2"
	    shift; shift
	    ;;
	--)
	    shift
	    break
	    ;;
	-*)
	    options="${options} $1 $2"
	    shift; shift
	    ;;
    esac
done

# Last optional parameter is accounting file.
if [ $# -gt 0 ]
then
    acct_file=$1
    shift
fi

# If there are parameters left, that's bad.
if [ $# -gt 0 ]
then
    echo $IAM: Usage: $IAM ... >&2
    exit $EXIT_USAGE
fi

# Check to see that we can locate the printcap entry.
if [ "X"`prpcent ${printer_name} if` = "X" ]
then
    echo $IAM: Configuration error in /etc/printcap >&2
    exit $EXIT_DISCARD
fi

# Determine the printer type from the printcap entry.
printer_type=`prpcent ${printer_name} PT`
if [ "X${printer_type}" = "X" ]
then
    echo $IAM: Printer type \(PT\) not defined in /etc/printcap entry >&2
    exit $EXIT_DISCARD
fi

# Checking for the driver using -x would be more appropriate here,
# but (sigh) ULTRIX doesn't support it so use -f for the sake of
# portability
if [ ! -f ${dir}/${printer_type}.driver ]
then
    echo $IAM: Unknown printer type: ${printer_type} >&2
    exit $EXIT_DISCARD
fi

# Use the CF parameter to get additional command line flags.
flags=`prpcent ${printer_name} CF`
if [ "X${flags}" != "X" ]
then
    options="${options} ${flags}"
fi

# And, finally, print the job.

echo `date +"%y-%m-%d %T:"` "$IAM: Starting job for ${name}@${host}" >&2

status=$EXIT_OK
trap "rm -f /tmp/$$.pc; exit \${status}" 0
${ECHO} "0000" > /tmp/$$.pc

cat - | \
(   A=`${READN} 2`; \
    ( ${CHR} $A; cat ) | \
    if [ "$A" = "% !" ]; \
    then \
	${printer_type}.driver ${options} ps ${printer_name} > /tmp/$$.pc; \
    else \
	${printer_type}.driver ${options} text ${printer_name} > /tmp/$$.pc; \
    fi; \
)
status=$?

echo `date +"%y-%m-%d %T:"` "$IAM: End of print job for ${name}@${host} -- status=${status}" >&2

# Accounting is currently not supported.  To do so, we'd need to send a line
# to the file $acct_file which looks like:
#
#   pppp.00<tab>host:name
#
# where pppp is the number of pages printed, host is $host and name is $name.
#(cat /tmp/$$.pc; print ".00"; chr HT; print "${host}:${name}") >&2

# Here we try to remap the exit status of ncdprint (cat2ncd or ps2ncd).  Since
# there appears to be no good way to detect a runaway spooler filter, we'll just
# discard any job that gets caught up in the mess.  This is unfortunate, but
# when we tried exiting with the code that tells the spooler to reprint, the
# spooler brought the system to it's knees.  The moral is, make sure the printer
# connection is operational before enabling the spooler!

case $status in
    0) # no errors, job printed
	: status is already set to indicate success
	;;
    1) # read or write failure involving NCD unit; would like to reprint
       # but this causes spooler to spawn filter procs like crazy.
	status=$EXIT_DISCARD
	;;
    2) # printer reported error (PostScript only)
	: status is already set so that the job will be discarded
	;;
    3) # command line usage error
	status=$EXIT_DISCARD
	;;
    4) # unable to connect to printer
	status=$EXIT_DISCARD
	;;
    5) # some internal error (usually select failure)
	status=$EXIT_DISCARD
	;;
    *) # who knows, probably dumped core
	status=$EXIT_DISCARD
	;;
esac

# status will actually be set in the trap "..." 0 line
exit $status
