#!/bin/sh
##########################
##
## Ricoh Aficio AP3850C
## Unix Printing Interface Program
## Last Update 12/09/2004
## Copyright 1999-2004
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## Update History
##
## 5/3/2002 First release
##
##########################

##########################
##
## Debug Flag, set to y to leave temp files in /tmp
##
##########################

DEBUG=

##########################
##
## Parse arguments passed as -o option of lp command
##
##########################

parse () {
	echo "`expr \"$1\" : \"^[^=]*=\(.*\)\"`"
}

##########################
#
# This program is invoked as
#
# ${SPOOLDIR}/.../printer request-id user title copies options files...
#
# The first three arguments are simply reprinted on the banner page,
# the fourth (copies) is used to control the number of copies to print,
# the fifth (options) is a blank separated list (in a single argument)
# of user or Spooler supplied options (without the -o prefix),
# and the last arguments are the files to print.
#
##########################

printer=`basename $0`
request_id=$1
user_name=$2
title=$3
copies=$4
option_list=$5

shift 5
files="$*"

nobanner="no"
nofilebreak="no"
stty=
inlist=

device_tmp=
prtout_tmp=
ps_tmp=
is_TXT=
is_PS=
is_PCL=
TXT=
PS=
PCL=
is_DSC=

##########################
#
# Change the following line to match to
# the remote printer's name
#
##########################

REMOTE_PRINTER=

##########################
#
# device_tmp stores device option commands
# prtout_tmp holds actual printout to the printer
# ps_tmp is used to hold pre-processed input PS file
#
##########################

device_tmp="/tmp/afcdev_$$.tmp"
prtout_tmp="/tmp/afcprt_$$.tmp"
ps_tmp="/tmp/afcps_$$"
pjl_tmp="/tmp/afcpjl_$$"
pcl_tmp="/tmp/afcpcl_$$"

##########################
#
# Store some default values
#
##########################
##########################
#
# Set default # of copies
#
##########################

echo "<</NumCopies $copies >>setpagedevice" >> $device_tmp
#echo "2  /languagelevel where {pop languagelevel}{1} ifelse
#2 ge { 1 dict dup /NumCopies 4 -1 roll put setpagedevice }{ userdict /#copies 3 -1 roll put } ifelse" >> $device_tmp
printf "\033&l%dx" $copies >> $pcl_tmp

##########################
#
# Device options (e.g. -o value=a) are identified
# and processed here.
#
##########################

for i in ${option_list}
do
	case "${inlist}${i}" in

	nobanner|nb )
		nobanner="yes"
		;;

	nofilebreak )
		nofilebreak="yes"
		;;

##########################
#
# Debug option
#
##########################

	debug )
		DEBUG=Y
		;;

##########################
#
# Text option
#
##########################

	text )
		TXT=1
		;;

##########################
#
# PS option
#
##########################

	ps )
		PS=1
		;;

##########################
#
# PCL option
#
##########################

	pcl|nofilter )
		PCL=1
		;;

##########################
# LNX_TUB_B
#
# Character compresser (16.5 or 16.7 per line)
#
##########################
        compress )
		printf "\033&k2S" >> $pcl_tmp
		;;

##########################
#
# Line Spacing
#
##########################
        lpi* )
		lpi=`expr $i : 'lpi\([0-9]*\)'`
		printf "\033&l%dD" $lpi >> $pcl_tmp
		;;

##########################
#
# Pitch (character per inch)
#
##########################
	cpi* )
		pitch=`expr $i : 'cpi\([0-9.]*\)'`
		if [ -n "$pitch" ]
		then
			printf "\033(s%.2fH" $pitch >> $pcl_tmp
		fi
		;;

	duplex=* )
		duplex=`parse ${i}`
		;;

	staple=* )
		staple=`parse ${i}`
		;;

	punch=* )
		punch=`parse ${i}`
		;;

	twohole=* )
		twohole=`parse ${i}`
		;;

	mediatype=* )
		mediatype=`parse ${i}`
		;;

	resolution=* )
		resolution=`parse ${i}`
		;;

	itray=* )
		itray=`parse ${i}`
		;;

	outbin=* )
		outbin=`parse ${i}`
		;;

	collate=* )
		collate=`parse ${i}`
		;;

	jobtype=* )
		jobtype=`parse ${i}`
		;;

	userid=* )
		userid=`parse ${i}`
		;;

	password=* )
		password=`parse ${i}`
		password=`expr $password : '\([0-9][0-9][0-9][0-9]\)'`
