#!/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: @(#)hpux_if.sh,v 1.4 1994/08/04 00:20:17 dct Exp $
#
# Interface program for HP-UX systems.
#

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=/usr/local/bin/X11/ncdprint332/
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

# Determine where diagnostic output gets sent
logfile=`prpcent ${printer_name} lf`
if [ "X${logfile}" = "X" ]
then
    logfile=/dev/null
fi

# Initialize parameter values.
options=""
debug=false
host=`hostname`

# ignore request id and title, but save user name and # of copies
name=$2
copies=$4
shift; shift; shift; shift

# Process options
set XXX `getopt dh:i:l: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"
	    shift; shift
	    ;;
	--)
	    shift
	    break
	    ;;
	-*)
	    options="${options} $1 $2"
	    shift; shift
	    ;;
    esac
done

# 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 >> ${logfile}
    exit $EXIT_DISCARD
fi
if [ ! -x ${dir}/${printer_type}.driver ]
then
    echo $IAM: Unknown printer type: ${printer_type} >> ${logfile}
    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}" >> ${logfile}

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

i=0
while [ $i -lt $copies ]
do
    for file in $*
    do
	cat $file | \
	(   A=`${READN} 2`; \
	    ( ${CHR} $A; cat ) | \
	    if [ "$A" = "% !" ]; \
	    then \
		${printer_type}.driver -h ${host} -n ${name} ${options} ps ${printer_name} > /tmp/$$.pc 2>> ${logfile}; \
	    else \
		${printer_type}.driver -h ${host} -n ${name} ${options} text ${printer_name} > /tmp/$$.pc 2>> ${logfile}; \
	    fi; \
	)
    done
    i=`expr $i + 1`
done
status=$?

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

# 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
