############################################################################
##########   DCC Network interface script     ##############################
##########        LPPlus rlse 3.1             ##############################
########## Parameters to this script are:     ##############################
##########    1. Request ID                   ##############################
##########    2. User ID                      ##############################
##########    3. Title                        ##############################
##########    4. Number of copies to print    ##############################
##########    5. Option list                  ##############################
##########    6. File to be printed           ##############################
##########    7. HOST                         ##############################  
##########    8. Printer Control name         ##############################
##########    9. Form name                    ##############################
##########   10. Character set                ##############################
##########   11. Lines in form                ##############################
############################################################################
TPUT=tput
CR=`${TPUT} cr 2>/dev/null`
[ -z "${CR}" ] && CR="\r"  # Init CR
FF=`${TPUT} ff 2>/dev/null` # Init formfeed
[ -z "${FF}" ] && FF="\f"
NL="${CR}\n"               # Init NL
PAD="#####${NL}"
############ Init columns ##################################################
COLS=`${TPUT} cols 2>/dev/null`
[ -z "${COLS}" -o 0 -ge "${COLS}" ] && COLS=132
############################################################################
export PT
RAW="no"
export RAW
WPN="no"          # LanMgr notify flag
SYSNOTIFY="no"         # Remote system notify flag
BANNER="yes"           # Default to banner in front of requests
export BANNER
RESTART=1              # Default to start at first page
EXIT_CODE=0            # Init exit code
TMPPREFIX="/tmp/"      # Prefix for temporary files
export TMPPREFIX
: ${CHARSETDIR:=/usr/lib/charsets}
export CHARSETDIR
EXIT_FILE="${TMPPREFIX}exit$$"    # Exit code file name
export EXIT_FILE
export DATATYPE
export FILEBREAK
export LPPRGROUP
FILE="$6"              # Rename input parms
PRINTER=`basename $0`  #
export PRINTER
COPIES=$4              #
REQUEST_ID="$1"        #
export REQUEST_ID
USER="$2"              #
OPTION_LIST="$5"       # 
export OPTION_LIST
TITLE="$3"             #
export TITLE
CONTROL_NAME="$8"      #
export CONTROL_NAME
FORM_NAME="$9"         #
export FORM_NAME
HOST="$7"
export HOST
shift
CHARSET="$9"           # Get character set
shift
LINES="$9"             # Get number of lines
if [ "$LINES" = "0" ]
then
    LINES=`${TPUT} lines 2>/dev/null`
    [ -z "${LINES}" -o 0 -ge "${LINES}" ] && LINES=66
    if [ LINES -gt 80 ]
    then
        LINES=88
    else
        LINES=66
    fi
fi
if [ "x$FILTER" = "x" ]
then
    FILTER="$LPHOME/bin/dcclpdcli"
fi
export FILTER
if [ "x$FILTER2" = "x" ]
then
    FILTER2="none"
fi
export FILTER2
i=1                    # Init copy counter
#####################   banner function   ############################
banner_sep ()
    {
    echo "${CR}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#####             L P  P L U S  3.1                      ${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#####  Copyright (C) 1993-8 Digital Controls Corporation ${PAD}\c"
    echo "#########################################################${PAD}"
    echo "             User: ${HOST} ${NL}"
    if [ -n "${TITLE}" ]
    then
        echo " \c"
        echo "           Title: ${TITLE}${NL}"
    fi
    if [ "${CONTROL_NAME}" = "*X*" ]
    then
        echo "\c"
    else
        echo " \c"
        echo "   Print Control: ${CONTROL_NAME}${NL}"
    fi
    if [ "${FORM_NAME}" = "*X*" ]
    then
        echo "\c"
    else
        echo " \c"
        echo "       Form Name: ${FORM_NAME}${NL}"
    fi
    echo " \c"
    echo "         Printed: `date '+%a %H:%M %h %d, %Y'` ${NL}"
    echo " \c"
    echo "      Request ID: ${REQUEST_ID}  ${NL}"
    echo " \c"
    echo "          System: `uname` ${NL}"
    echo "${NL}\c"
    if [ "${RESTART}" -ne 1 ]
    then
        echo "             Restarted from page ${RESTART} ${NL}\c"
    fi
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "#########################################################${PAD}\c"
    echo "${CR}${FF}\c"
    }