#		echo $password > /tmp/aaa
		if test -z "$password" -a "$jobtype" = "secure"
		then
		    jobtype="normal"
		fi
		;;

	paper=* )
		paper=`parse ${i}`
		;;

	color=* )
		color=`parse ${i}`
		;;

	slipsheet=* )
		slipsheet=`parse ${i}`
		;;

	slipsheettray=* )
		slipsheettray=`parse ${i}`
		;;

	landscape=* )
		landscape=`parse ${i}`
		;;

	rotate180=* )
		rotate180=`parse ${i}`
		;;

	rendering=* )
		rendering=`parse ${i}`
		;;

	brightness=* )
		brightness=`parse ${i}`
		;;

	rgb=* )
		rgb=`parse ${i}`
		;;

	gamma=* )
		gamma=`parse ${i}`
		;;

	whitepoint=* )
		whitepoint=`parse ${i}`
		;;

	phosphors=* )
		phosphors=`parse ${i}`
		;;

	separation=* )
		separation=`parse ${i}`
		;;

	simulation=* )
		simulation=`parse ${i}`
		;;

	simspeed=* )
		simspeed=`parse ${i}`
		;;

	outprofile=* )
		outprofile=`parse ${i}`
		;;

	pureblack=* )
		pureblack=`parse ${i}`
		;;

	blackoverprint=* )
		blackoverprint=`parse ${i}`
		;;

	spotcolor=* )
		spotcolor=`parse ${i}`
		;;

	texthalftone=* )
		texthalftone=`parse ${i}`
		;;

	graphicshalftone=* )
		graphicshalftone=`parse ${i}`
		;;

	imagehalftone=* )
		imagehalftone=`parse ${i}`
		;;

	esac

done

##############################################################
#
# Check some relational conditions here:
#
##############################################################

if [ "$itray" = "bypass" ]
then
	staple=off
	punch=off
fi

if [ -n "$staple" -a "$staple" != "off" ]
then
	collate="on"
fi

if [ -n "$slipsheettray" ]
then
	slipsheet="on"
fi

if [ "$slipsheet" = "on" -a -z "$mediatype" ]
then
	mediatype="transparency"
fi

if [ -z "$punch" -o "$punch" = "off" ]
then
	twohole="false"
fi

##########################
#
# Paper Orientation (Default=Portrait)
#
##########################

	case "$landscape" in

	yes )
		case "$rotate180" in

		yes )
			printf "\033&l3O" >> $pcl_tmp
			;;

		* )
			printf "\033&l1O" >> $pcl_tmp
			;;

		esac
		;;

	* )
		case "$rotate180" in

		yes )
			printf "\033&l2O" >> $pcl_tmp
			;;

		* )
			printf "\033&l0o" >> $pcl_tmp
			;;

		esac
		;;

	esac

##########################
#
# Resolution (Default=600x600dpi)
#
##########################

		case "$resolution" in

		600 )
			(
			echo "(600X600) XJXsetresolution" >> $device_tmp
			echo "@PJL SET RESOLUTION=600" >> $pjl_tmp
			)
			;;

		1200 )
			(
			echo "(1200X1200) XJXsetresolution" >> $device_tmp
			echo "@PJL SET RESOLUTION=1200" >> $pjl_tmp
			)
			;;

		* )
			(
			echo "(600X600) XJXsetresolution" >> $device_tmp
			echo "@PJL SET RESOLUTION=600" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Halftone Text (Default=Detail)
#
##########################

		case "$texthalftone" in

		detail )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD) }{ (EFI_StandardHTD) } ifelse XJXsetTextHalftone" >> $device_tmp
			)
			;;

		smooth )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD2) }{ (EFI_StandardHTD2) } ifelse XJXsetTextHalftone" >> $device_tmp
			)
			;;

		* )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD) }{ (EFI_StandardHTD) } ifelse XJXsetTextHalftone" >> $device_tmp
			)
			;;

		esac

##########################
#
# Halftone Graphics (Default=Detail)
#
##########################

		case "$graphicshalftone" in

		detail )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD) }{ (EFI_StandardHTD) } ifelse XJXsetGraphicsHalftone" >> $device_tmp
			)
			;;

		smooth )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD2) }{ (EFI_StandardHTD2) } ifelse XJXsetGraphicsHalftone" >> $device_tmp
			)
			;;

		* )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD) }{ (EFI_StandardHTD) } ifelse XJXsetGraphicsHalftone" >> $device_tmp
			)
			;;

		esac

##########################
#
# Halftone Image (Default=Detail)
#
##########################

		case "$imagehalftone" in

		detail )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD) }{ (EFI_StandardHTD) } ifelse XJXsetImageHalftone" >> $device_tmp
			)
			;;

		smooth )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD2) }{ (EFI_StandardHTD2) } ifelse XJXsetImageHalftone" >> $device_tmp
			)
			;;

		* )
			(
			echo "currentpagedevice /HWResolution get 0 get 1200 eq
			{ (EFI_1200HTD) }{ (EFI_StandardHTD) } ifelse XJXsetImageHalftone" >> $device_tmp
			)
			;;

		esac

