#!/bin/sh
#
# declps -- UNIX SYSTEM V printer interface wrapper script for
#           Digital Equipment Corporation PrintServer printers.
#
# @(#)declps	1.8	LPS_UNX_COM	9/1/94
#
# This script is used as a "front end" to the "sysvpc" program that
# actually performs the print job processing and communications with
# the PrintServer printer.
#
###

SCRIPTNAME="$0"

QNAME=`basename $0`
TMPNAME=`echo $$${QNAME}.log | cut -c1-14`   # For SHORT-minded SVR3 systems
TMPLOG=/tmp/$TMPNAME

REQID="$1"
USER="$2"
TITLE="$3"
COPIES="$4"
OPTS="$5"
shift ; shift ; shift ; shift ; shift
FILES="$*"

# Common functions

Err ()
{
    echo "$SCRIPTNAME: $*" 1>&2

    echo "`date`  $SCRIPTNAME: $*" >> $TMPLOG
}

Fatal ()
{
    Err "$*"
    Err "Terminating $SCRIPTNAME due to fatal error"

    exit 129	       # This should shutdown the queue
}

# See if we can create our temporary log file:

touch $TMPLOG

if [ $? -ne 0 ]
then
    Err "Unable to create temporary file for PrintServer interface"
fi


# Extract the LPSBIN directory path from the ENV object definition
# from the $LPSODB and ensure the path is valid.

LPSODB=/etc/lpsodb

ENTRY="`sed -n -e '/^LPSENV|/,/[^\\\\]$/p' < $LPSODB`"

LPSBIN="`expr "$ENTRY" : '.*:lpsbin=\([^:]*\):'`"
LPSROOT="`expr "$ENTRY" : '.*:lpsroot=\([^:]*\):'`"

ERRTEXT="could not be extracted from $LPSODB"

if [ -z "$ENTRY" -o -z "$LPSBIN" -o -z "$LPSROOT" ]
then
    if [ -z "$ENTRY" ]
    then
	Err "The ENV entry $ERRTEXT"
    elif [ -z "$LPSROOT" ]
    then
	Err "The LPSROOT path $ERRTEXT"
    else
	Err "The LPSBIN path $ERRTEXT"
    fi

    Fatal "The LPS Object Database ($LPSODB) appears to be corrupted"
fi

# Check to see if $LPSBIN is an absolute path; if not, then
# prepend the $LPSROOT definition to derive what should be
# an absolute path.  Check to see if the $LPSBIN exists, then
# construct the path to the actual printer interface program
# and check to see if it exists for execution.

if [ "`echo $LPSBIN | cut -c1`" != "/" ]
then
    LPSBIN="$LPSROOT/$LPSBIN"
fi

if [ ! -d $LPSBIN ]
then
    Fatal "The $LPSBIN directory does not exist!"
fi

PROG=$LPSBIN/sysvpc

if [ ! -x $PROG ]
then
    Fatal "The executable $PROG interface program does not exist!"
fi

# Finally execute the printer driver to print the job.

$PROG -Q $QNAME "$REQID" "$USER" "$TITLE" "$COPIES" "$OPTS" $FILES > $TMPLOG 2>&1

# Delete the temporary log IFF we were successful in printing the job,
# otherwise leave it hanging around for later inspection by whoever

if [ $? -eq 0 ]
then
    rm -f $TMPLOG
fi