####################   Postscript banner function #########################
banner_post ()
{
    echo "%!PS"
    echo "/inch { 72 mul } def"
    echo "save"
    echo "/Times-Roman findfont"
    echo "32 scalefont"
    echo "setfont"
    echo "3.0 inch 10.5 inch moveto"
    echo "(${REQUEST_ID} - ${HOST}) show"
    echo "/Times-Roman findfont"
    echo "20 scalefont"
    echo "setfont"
    echo "1.0 inch 9.5 inch moveto"
    echo "(LP Plus 3.1) show"
    echo "1.0 inch 9.1 inch moveto"
    echo "(Copyright 1998 Digital Controls Corporation) show" 
    echo "/Times-Roman findfont"
    echo "12 scalefont"
    echo "setfont"
    echo "1.0 inch 8.4 inch moveto"
    echo "(User: ${HOST}) show"
    echo "1.0 inch 8.1 inch moveto"
    echo "(Title: ${TITLE}) show"
    echo "1.0 inch 7.8 inch moveto"
    echo "(Print Control: ${CONTROL_NAME}) show"
    echo "1.0 inch 7.5 inch moveto"
    echo "(Form Name: ${FORM_NAME}) show"
    echo "1.0 inch 7.2 inch moveto"
    echo "(Printed: `date '+%a %H:%M %h %d, %Y'`) show"
    echo "1.0 inch 6.9 inch moveto"
    echo "(System: `uname`) show"
    echo "1.0 inch 6.6 inch moveto"
    if [ "${RESTART}" -ne 1 ]
    then
        echo "(Restarted from page ${RESTART}) show"
    fi
    echo "/Times-Roman findfont"
    echo "2.25 inch scalefont"
    echo "setfont"
    echo ".90 setgray"
    echo "2.5 inch 2.0 inch moveto"
    echo " 52 rotate"
    echo "(LP Plus!) false charpath"
    echo "2 setlinewidth stroke"
    echo "showpage"
    echo "restore"
}
#############  Intercept cancel #########################################
catch_interrupt ()
    {
    EXIT_CODE="8"
    exit_func
    }
##############  Intercept carrier drop    ###############################
catch_hangup ()
    {
    EXIT_CODE="7"
    exit_func
    }
####################   exit function #####################################
exit_func ()
{

##
##  Export environment variables for interface event script
##
export BANNER
export CHARSET
export COLS
export COPIES
export COPYBREAK
export DATATYPE
export EXIT_CODE
export EXIT_FILE
export FILE
export FILTER
export FORM_NAME
export HOST
export LF_TO_CRLF
export LINES
export LPHOME
export PRINTER
export RAW
export REQUEST_ID
export RESTART
export TITLE
export USER
export WPN

##
##  Execut interface event script
##
${LPHOME}/event/interface.sh

exit ${EXIT_CODE}
}
##############  Parse function for -o list ##############################
parse ()
    {
    echo "`expr \"$1\" : \"^[^=]*=\(.*\)\"`"
    }
#########################################################################
trap 'exit 10' 15 9        # Return a 10 if killed
trap 'catch_hangup' 1
trap 'catch_interrupt' 2 3
########### Parse -o string  ############################################
for xx in ${OPTION_LIST}
    do
        case "$xx" in
        nobanner )
                BANNER="no"
                export BANNER
                ;;
        banner )
                BANNER="yes"
                export BANNER
                ;;
        nofilebreak )
                FILEBREAK="no"
                export FILEBREAK
                ;;
        filebreak )
                FILEBREAK="yes"
                export FILEBREAK
                ;;
        fbst )
                FILEBREAK="s"
                export FILEBREAK
                ;;
        nocopybreak )
                COPYBREAK="no"
                export COPYBREAK
                ;;
        copybreak )
                COPYBREAK="yes"
                export COPYBREAK
                ;;
        lfc )
                LF_TO_CRLF="yes"
                export LF_TO_CRLF
                ;;
        nolfc )
                LF_TO_CRLF="no"
                export LF_TO_CRLF
                ;;
        restart=* )
                RESTART=`parse $xx`
                export RESTART
                ;;
        stty=* )
                if [ `parse $xx` = "raw" ]
                then
                   RAW="yes"
                   export RAW
                fi
                ;;
        raw    )
                RAW="yes"
                export RAW
                ;;
        nopurge )
                PURGE="no"
                export PURGE
                ;;
        purge   )
                PURGE="yes"
                export PURGE
                ;;
        wpn )
                WPN="yes"
                export WPN
                ;;
        sysnotify )
                SYSNOTIFY="yes"
                export SYSNOTIFY
                ;;
        esac
    done
###########  Print banner   #############################################
if [ "${BANNER}" = "yes" ]
then
    if [ "${DATATYPE}" = "post-DSC" -o "${DATATYPE}" = "post" -o "${DATATYPE}" = "post-NR" ]
    then
        banner_post > $LPHOME/tmp/$$
    else
        banner_sep > $LPHOME/tmp/$$
    fi
fi
    FILTER_STRING="$FILTER 0 ${HOST} ${PRINTER} ${LPHOME} ${LINES} \
      ${COLS} ${RESTART} ${REQUEST_ID} ${FORM_NAME} ${CONTROL_NAME} \
      ${LPHOME}/tmp/$$ ${LF_TO_CRLF} ${TITLE} ${COPIES}"
    trap '' 1        #Let the filter handle these
    trap '' 2 3 
    {
    eval ${FILTER_STRING}  
    }
    trap 'catch_hangup' 1
    trap 'catch_interrupt' 2 3
    if [ ! -s "${EXIT_FILE}" ]
    then
        sleep 7
    fi
    if [ ! -s "${EXIT_FILE}" ]
    then
        EXIT_CODE="6"
    else
        EXIT_CODE=`cat ${EXIT_FILE}`
    fi
    rm "${EXIT_FILE}"
    if [ "${BANNER}" = "yes" ]
    then
       rm $LPHOME/tmp/$$
    fi
exit_func 
###########################################################################
