############################################################################
##########    DCC terminal server 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="\n${CR}"               # Init NL
PAD="#####${NL}"
############ Init columns ##################################################
COLS=`${TPUT} cols 2>/dev/null`
[ -z "${COLS}" -o 0 -ge "${COLS}" ] && COLS=132
############################################################################
BANNER="yes"           # Default to banner in front of requests
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"              #
export USER
OPTION_LIST="$5"       # 
export OPTION_LIST
TITLE="$3"             #
export TITLE
HOST="$7"
export HOST
CONTROL_NAME="$8"      #
export CONTROL_NAME
FORM_NAME="$9"         #
export FORM_NAME
shift
CHARSET="$9"           # Get character set
shift
LINES="$9"             # Get number of lines
if [ "$FILTER" = "" ]
then
    FILTER="$LPHOME/bin/dcctmserv"
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"
}
####################   exit function #####################################
exit_func ()
{
##
##  Export environment variables for interface event script
##
export BANNER
export CHARSET
export COLS
export COPIES
export COPYBREAK
export EXIT_CODE
export FILE
export FILTER
export LF_TO_CRLF
export LINES
export LPHOME
export PRINTER
export RAW
export RESTART
export WPN

##
##  Execute 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
                ;;
        nopurge )
                PURGE="no"
                export PURGE
                ;;
        purge   )
                PURGE="yes"
                export PURGE
                ;;
        esac
    done
###########  Print banner   #############################################
if [ "${BANNER}" = "yes" ]
then
    if [ "${DATATYPE}" = "post-DSC" -o "${DATATYPE}" = "post"  -o "${DATATYPE}" = "post-NR" ]
    then
        banner_post 
    else
        banner_sep 
    fi
fi
FILTER_STRING="$FILTER 0 ${HOST} ${PRINTER} ${LPHOME} ${LINES} \
  ${COLS} ${RESTART} ${REQUEST_ID} ${FORM_NAME} ${CONTROL_NAME} \
  0 ${LF_TO_CRLF} ${TITLE} ${COPIES}"
for file in ${FILE}
do
    if [ -r "${file}" ]
    then
        trap '' 1        #Let the filter handle these
        trap '' 2 3 
        {
        0<${file} eval ${FILTER_STRING}  
        }
        if [ ! -s "${EXIT_FILE}" ]
        then
            sleep 7
        fi
        trap 'catch_hangup' 1
        trap 'catch_interrupt' 2 3
        if [ ! -s "${EXIT_FILE}" ]
        then
            EXIT_CODE="6"
        else
            EXIT_CODE=`cat ${EXIT_FILE}`
            rm "${EXIT_FILE}"
        fi
    fi
done
exit_func 
###########################################################################