##########################
#
# Color Mode (Default=Yes)
#
##########################

		case "$color" in

		yes )
			(
			echo "userdict /XJXsetcolormode known
			   { (CMYK) XJXsetcolormode} if" >> $device_tmp
			echo "@PJL SET RENDERMODE=COLOR" >> $pjl_tmp
			)
			;;

		no )
			(
			echo "userdict /XJXsetcolormode known
			   { (Grayscale) XJXsetcolormode} if" >> $device_tmp
			echo "@PJL SET RENDERMODE=GRAYSCALE" >> $pjl_tmp
			)
			;;

		* )
			(
			echo "userdict /XJXsetcolormode known
			   { (CMYK) XJXsetcolormode} if" >> $device_tmp
			echo "@PJL SET RENDERMODE=COLOR" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Slip Sheet (Default=Off)
#
##########################

		case "$slipsheet" in

		off )
			(
			echo "userdict /XJXsetSlipSheet known
			   { 0 XJXsetSlipSheet }
			   { (printerinfo SlipSheet 0) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SLIPSHEET=GENERICOFF" >> $pjl_tmp
			)
			;;

		on )
			(
			echo "userdict /XJXsetSlipSheet known
			   { 1 XJXsetSlipSheet }
			   { (printerinfo SlipSheet 1) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SLIPSHEET=GENERICON" >> $pjl_tmp
			)
			;;

		* )
			(
			echo "userdict /XJXsetSlipSheet known
			   { 0 XJXsetSlipSheet }
			   { (printerinfo SlipSheet 0) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SLIPSHEET=GENERICOFF" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Landscape (Default=No)
#
##########################

		case "$landscape" in

		no )
			(
			echo "userdict /EFLandscapeSetup known { 0 EFLandscapeSetup } if " >> $device_tmp
			)
			;;

		yes )
			(
			echo "userdict /EFLandscapeSetup known { 1 EFLandscapeSetup } if " >> $device_tmp
			)
			;;

		* )
			(
			echo "userdict /EFLandscapeSetup known { 0 EFLandscapeSetup } if " >> $device_tmp
			)
			;;

		esac

##########################
#
# Input Tray Selection	(Default=AutoSelect)
#
##########################

		case "$itray" in

		auto )
			(
			echo " /XJXsettraysel where
			   { pop 7 XJXsettraysel } if" >> $device_tmp
			printf "\033&l7H" >> $pcl_tmp
			)
			;;
		tray1 )
			(
			echo " /XJXsettraysel where
			   { pop 1 XJXsettraysel } if" >> $device_tmp
			printf "\033&l1H" >> $pcl_tmp
			)
			;;
		tray2 )
			(
			echo " /XJXsettraysel where
			   { pop 4 XJXsettraysel } if" >> $device_tmp
			printf "\033&l4H" >> $pcl_tmp
			)
			;;
		tray3 )
			(
			echo " /XJXsettraysel where
			   { pop 5 XJXsettraysel } if" >> $device_tmp
			printf "\033&l5H" >> $pcl_tmp
			)
			;;
		tray4 )
			(
			echo " /XJXsettraysel where
			   { pop 20 XJXsettraysel } if" >> $device_tmp
			printf "\033&l20H" >> $pcl_tmp
			)
			;;
		lct )
			(
			echo " /XJXsettraysel where
			   { pop 21 XJXsettraysel } if" >> $device_tmp
			printf "\033&l21H" >> $pcl_tmp
			)
			;;
		bypass )
			(
			echo " /XJXsettraysel where
			   { pop 8 XJXsettraysel } if" >> $device_tmp
			printf "\033&l8H" >> $pcl_tmp
			)
			;;
		* )
			printf "\033&l7H" >> $pcl_tmp
			;;

		esac

##########################
#
# First Page Tray (Default=Tray1)
#
##########################

		case "$slipsheettray" in

		tray1 )
			(
			echo "/XJXsetFirstPageTray where { pop 1 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=1" >> $pjl_tmp
			)
			;;

		tray2 )
			(
			echo "/XJXsetFirstPageTray where { pop 4 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=4" >> $pjl_tmp
			)
			;;

		tray3 )
			(
			echo "/XJXsetFirstPageTray where { pop 5 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=5" >> $pjl_tmp
			)
			;;

		tray4 )
			(
			echo "/XJXsetFirstPageTray where { pop 20 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=20" >> $pjl_tmp
			)
			;;

		lct )
			(
			echo "/XJXsetFirstPageTray where { pop 21 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=21" >> $pjl_tmp
			)
			;;

		bypass )
			(
			echo "/XJXsetFirstPageTray where { pop 8 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=8" >> $pjl_tmp
			)
			;;

		* )
			(
			echo "/XJXsetFirstPageTray where { pop 1 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=1" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Rotate180 (Default=No)
#
##########################

		case "$rotate180" in

		no )
			(
			echo "userdict /EFUserRotate180 known
			   { 0 EFUserRotate180 } if" >> $device_tmp
			)
			;;

		yes )
			(
			echo "userdict /EFUserRotate180 known
			   { 1 EFUserRotate180 } if" >> $device_tmp
			)
			;;

		* )
			(
			echo "userdict /EFUserRotate180 known
			   { 0 EFUserRotate180 } if" >> $device_tmp
			)
			;;

		esac

