#!/usr/bin/sh
## @(#) $Header: HPGL2,v 78.5 96/04/22 14:19:49 ssa Exp $
###############################################################################
##
## Interface Program for DesignJet Plotters
##
## This interface program file, which is based on DesignJet interface program
## file for LP spooler, is intended for use by HPDPS supervisor.  Generally,
## interface programs which are used in the LP spooler can be used in the
## HPDPS environment.  However, the following changes are required:
##
## o Write the log and error messages to standard error output instead of
##   /var/adm/lp/log, which is the log file used by the LP spooler.  The
##   messages written to standard error output are logged by HPDPS supervisor.
##
## o Don't rely on the LP spooler.  It may not be installed.
##
##   (1) Don't use filter programs provided by the LP spooler.  The following
##       filters are available in the HPDPS environment.  If they are needed,
##       add /opt/pd/lbin to the $PATH environment variable.
##
##         HPDPS location                         LP location
##      
##       - /opt/pd/lbin/divpage                   /usr/lbin/divapage
##       - /opt/pd/lbin/fontdl                    /usr/lbin/fontdl
##       - /opt/pd/lbin/lprpp                     /usr/lbin/lprpp
##       - /opt/pd/lbin/plotdvr                   /usr/lbin/plotdvr
##       - /opt/pd/lbin/printstat                 /usr/sbin/printstat
##       - /opt/pd/lbin/reverse                   /usr/sbin/reverse
##
##       The following fonts are also available:
##
##         HPDPS location                         LP location
##
##       - /opt/pd/lib/fonts/lp2l                 /usr/lib/lp/fonts/lp2l
##       - /opt/pd/lib/fonts/lp4l                 /usr/lib/lp/fonts/lp4l
##       - /opt/pd/lib/fonts/lp4p                 /usr/lib/lp/fonts/lp4p
##
##   (2) Don't access LP spooler specific files.  Some interface program
##       need to know the the path name of the device file associated with
##       the physical printer.  In HPDPS environment, the path name cannot
##       be obtained by looking for those files.  Use job device-name assigned
##       attribute for this case.  This attribute is internally set by HPDPS
##       when connected via the serail or parallel interface.  Follow the
##       folloinwg steps:
##
##       - Create a new option like "dev"
##       - Add the following entry in the corresponding ipmap file.
##
##         device-name-assigned=$1  > dev$1
##
## o Modify the banner page function if desired.  In HPDPS environment the
##   banner page function is used when the job attribute job-sheets is
##   specified with the value job-set-start (default).
##
## In HPDPS environment, the following parameters are passed to the interface
## program from the HPDPS supervisor.
##
## o $1: Request ID generated by the HPDPS supervisor.
##
## o $2: User ID.  The value of job-originator job attribute is used.
##
## o $3: Title.  The value of job-name attribute is used if specified.  If
##   not, the name of the first document in the job is used.
##
## o $4: Copies.  See the description about the job interface-program-method
##   attribute below.
##
## o $5: Options.  Job and document attributes are converted to the interface
##   program options according to the corresponding ipmap file.  Then they are
##   passed as the $5 parameter of the interface program.
##
## o $6...: Document File Names.  See the description about the job interface-
##   program-method attribute below.
##
## -- NOTE : interface-program-method -----------------------------------------
##
## In HPDPS environment, the interface program is executed to print one job
## result set by defalt.  However, this can be changed by the job interface-
## program-method attribute.
##
## o If the hpdps (default) is specified for the job-attribute interface-
##   program-method, the interface program is invoked to print one job
##   result set according to the ISO/DPA 10175 print architecture:
##
##   - The value of the job-copies component of the job  results-profile
##     attribute is passed as the 4th argument of the interface program.
##
##   - Each document is duplicated n times before being passed to the
##     interface program, where n is the value of the document copy-count
##     attribute.
##
##   For example,
##
##   pdpr -p logical_printer\
##        -x "interface-program-method=hpdps"\
##        -x "results-profile=:::2:"\
##        -x "copy-count=3"\
##        document1 document2 document3
##
##   In this case, the following are passed to the $4 and $6... parameters:
##
##   $4:  2
##   $6:  document_1
##   $7:  document_1
##   $8:  document_1
##   $9:  document_2
##   $10: document_2
##   $11: document_2
##   $12: document_3
##   $13: document_3
##   $14: document_3
##
## o If the lp is specified for the job-attribute interface-program-
##   method, the interface program is invoked to print each job copy in
##   one job result set:
##
##   - The value of document attribute copy count is passed as the 4th
##     argument of the interface program.
##
##   - The interface program is invoked n times, where n is the value
##     of the job-copies component of the job results-profile attribute.
##
##   In this case, the interface program is executed 2 times with the
##   following paramers:
##
##   $4:  3
##   $6:  document_1
##   $7:  document_2
##   $8:  document_3
##
###############################################################################
#=======================================================================#
# **** This script may be used with Designjet plotter models     ****   #
# **** which understand PostScript.  Options below may           ****   #    
# **** NOT be available to all models.  Use them discreetly      ****   #
#                                                                       #
# OPTIONS RECOGNIZED:                                                   #
#                                                                       #
# Language Switch                                                       #
#       postscript      print in PostScript mode                        #
#	hpgl		send HPGL file to plotter wrapped with IN/PG    #
#	hpgl2		send HPGL2 to plotter wrapped with IN/PG        #
#       relay           pass the data to the plotter without adding any #
#                       options or personality switching statement      #
#			or HPGLx commands				#
#=======================================================================#

