#!/usr/bin/sh
## @(#) HPDPS $Header: HPGL1,v 78.2 96/03/18 19:54:53 ssa Exp $
###############################################################################
##
## Interface Program for HPGL1
##
## This interface program file, which is based on HPGL1 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 the /opt/pd/bin and /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 to 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
##
###############################################################################

# HPGL1:  lp interface for HP7440A HP7475A plotter.
#
#=====================================================================
# OPTIONS RECOGNIZED:
#
# Do not initialize
#	ni		do not initialize Plotter
#				(default initialize)
# Do not stop for paper change
#	ns		do not stop for paper change
#				(default stop)
# Do not eject a page at the end of the plot
#	np		do not eject page
#				(default eject)
#
#======================================================================
#
plotfilter=/usr/lbin/plotdvr 

if [ -z "$LANG" ]
then
	LANG="C"
	export LANG
fi
# save the arguments to the model
plotter=`basename $0`
reqid=$1
user=$2
title=$3
copies=$4

# Set up RS-232C interface if this is a tty port
if [ -t 1 ]
then
	stty 9600 opost onlcr -parenb cs8 ixon -istrip clocal tab3 <&1 2>/dev/null
fi
# Determine which options have been invoked
initialize=""
paper=""
inputfile="def"
send_PG="true"

for i in $5
do
    case "$i" in
    -ni | ni)  # do not initialize
	initialize="-i";;

    -ns | ns)  # do not stop for paper change
	paper="-f";;

    -np | np) # do not eject a page
	send_PG="false";;

    -dev* | dev* ) # get the device file name
	devicename="`echo "$i" | sed 's/^[-]*dev//'`" ;;
    esac
done

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

# Assume that the rest of the arguments are files
shift; shift; shift; shift; shift
files="$*"

# plot the spooled file

i=1
while [ $i -le $copies ]
do
	for file in $files
	do
		case "$inputfile" in
			def)	$plotfilter -l$reqid -u$user\
				$initialize $paper $file <$devicename >&2;;
			*);;
		esac
		if $send_PG
		then
		    echo ";PG;\n"
		fi
	done
	i=`expr $i + 1`
done

# Insure all buffers are flushed to plotter
if [ -t 1 ]
then
	stty 9600 opost onlcr -parenb cs8 ixon -istrip clocal tab3 <&1 2>/dev/null
fi

exit 0