##########################
#
# Duplex (Default=OFF)
#
##########################

		case "$duplex" in

		off )
			(
			echo "false XJXsetduplex
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if " >> $device_tmp
			echo "@PJL SET DUPLEX=OFF" >> $pjl_tmp
			)
			;;
		side )
			(
			echo "true XJXsetduplex
			   << /Tumble false >> setpagedevice
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if " >> $device_tmp
			)
				echo "@PJL SET DUPLEX=ON" >> $pjl_tmp
			case "$landscape" in
			yes )
				echo "@PJL SET BINDING=SHORTEDGE" >> $pjl_tmp
				;;
			* )
				echo "@PJL SET BINDING=LONGEDGE" >> $pjl_tmp
				;;
			esac
			;;
		top )
			(
			echo "true XJXsetduplex
			   << /Tumble true >> setpagedevice
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if" >> $device_tmp
			)
				echo "@PJL SET DUPLEX=ON" >> $pjl_tmp
			case "$landscape" in
			yes )
				echo "@PJL SET BINDING=LONGEDGE" >> $pjl_tmp
				;;
			* )
				echo "@PJL SET BINDING=SHORTEDGE" >> $pjl_tmp
				;;
			esac
			;;
		* )
			(
			echo "false XJXsetduplex
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if " >> $device_tmp
			echo "@PJL SET DUPLEX=OFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
#  Output Bin (Default=PrinterDefault)
#
##########################

		case "$outbin" in

		default )
			(
			echo " /XJXsetOutputBin where { pop 0 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		standard )
			(
			echo " /XJXsetOutputBin where { pop 1 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=UPPER" >> $pjl_tmp
			)
			;;
		external )
			(
			echo " /XJXsetOutputBin where { pop 2 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=LOWER" >> $pjl_tmp
			)
			;;
		mbin1 )
			(
			echo " /XJXsetOutputBin where { pop 5 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTBIN2" >> $pjl_tmp
			)
			;;
		mbin2 )
			(
			echo " /XJXsetOutputBin where { pop 6 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTBIN3" >> $pjl_tmp
			)
			;;
		mbin3 )
			(
			echo " /XJXsetOutputBin where { pop 7 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTBIN4" >> $pjl_tmp
			)
			;;
		mbin4 )
			(
			echo " /XJXsetOutputBin where { pop 8 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTBIN5" >> $pjl_tmp
			)
			;;
		shift1 )
			(
			echo " /XJXsetOutputBin where { pop 9 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTBIN6" >> $pjl_tmp
			)
			;;
		shift2 )
			(
			echo " /XJXsetOutputBin where { pop 10 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTBIN7" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetOutputBin where { pop 0 XJXsetOutputBin } if " >> $device_tmp
			)
			;;

		esac

##########################
#
# Staple (Default=OFF)
#
##########################

		case "$staple" in

		off )
			(
			echo " userdict /XJXsetstapler known
			   { 0 XJXsetstapler } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=GENERICOFF" >> $pjl_tmp
			)
			;;
		lefts )
			(
			echo " /XJXsetstaple where {pop 8 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPLEFT_SLANT" >> $pjl_tmp
			)
			;;
		leftv )
			(
			echo " /XJXsetstaple where {pop 6 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPLEFT_VER" >> $pjl_tmp
			)
			;;

		lefth )
			(
			echo " /XJXsetstaple where {pop 1 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPLEFT_HOR" >> $pjl_tmp
			)
			;;
		left2 )
			(
			echo " /XJXsetstaple where {pop 3 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=TWOLEFT" >> $pjl_tmp
			)
			;;
		top2 )
			(
			echo " /XJXsetstaple where {pop 5 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=TWOUP" >> $pjl_tmp
			)
			;;
		rights )
			(
			echo " /XJXsetstaple where {pop 9 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPRIGHT_SLANT" >> $pjl_tmp
			)
			;;
		rightv )
			(
			echo " /XJXsetstaple where {pop 7 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPRIGHT_VER" >> $pjl_tmp
			)
			;;
		righth )
			(
			echo " /XJXsetstaple where {pop 2 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPRIGHT_HOR" >> $pjl_tmp
			)
			;;
		right2 )
			(
			echo " /XJXsetstaple where {pop 4 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=TWORIGHT" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " userdict /XJXsetstapler known
			   { 0 XJXsetstapler } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=GENERICOFF" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Punch (Default=Off)
#
##########################

		case "$punch" in

		off )
			(
			echo " /XJXsetpunch where { pop 0 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=GENERICOFF" >> $pjl_tmp
			)
			;;
		left )
			(
			echo " /XJXsetpunch where { pop 1 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=LEFT" >> $pjl_tmp
			)
			;;
		top )
			(
			echo " /XJXsetpunch where { pop 2 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=TOP" >> $pjl_tmp
			)
			;;
		right )
			(
			echo " /XJXsetpunch where { pop 3 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=RIGHT" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetpunch where { pop 0 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=GENERICOFF" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Two Hole (Default=False)
#
##########################

		case "$twohole" in

		false )
			;;

		true )
			(
			echo "userdict /XJXsetpunchholes known
			   { 2 XJXsetpunchholes }
			   { (printerinfo punchholes 2) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET NUMPUNCHES=2" >> $pjl_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# Collate (Default=off)
#
##########################

		case "$collate" in

		off )
			(
			echo "/XJXsetcollate where { pop false XJXsetcollate } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SORTERMODE=GENERICOFF" >> $pjl_tmp
			)
			;;
		on )
			(
			echo "/XJXsetcollate where { pop true XJXsetcollate } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SORTERMODE=COLLATE" >> $pjl_tmp
			)
			;;
		* )
			(
			echo "/XJXsetcollate where { pop false XJXsetcollate } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SORTERMODE=GENERICOFF" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Job Type (Default=Normal)
#
##########################

		case "$jobtype" in

		normal )
			;;
		check )
			(
			echo "(printerinfo proofnprint on) =string
			   FieryXJdict /ExtCmdGetExec get exec {pop} if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SORTERMODE=GENERICOFF" >> $pjl_tmp
			echo "@PJL COMMENT EFIPJL SET HOLDJOB=GENERICON" >> $pjl_tmp
			)
			;;
		secure )
			(
			echo "/XJXsetcollate where { pop false XJXsetcollate } if
			   (printerinfo secureprint on) =string
			   FieryXJdict /ExtCmdGetExec get exec {pop} if" >> $device_tmp
			echo "/XJXsetUserID where { pop ($userid) XJXsetUserID} if" >> $device_tmp
			echo "/XJXsetPassWordUse where { pop ($password) XJXsetPassWordUse} if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SORTERMODE=GENERICOFF" >> $pjl_tmp
			echo "@PJL COMMENT EFIPJL SET SECUREPRINT=GENERICON" >> $pjl_tmp
			echo "@PJL COMMENT EFIPJL SET USERID=\"$userid\"" >> $pjl_tmp
			echo "@PJL COMMENT EFIPJL SET COPIER_PASSWD_STR=\"$password\"" >> $pjl_tmp
			)
			;;
		* )
			;;
		esac