PATH="/usr/bin:/usr/lbin:/usr/sbin:/sbin:/opt/pd/lbin"
export PATH

# Save the arguments to the model
printer=`basename $0`
reqid=$1
user=$2
title=$3
copies=$4
options=$5
# Assume that the rest of the arguments are files
shift; shift; shift; shift; shift
files="$*"

# Set up interface (parallel/serial, no effect on JetDirect)
if [ -t 1 ]
then
	stty raw 9600 -parenb cs8 ixon -istrip clocal <&1 2>/dev/null
else
	slp -r 2>/dev/null
fi

# Handle disable and cancel traps.  
trap "trap 15; kill -15 0; exit 0 "  15

# default language.
lang="HPGL"

# Determine which options have been invoked

for i in $options
do
    case "$i" in
	hpgl)
	    lang="HPGL";;
	hpgl2)
	    lang="HPGL2";;
	postscript)
	    lang="PS";;
	relay)
	    lang="RELAY";;
    esac
done

# ********** Start sending bytes out  ****************

echo "\033%-12345X@PJL"       

# Begin-of-Job
echo "@PJL JOB NAME = \"Job:$reqid; User: $user\""    

# Print the spooled files
i=1
while [ $i -le $copies ]
do
   for file in $files
   do
       case "$lang" in
         HPGL)
           echo "\033%-12345X@PJL enter language=HPGL"
           echo "IN;\n"
           cat "$file" 2>&1    # straight to the printer
           echo "PG;\n"
           echo "\033%-12345X@PJL"
           ;;
         HPGL2)
           echo "\033%-12345X@PJL enter language=HPGL2"
           echo "IN;\n"
           cat "$file" 2>&1    # straight to the printer
           echo "PG;\n"
           echo "\033%-12345X@PJL"
           ;;
         PS)       
           echo "\033%-12345X@PJL enter language=postscript"
           cat "$file" 2>&1    # straight to the printer
           echo "\033%-12345X@PJL"
           ;;
         RELAY)
           cat "$file" 2>&1    # straight to the printer
           echo "\033%-12345X@PJL"
           ;;
	esac
    done
    i=`expr $i + 1`
done

# end of job
echo "\033%-12345X@PJL RESET"
echo "@PJL EOJ NAME = \"Job: $reqid, User: $user\" "     # end-of-job, 
echo "\033%-12345X@PJL"

# RS-232 interface insure all buffers are flushed to printer
if [ -t 1 ]
then
     stty raw 9600 -parenb cs8 ixon -istrip clocal <&1 2>/dev/null
fi

exit 0

