#!/bin/sh

USAGE="Usage: $0 lpCmd prtDst numCopy prtType prtCol paprWid paprHgt dpi psFile productID"

# ignore hang up signals (ie if parent quits before we are done)
# interrupts and quits
trap "" 1 2 3

lpCmd=$1
prtDst=$2
numCopy=$3
prtType=$4
prtCol=$5
paprWid=$6
paprHgt=$7
dpi=$8
isiDir=$9
shift
psFile=$9


if [ $# -ne 9 ]
then
	echo "$USAGE"
	exit 1
fi

# set site specific lp command options here with the lpopt variable
if [ "$lpCmd" = "lp" ]
then
#  if the printer is lp; we need to copy the file to spool area.  If we don't
#  then the rm command will remove the PostScript file and the print job will
#  fail.
	lpopt="-c -oraw"
else
	lpopt=""
fi

if [ "$prtType" = "POSTSCRIPT" ]
then

	$lpCmd $lpopt $prtDst $numCopy $psFile
	if [ $? -ne 0 ]
	then
		echo "Error!  Printer process failed"
		rm -f $psFile
		exit 4
	fi
else

	if [ -z "$isiDir" -o ! -d "$isiDir" ]
	then
		echo "Cannot determine where HP_filt is located.  Check that"
		echo "the INFORMIXDIR environment variable is defined"
		rm -f $psFile
		exit 5
	fi

	if [ ! -x $isiDir/bin/HP_filt ]
	then
		echo "Error!  HP printer support has not been installed."
		echo "HP printer support must be installed from the Release Media."
		echo "Print job failed."
		rm -f $psFile
		exit 6
	fi

#	parent passes dpi in as $8.  Which will be a multiple of 72 that defines
#	postscript resolution.  Could use that for dpi.  Here we will set it
#	to the best resolution for the type.  Possible dpi size are :
#  LaserJet and DeskJet   75, 100, 150, 300
#  PaintJet               90, 180
#  Lower dpi's render faster, create smaller files to send to the printer
#  but don't look as good.  So I set them to best resolution.

	if [ "$prtType" = "HPLASERJET" -o "$prtType" = "HPDESKJET" ]
	then
		dpi=300
	else
		dpi=180
	fi

	if [ "$prtType" = "HPLASERJET" ]
	then
		page_size="$paprWid $dpi mul cvi $paprHgt $dpi mul cvi $dpi"
		devicearg="$page_size setmonohpljdevice"
	else
		page_size="$paprWid $dpi mul cvi $paprHgt $dpi mul cvi $dpi"
		use_reset_at_start=true
		use_reset_at_end=false
		use_transparency=false
		use_compression=true
		isXLpaintjet=true
		if [ "$prtType" = "HPDESKJET" -a "$prtCol" = "B&W" ]
		then
			setdevice=setmonohppjdevice
		else
			setdevice=setrgbhppjdevice
		fi
		devicearg="$page_size 1 $use_reset_at_start $use_reset_at_end \
			$use_transparency $use_compression $isXLpaintjet $setdevice"
	fi
	$isiDir/bin/HP_filt -no-init -e "
		 { 
		%userdict /#copies 1 put
		(stdout) $devicearg
		/FontFileMapName ($isiDir/bin/HP_map) def
		($isiDir/lib/.hpfonts) setfontpath
		setpipelinefontdevice
		($psFile) run
		 } stopped {
			handleerror
		 } if " | $lpCmd $lpopt $prtDst $numCopy
	if [ $? -ne 0 ]
	then
		echo "Error!  Printer process failed"
		rm -f $psFile
		exit 4
	fi
fi
rm -f $psFile
exit 0