##########################
#
# Media Type (Default=Plain)
#
##########################

		case "$mediatype" in

		plain )
			(
			echo " userdict /XJXsetmediatype known { 0 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=PAPER" >> $pjl_tmp
			)
			;;
		recycled )
			(
			echo " userdict /XJXsetmediatype known
			   { 105 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=RECYCLED" >> $pjl_tmp
			)
			;;
		special )
			(
			echo " userdict /XJXsetmediatype known
			   { 2 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=SPECIAL" >> $pjl_tmp
			)
			;;
		color )
			(
			echo " userdict /XJXsetmediatype known
			   { 106 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=COLOR" >> $pjl_tmp
			)
			;;
		letterhead )
			(
			echo " userdict /XJXsetmediatype known
			   { 102 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=LETTERHEAD" >> $pjl_tmp
			)
			;;
		preprinted )
			(
			echo " userdict /XJXsetmediatype known
			   { 101 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=PREPRINTED" >> $pjl_tmp
			)
			;;
		prepunched )
			(
			echo " userdict /XJXsetmediatype known
			   { 103 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=PREPUNCHED" >> $pjl_tmp
			)
			;;
		bond )
			(
			echo " userdict /XJXsetmediatype known
			   { 1 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=BOND" >> $pjl_tmp
			)
			;;
		labels )
			(
			echo " userdict /XJXsetmediatype known
			   { 104 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=LABELS" >> $pjl_tmp
			)
			;;
		transparency )
			(
			echo " userdict /XJXsetmediatype known
			   { 4 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=TRANSPARENCY" >> $pjl_tmp
			)
			;;
		thick )
			(
			echo " userdict /XJXsetmediatype known
			   { 108 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=HEAVY" >> $pjl_tmp
			)
			;;
		cardstock)
			(
			echo " userdict /XJXsetmediatype known
			   { 107 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=CARDSTOCK" >> $pjl_tmp
			)
			;;
		duplexplain )
			(
			echo " userdict /XJXsetmediatype known
			   { 120 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=2NDSIDE" >> $pjl_tmp
			)
			;;
		duplexthick )
			(
			echo " userdict /XJXsetmediatype known
			   { 169 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=HEAVY2NDSIDE" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " userdict /XJXsetmediatype known { 0 XJXsetmediatype } if" >> $device_tmp
			echo "@PJL SET MEDIATYPE=PAPER" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Color Rendering (Default=Printer's Default)
#
##########################

		case "$rendering" in

		photographic )
			(
			echo "userdict /XJXsetrenderingintent known
			   { (Photographic) XJXsetrenderingintent } if" >> $device_tmp
			)
			;;

		presentation )
			(
			echo "userdict /XJXsetrenderingintent known
			   { (Presentation) XJXsetrenderingintent } if" >> $device_tmp
			)
			;;

		absolute )
			(
			echo "userdict /XJXsetrenderingintent known
			   { (Absolute) XJXsetrenderingintent } if" >> $device_tmp
			)
			;;

		relative )
			(
			echo "userdict /XJXsetrenderingintent known
			   { (Relative) XJXsetrenderingintent } if" >> $device_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# Brightness (Default=Normal)
#
##########################

		case "$brightness" in

		lightest )
			(
			echo "/XJXsetBrightness where
			   { pop (LIGHTEST) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		lighter )
			(
			echo "/XJXsetBrightness where
			   { pop (LIGHTER) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		light )
			(
			echo "/XJXsetBrightness where
			   { pop (LIGHT) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		normal )
			(
			echo "/XJXsetBrightness where
			   { pop (NORMAL) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		dark )
			(
			echo "/XJXsetBrightness where
			   { pop (DARK) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		darker )
			(
			echo "/XJXsetBrightness where
			   { pop (DARKER) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		darkest )
			(
			echo "/XJXsetBrightness where
			   { pop (DARKEST) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		* )
			(
			echo "/XJXsetBrightness where
			   { pop (NORMAL) XJXsetBrightness } if" >> $device_tmp
			)
			;;

		esac

##########################
#
# RGB Source Profile (Default=Printer's Default)
#
##########################

		case "$rgb" in

		none )
			(
			echo "/XJXsetRGBOverride where
			   { pop (Off) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		pc )
			(
			echo "/XJXsetRGBOverride where
			   { pop (SRGB) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		efi )
			(
			echo "/XJXsetRGBOverride where
			   { pop (EFIRGB) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		apple )
			(
			echo "/XJXsetRGBOverride where
			   { pop (APPLE13) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		other )
			(
			echo "/XJXsetRGBOverride where
			   { pop (OTHER) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source1 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB1) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source2 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB2) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source3 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB3) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source4 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB4) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source5 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB5) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source6 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB6) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source7 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB7) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source8 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB8) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source9 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB9) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		source10 )
			(
			echo "/XJXsetRGBOverride where
			   { pop (RGB10) XJXsetRGBOverride } if" >> $device_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# Gamma (Default=Printer's Default)
#
##########################

		case "$gamma" in

		10 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (1.0) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		12 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (1.2) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		14 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (1.4) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		16 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (1.6) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		18 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (1.8) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		20 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (2.0) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		22 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (2.2) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		24 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (2.4) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		26 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (2.6) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		28 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (2.8) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		30 )
			(
			echo "/XJXsetRGBOtherGamma where
			   { pop (3.0) XJXsetRGBOtherGamma } if" >> $device_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# White Point (Default=Printer's Default)
#
##########################

		case "$whitepoint" in

		5000k )
			(
			echo "/XJXsetRGBOtherWtPt where
			   { pop (5000K) XJXsetRGBOtherWtPt } if" >> $device_tmp
			)
			;;

		5500k )
			(
			echo "/XJXsetRGBOtherWtPt where
			   { pop (5500K) XJXsetRGBOtherWtPt } if" >> $device_tmp
			)
			;;

		6500k )
			(
			echo "/XJXsetRGBOtherWtPt where
			   { pop (6500K) XJXsetRGBOtherWtPt } if" >> $device_tmp
			)
			;;

		7500k )
			(
			echo "/XJXsetRGBOtherWtPt where
			   { pop (7500K) XJXsetRGBOtherWtPt } if" >> $device_tmp
			)
			;;

		9300k )
			(
			echo "/XJXsetRGBOtherWtPt where
			   { pop (9300K) XJXsetRGBOtherWtPt } if" >> $device_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# Phosphors (Default=Printer's Default)
#
##########################

		case "$phosphors" in

		hitachi )
			(
			echo "/XJXsetRGBOtherPhos where
			   { pop (Hitachi EBU) XJXsetRGBOtherPhos } if" >> $device_tmp
			)
			;;

		ikegami )
			(
			echo "/XJXsetRGBOtherPhos where
			   { pop (Hitachi/Ikegami) XJXsetRGBOtherPhos } if" >> $device_tmp
			)
			;;

		ntsc )
			(
			echo "/XJXsetRGBOtherPhos where
			   { pop (NTSC) XJXsetRGBOtherPhos } if" >> $device_tmp
			)
			;;

		pivot )
			(
			echo "/XJXsetRGBOtherPhos where
			   { pop (Radius Pivot) XJXsetRGBOtherPhos } if" >> $device_tmp
			)
			;;

		smpte )
			(
			echo "/XJXsetRGBOtherPhos where
			   { pop (SMPTE) XJXsetRGBOtherPhos } if" >> $device_tmp
			)
			;;

		trinitron )
			(
			echo "/XJXsetRGBOtherPhos where
			   { pop (Trinitron) XJXsetRGBOtherPhos } if" >> $device_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# RGB Separation (Default=Printer's default)
#
##########################

		case "$separation" in

		output )
			(
			echo "/XJXsetRGBSeparation where
			   { pop (False) XJXsetRGBSeparation } if" >> $device_tmp
			)
			;;
		simulation )
			(
			echo "/XJXsetRGBSeparation where
			   { pop (True) XJXsetRGBSeparation } if" >> $device_tmp
			)
			;;
		* )
			;;

		esac

##########################
#
# Simulation Profile (Default=Printer's Default)
#
##########################

		case "$simulation" in

		none )
			(
			echo "/XJXsetSimulation where
			   { pop (.None) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		coated )
			(
			echo "/XJXsetSimulation where
			   { pop (SWOP-Coated) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		dic )
			(
			echo "/XJXsetSimulation where
			   { pop (DIC) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		euroscale )
			(
			echo "/XJXsetSimulation where
			   { pop (Euroscale) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom1 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-1) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		costum2 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-2) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom3 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-3) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom4 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-4) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom5 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-5) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom6 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-6) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom7 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-7) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom8 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-8) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom9 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-9) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		custom10 )
			(
			echo "/XJXsetSimulation where
			   { pop (Custom-10) XJXsetSimulation } if" >> $device_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# Simulation Speed (Default=OFF)
#
##########################

		case "$simspeed" in

		full )
			(
			echo "/XJXsetSimSpeed where
			   { pop (Full) XJXsetSimSpeed } if" >> $device_tmp
			)
			;;
		quick )
			(
			echo "/XJXsetSimSpeed where
			   { pop (Quick) XJXsetSimSpeed } if" >> $device_tmp
			)
			;;
		* )
			;;

		esac

##########################
#
# Output Profile (Default=Printer's Default)
#
##########################

		case "$outprofile" in

		600 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (ICC3COAX) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		1200 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (ICC3COBX) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out1 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT1) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out2 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT2) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out3 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT3) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out4 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT4) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out5 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT5) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out6 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT6) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out7 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT7) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out8 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT8) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out9 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT9) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		out10 )
			(
			echo "/XJXsetOutputProfile where
			   { pop (OUT10) XJXsetOutputProfile } if" >> $device_tmp
			)
			;;

		* )
			;;

		esac

##########################
#
# Pure Black Text/Graphics (Default=Printer's default)
#
##########################

		case "$pureblack" in

		off )
			(
			echo "/XJXsetPureBlack where
			   { pop (False) XJXsetPureBlack } if" >> $device_tmp
			)
			;;
		on )
			(
			echo "/XJXsetPureBlack where
			   { pop (True) XJXsetPureBlack } if" >> $device_tmp
			)
			;;
		* )
			;;

		esac

##########################
#
# Black Overprint (Default=Printer's default)
#
##########################

		case "$blackoverprint" in

		off )
			(
			echo "/XJXsetBlkOverprint where
			   { pop (False) XJXsetBlkOverprint } if" >> $device_tmp
			)
			;;
		on )
			(
			echo "/XJXsetBlkOverprint where
			   { pop (True) XJXsetBlkOverprint } if" >> $device_tmp
			)
			;;
		* )
			;;

		esac

##########################
#
# Spot Color Matching (Default=Printer's default)
#
##########################

		case "$spotcolor" in

		off )
			(
			echo "/XJXsetSpotColors where
			   { pop (False) XJXsetSpotColors } if" >> $device_tmp
			)
			;;
		on )
			(
			echo "/XJXsetSpotColors where
			   { pop (True) XJXsetSpotColors } if" >> $device_tmp
			)
			;;
		* )
			;;

		esac

##########################
#
# Paper Size (Default=Letter)
#
##########################

		case "$paper" in

		8x13 )
			(
			echo "/XJXsetpagesize where { pop (SEFF4) XJXsetpagesize }
			   { << /PageSize [576 936] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l12a" >> $pcl_tmp
			)
			;;
		825x13 )
			(
			echo "/XJXsetpagesize where { pop (SEFFolio) XJXsetpagesize }
			   { << /PageSize [595 935] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l13a" >> $pcl_tmp
			)
			;;
		85x13 )
			(
			echo "/XJXsetpagesize where { pop (SEFFanFoldGermanLegal) XJXsetpagesize }
			   { << /PageSize [612 936] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l14a" >> $pcl_tmp
			)
			;;
		A3 )
			(
			echo "/XJXsetpagesize where { pop (A3) XJXsetpagesize }
			   { << /PageSize [842 1191] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l27a" >> $pcl_tmp
			)
			;;
		A4 )
			(
			echo "/XJXsetpagesize where { pop (A4) XJXsetpagesize }
			   { << /PageSize [595 842] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l26a" >> $pcl_tmp
			)
			;;
		A5 )
			(
			echo "/XJXsetpagesize where { pop (A5) XJXsetpagesize }
			   { << /PageSize [420 595] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l25a" >> $pcl_tmp
			)
			;;
		A6 )
			(
			echo "/XJXsetpagesize where { pop (A6) XJXsetpagesize }
			   { << /PageSize [297 420] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l1024a" >> $pcl_tmp
			)
			;;
		B4 )
			(
			echo "/XJXsetpagesize where { pop (B4) XJXsetpagesize }
			   { << /PageSize [729 1032] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l46a" >> $pcl_tmp
			)
			;;
		B5 )
			(
			echo "/XJXsetpagesize where { pop (B5) XJXsetpagesize }
			   { << /PageSize [516 729] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l45a" >> $pcl_tmp
			)
			;;
		env-c5 )
			(
			echo "/XJXsetpagesize where { pop (EnvC5) XJXsetpagesize }
			   { << /PageSize [459 649] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l91a" >> $pcl_tmp
			)
			;;
		env-c6 )
			(
			echo "/XJXsetpagesize where { pop (EnvC6) XJXsetpagesize }
			   { << /PageSize [323 459] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l772a" >> $pcl_tmp
			)
			;;
		envelope )
			(
			echo "/XJXsetpagesize where { pop (Env10) XJXsetpagesize }
			   { << /PageSize [297 684] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l81a" >> $pcl_tmp
			)
			;;
		env-dl )
			(
			echo "/XJXsetpagesize where { pop (EnvDL) XJXsetpagesize }
			   { << /PageSize [312 624] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l90a" >> $pcl_tmp
			)
			;;
		executive )
			(
			echo "/XJXsetpagesize where { pop (SEFExecutive) XJXsetpagesize }
			   { << /PageSize [522 757] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l1a" >> $pcl_tmp
			)
			;;
		legal )
			(
			echo "/XJXsetpagesize where { pop (Legal) XJXsetpagesize }
			   { << /PageSize [612 1008] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l3a" >> $pcl_tmp
			)
			;;
		letter )
			(
			echo "/XJXsetpagesize where { pop (Letter) XJXsetpagesize }
			   { << /PageSize [612 792] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l2a" >> $pcl_tmp
			)
			;;
		env-monarch )
			(
			echo "/XJXsetpagesize where { pop (EnvMonarch) XJXsetpagesize }
			   { << /PageSize [279 540] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l80a" >> $pcl_tmp
			)
			;;
		halfletter )
			(
			echo "/XJXsetpagesize where { pop (Statement) XJXsetpagesize }
			   { << /PageSize [396 612] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l11a" >> $pcl_tmp
			)
			;;
		tabloid )
			(
			echo "/XJXsetpagesize where { pop (Tabloid) XJXsetpagesize }
			   { << /PageSize [793 1224] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l6a" >> $pcl_tmp
			)
			;;
		8k )
			(
			echo "/XJXsetpagesize where { pop (R8KSEF) XJXsetpagesize }
			   { << /PageSize [757 1106] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l819a" >> $pcl_tmp
			)
			;;
		16k )
			(
			echo "/XJXsetpagesize where { pop (R16K) XJXsetpagesize }
			   { << /PageSize [553 758] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l817a" >> $pcl_tmp
			)
			;;
		* )
			(
			echo "/XJXsetpagesize where { pop (Letter) XJXsetpagesize }
			   { << /PageSize [612 792] >> setpagedevice } ifelse" >> $device_tmp
			printf "\033&l2a" >> $pcl_tmp
			)
			;;

		esac

##########################
# LNX_TUB_E
#
# Print the files
#
##########################

for i in $*
do

##########################
#
# Auto-detect input file type here
#
# If is_PS=1, input file is PostScript
# If is_TXT=1, input file is text
# else, input file is PCL/PJL
# is_PS is overwritten, if user specified
#	input file type with "-o text" or "-o ps" option
#
##########################

if [ -n "$PS" ]
then
	is_PS=1
	is_TXT=0
elif [ -n "$TXT" ]
then
	is_TXT=1
	is_PS=0
elif [ -n "$PCL" ]
then
	is_PS=0;
	is_TXT=0;
else

	OS_NAME=`uname -s`

	case $OS_NAME in
	SunOS)
		is_PS=`file $i | grep -c PostScript`
		;;
	HP-UX)
		is_PS=`file $i | grep  -c postscript`
		;;
	*)
		is_PS=`file $i | grep -c PostScript`
		;;
	esac
		file $i | egrep -i 'ASCII|text|script' > /dev/null
		is_TXT=`expr "$?" = 0`
fi

if [ "$is_PS" -eq 1 ]
then

	##########################
	#
	# Check if the input file has %%EndSetup string in it
	#
	##########################

	is_DSC=`cat $i | grep -c "%EndSetup"`

	##########################
	#
	# Check if is_DSC variable is empty
	#
	##########################

	case $is_DSC in

	1)

		if grep '^setup' $i > /dev/null
		then
			csplit -f $ps_tmp $i "/^setup/"
		else
			csplit -f $ps_tmp $i "/%%EndSetup/"
		fi
		cat $device_tmp >> $ps_tmp"00"
		cat $ps_tmp"00" $ps_tmp"01" > $prtout_tmp

		;;
	*)
		echo "%!PS-Adobe-3.0" > $prtout_tmp
		echo "%%BeginSetup" >> $prtout_tmp
		cat $device_tmp >> $prtout_tmp
		echo "%%EndSetup" >> $prtout_tmp
		cat $i >> $prtout_tmp
		;;
	esac

elif [ "$is_TXT" -eq 1 ]
then

	##########################
	#
	# If text file, apply a filter to remove stair case effect
	#
	##########################

	printf "\033%%-12345X" > $prtout_tmp

	echo "@PJL SET JOB NAME=\"$title\"" >> $prtout_tmp

	cat $pjl_tmp >> $prtout_tmp

	echo "@PJL ENTER LANGUAGE=PCL" >> $prtout_tmp

	printf "\033E" >> $prtout_tmp

	cat $pcl_tmp >> $prtout_tmp; echo >> $prtout_tmp

	cat $i | awk '{print $0, "\r"}' >> $prtout_tmp

	printf "\033%%-12345X" >> $prtout_tmp

else

	##########################
	#
	# If PCL file, send to print directly
	#
	##########################

	cat $i > $prtout_tmp

fi

##########################
#
# Loop for printing
#
##########################

if [ "$nobanner" = "yes" ]
then
	case `uname -s` in
	HP-UX)
	        cat $prtout_tmp | lp -d $REMOTE_PRINTER -o nb
		;;
	*)
	        cat $prtout_tmp | lp -d $REMOTE_PRINTER -o nobanner
		;;
	esac
else
        cat $prtout_tmp | lp -d $REMOTE_PRINTER
fi

done

##########################
#
# Remove temporary files if DEBUG=empty
#
##########################

if test -z "$DEBUG"
then
	if test -f "$device_tmp"
	then
		/usr/bin/rm $device_tmp
	fi

	if test -f "$prtout_tmp"
	then
		/usr/bin/rm $prtout_tmp
	fi

	if test -f $ps_tmp"00"
	then
		/usr/bin/rm $ps_tmp"00"
	fi

	if test -f $ps_tmp"01"
	then
		/usr/bin/rm $ps_tmp"01"
	fi

	if test -f $pjl_tmp
	then
		/usr/bin/rm $pjl_tmp
	fi

	if test -f $pcl_tmp
	then
		/usr/bin/rm $pcl_tmp
	fi

fi

exit_code=0 exit 0
