#!/bin/sh
##########################
##
## Aficio 551/700/1055 Type 700 and 1055
## Unix Printing Interface Program.
## Last Update 12/09/2004
## Copyright 1999-2004
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## Supported products
##
## Aficio 551/700/1055 Type 700
##
##########################

##########################
##
## Update History
##
## 6/10/2002 Add 1055 Support
## 5/30/2002 Add Text File support
## 9/25/2000 Minor corrections
## 6/20/2000 Default Paper Size Commented Out
##                Paper Size Selection Corrected
##
##########################

##########################
##
## Debug Flag, set to Y for debugging
##
##########################

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=
pjl_tmp=
pcl_tmp=
TXT=
PS=
PCL=
is_TXT=
is_PS=
is_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
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 )
		nobanner="yes"
		;;

	nofilebreak )
		nofilebreak="yes"
		;;

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

	debug )
		DEBUG=Y
		;;

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

	text )
		TXT=1
		;;

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

	ps )
		PS=1
		;;

##########################
#
# PCL/PJL option
#
##########################

	pcl|nofilter )
		PCL=1
		;;

##########################
# LNX_TUB_B
#
# Paper Orientation (Default=Portrait)
#
##########################
        portrait  )
		printf "\033&l0o" >> $pcl_tmp
		;;

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

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

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

##########################
#
# 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}`
		;;

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

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

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

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

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

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

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

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

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

	idiom=* )
		idiom=`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"
	if [ -z "$outbin" ]
	then
		outbin="shift"
	fi
fi

if [ "$separate" = "on" ]
then
	collate="on"
	if [ -z "$outbin" ]
	then
		outbin="shift"
	fi
fi

##############################################################
#
# The order below is somehow important:
#
#	paper have to be 1st
#
##############################################################

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

		case "$outbin" in
		itray )
			(
			echo " <</OutputType (Tray1)>> setpagedevice "
			echo "@PJL SET OUTBIN=UPPER" >> $pjl_tmp
			 >> $device_tmp
			)
			;;

		etray )
			(
			echo "<</OutputType  (Inner Joint Unit)
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET OUTBIN=UPPER" >> $pjl_tmp
			)
			;;
		main|proof )
			(
			echo "<</OutputType (Finisher Main Tray)
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET OUTBIN=FINISHERPROOF" >> $pjl_tmp
			)
			;;
		shift )
			(
			echo "<</OutputType (Finisher Shift Tray)
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET OUTBIN=FINISHERSHIFT" >> $pjl_tmp
			)
			;;
		bin7 )
			(
			echo "<</OutputType (Mailbox Main Tray)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN1" >> $pjl_tmp
			)
			;;
		mbin1 )
			(
			echo "<</OutputType (Mailbox Bin 1)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN2" >> $pjl_tmp
			)
			;;
		mbin2 )
			(
			echo "<</OutputType (Mailbox Bin 2)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN3" >> $pjl_tmp
			)
			;;
		mbin3 )
			(
			echo "<</OutputType (Mailbox Bin 3)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN4" >> $pjl_tmp
			)
			;;
		mbin4 )
			(
			echo "<</OutputType (Mailbox Bin 4)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN5" >> $pjl_tmp
			)
			;;
		mbin5 )
			(
			echo "<</OutputType (Mailbox Bin 5)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN6" >> $pjl_tmp
			)
			;;
		mbin6 )
			(
			echo "<</OutputType (Mailbox Bin 6)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN7" >> $pjl_tmp
			)
			;;
		mbin7 )
			(
			echo "<</OutputType (Mailbox Bin 7)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN8" >> $pjl_tmp
			)
			;;
		mbin8 )
			(
			echo "<</OutputType (Mailbox Bin 8)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN9" >> $pjl_tmp
			)
			;;
		mbin9 )
			(
			echo "<</OutputType (Mailbox Bin 9)
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN10" >> $pjl_tmp
			)
			;;
		booklet )
			(
			echo "<</OutputType (Booklet Finisher Stack)>> setpagedevice" >> $device_tmp
			echo "@PJL SET OUTBIN=FINISHERBOOKLET" >> $pjl_tmp
			)
			;;
		* )
			;;

		esac

##########################
#
#  Edge Smoothing (Default=False)
#
##########################

		case "$smoothing" in
		off )
			(
			echo "<< /PostRenderingEnhance currentpagedevice
			 /PostRenderingEnhanceDetails get /TonerSaver get 0 ne
			 /PostRenderingEnhanceDetails  <</Type 18 /REValue 0
			 /TonerSaver currentpagedevice /PostRenderingEnhanceDetails get
			 /TonerSaver get >>
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET SMOOTHING=OFF" >> $pjl_tmp
			)
			;;
		on )
			(
			echo "<</PostRenderingEnhance true /PostRenderingEnhanceDetails
 			 <</Type 18 /REValue 1 /TonerSaver currentpagedevice
			 /PostRenderingEnhanceDetails get /TonerSaver get >>
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET SMOOTHING=ON" >> $pjl_tmp
			)
			;;
		* )
			(
			echo "<< /PostRenderingEnhance currentpagedevice
			 /PostRenderingEnhanceDetails get /TonerSaver get 0 ne
			 /PostRenderingEnhanceDetails  <</Type 18 /REValue 0
			 /TonerSaver currentpagedevice /PostRenderingEnhanceDetails get
			 /TonerSaver get >>
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET SMOOTHING=OFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
# Input Tray Selection	(Default=Automatically Select)
#
##########################

		case "$itray" in

		LCT )
			(
			echo "<</ManualFeed false /MediaPosition 8
			 >> setpagedevice " >> $device_tmp
			printf "\033&l5H" >> $pcl_tmp
			)
			;;

		tray1 )
			(
			echo "<</ManualFeed false /MediaPosition 0
			 >> setpagedevice " >> $device_tmp
			printf "\033&l8H" >> $pcl_tmp
			)
			;;

		tray2 )
			(
			echo "<</ManualFeed false /MediaPosition 1
			 >> setpagedevice " >> $device_tmp
			printf "\033&l1H" >> $pcl_tmp
			)
			;;

		tray3 )
			(
			echo "<</ManualFeed false /MediaPosition 2
			 >> setpagedevice " >> $device_tmp
			printf "\033&l4H" >> $pcl_tmp
			)
			;;

		tray4 )
			(
			echo "<</ManualFeed false /MediaPosition 3
			 >> setpagedevice " >> $device_tmp
			printf "\033&l30H" >> $pcl_tmp
			)
			;;

		bypass )
			(
			echo "<< /ManualFeed true >> setpagedevice " >> $device_tmp
			printf "\033&l2H" >> $pcl_tmp
			)
			;;

		* )
			(
			printf "\033&l7H" >> $pcl_tmp
			)
			;;

		esac

##########################
#
# Auto Tray Switching (Default=ON)
#
##########################

		case "$trayswitch" in

		on )
			(
			echo "<</TraySwitch true>>setpagedevice" >> $device_tmp
			echo "@PJL SET AUTOTRAYCHANGE=ON" >> $pjl_tmp
			)
			;;

		off )
			(
			echo "<</TraySwitch false>>setpagedevice" >> $device_tmp
			echo "@PJL SET AUTOTRAYCHANGE=OFF" >> $pjl_tmp
			)
			;;

		* )
			(
			echo "<</TraySwitch true>>setpagedevice" >> $device_tmp
			echo "@PJL SET AUTOTRAYCHANGE=ON" >> $pjl_tmp
			)
			;;

		esac

##########################
#
# Punch (Default=OFF)
#
##########################

		case "$punch" in
		off )
			(
			echo "<</Punch 0/PunchDetails
			 <</Type 1 /Position 0 >>
			 >> setpagedevice">> $device_tmp
			echo "@PJL SET PUNCH=OFF" >> $pjl_tmp
			)
			;;
		llandscape )
			(
			echo "currentpagedevice /OutputType get (Finisher Main Tray) eq
                        {<< /OutputType (Finisher Main Tray)>> setpagedevice}
                        {<< /OutputType (Finisher Shift Tray)>> setpagedevice}ifelse
                        << /Punch 2 /PunchDetails <</Type 1 /Position 2 >>
                        >> setpagedevice" >> $device_tmp
			echo "@PJL SET PUNCH=LEFTLAND" >> $pjl_tmp
			)
			;;
		lportrait )
			(
			echo "currentpagedevice /OutputType get (Finisher Main Tray) eq
			 {<< /OutputType (Finisher Main Tray)>> setpagedevice}
 			 {<< /OutputType (Finisher Shift Tray)>> setpagedevice}ifelse
			 << /Punch 2 /PunchDetails <</Type 1 /Position 1 >>
  			>> setpagedevice" >> $device_tmp
			echo "@PJL SET PUNCH=LEFTPORT" >> $pjl_tmp
			)
			;;

		tlandscape )
			(
			echo " currentpagedevice /OutputType get (Finisher Main Tray) eq
			 {<< /OutputType (Finisher Main Tray)>> setpagedevice}
			 {<< /OutputType (Finisher Shift Tray)>> setpagedevice}ifelse
			 << /Punch 2 /PunchDetails <</Type 1 /Position 6 >>
			 >>setpagedevice" >> $device_tmp
			echo "@PJL SET PUNCH=TOPLAND" >> $pjl_tmp
			)
			;;
		tportrait )
			(
			echo " currentpagedevice /OutputType get (Finisher Main Tray) eq
			 {<< /OutputType (Finisher Main Tray)>> setpagedevice}
			 {<< /OutputType (Finisher Shift Tray)>> setpagedevice}ifelse
			 << /Punch 2 /PunchDetails <</Type 1 /Position 5 >>
			 >>setpagedevice" >> $device_tmp
			echo "@PJL SET PUNCH=TOPPORT" >> $pjl_tmp
			)
			;;
		* )
			(
			echo "<</Punch 0/PunchDetails
			 <</Type 1 /Position 0 >>
			 >> setpagedevice">> $device_tmp
			echo "@PJL SET PUNCH=OFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
# Toner Saver (Default=False)
#
##########################

		case "$toner" in
		off )
			(
			echo "<< /PostRenderingEnhance currentpagedevice
			 /PostRenderingEnhanceDetails get /REValue get 0 ne
			 /PostRenderingEnhanceDetails  <</Type 18 /TonerSaver 0
 			 /REValue currentpagedevice /PostRenderingEnhanceDetails
			 get /REValue get >>
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET ECONOMODE=OFF" >> $pjl_tmp
			)
			;;
		on )
			(
			echo "<</PostRenderingEnhance true /PostRenderingEnhanceDetails
 			 <</Type 18 /TonerSaver 1 /REValue currentpagedevice
 			 /PostRenderingEnhanceDetails get/REValue get >>
 			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET ECONOMODE=ON" >> $pjl_tmp
			)
			;;
		* )
			(
			echo "<< /PostRenderingEnhance currentpagedevice
			 /PostRenderingEnhanceDetails get /REValue get 0 ne
			 /PostRenderingEnhanceDetails  <</Type 18 /TonerSaver 0
 			 /REValue currentpagedevice /PostRenderingEnhanceDetails
			 get /REValue get >>
			 >> setpagedevice " >> $device_tmp
			echo "@PJL SET ECONOMODE=OFF" >> $pjl_tmp
			)
			;;
		esac

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

		case "$duplex" in
		off )
			(
			echo "<</Duplex false
				/Tumble false /Policies <</Duplex 2>>
				>> setpagedevice" >> $device_tmp
			printf "\033&l0S" >> $pcl_tmp
			)
			;;
		long )
			(
			echo "<</Duplex true
				/Tumble false /Policies <</Duplex 2>>
				>> setpagedevice" >> $device_tmp
			printf "\033&l1S" >> $pcl_tmp
			)
			;;
		short )
			(
			echo "<</Duplex true
				/Tumble true /Policies <</Duplex 2>>
				>> setpagedevice" >> $device_tmp
			printf "\033&l2S" >> $pcl_tmp
			)
			;;
		* )
			(
			echo "<</Duplex false
				/Tumble false /Policies <</Duplex 2>>
				>> setpagedevice" >> $device_tmp
			printf "\033&l0S" >> $pcl_tmp
			)
			;;
		esac

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

		case "$collate" in

		off )
			(
			echo "<</Collate false>>setpagedevice" >> $device_tmp
			echo "@PJL SET JOBOFFSET=OFF" >> $pjl_tmp
			printf "\033&l%dx" $copies >> $pcl_tmp
			)
			;;

		on )
			(
			echo "<</Collate true>>setpagedevice" >> $device_tmp
			echo "@PJL SET QTY=$copies" >> $pjl_tmp
			printf "\033&l1x" >> $pcl_tmp
			)
			;;

		* )
			(
			echo "<</Collate false>>setpagedevice" >> $device_tmp
			echo "@PJL SET JOBOFFSET=OFF" >> $pjl_tmp
			printf "\033&l%dx" $copies >> $pcl_tmp
			)
			;;

		esac

##########################
#
#  Idiom Recognition (Default=On)
#
##########################

		case "$idiom" in
		on )
			(
			echo "<</IdiomRecognation true>> setuserparams" >> $device_tmp
			)
			;;
		off )
			(
			echo "<</IdiomRecognation false>> setuserparams" >> $device_tmp
			)
			;;
		* )
			(
			echo "<</IdiomRecognation true>> setuserparams" >> $device_tmp
			)
			;;
		esac

##########################
#
# Media Type (Default=PLAIN)
#
##########################

		case "$mediatype" in

		plain )
			(
			echo "<< /MediaType (Plain)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n6WdPlain" >> $pcl_tmp
			)
			;;

		recycled )
			(
			echo "<< /MediaType (Recycled)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n9WdRecycled" >> $pcl_tmp
			)
			;;

		special )
			(
			echo "<< /MediaType (Special)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n8WdSpecial" >> $pcl_tmp
			)
			;;

		thick )
			(
			echo "<< /MediaType (Thick)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n6WdThick" >> $pcl_tmp
			)
			;;

		tabstock )
			(
			echo "<< /MediaType (Tabstock)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n9WdTabstock" >> $pcl_tmp
			)
			;;

		translucent )
			(
			echo "<< /MediaType (Translucent)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n12WdTranslucent" >> $pcl_tmp
			)
			;;

		usercolor1 )
			(
			echo "<< /MediaType (User Color 1)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n13WdUser Color 1" >> $pcl_tmp
			)
			;;

		usercolor2 )
			(
			echo "<< /MediaType (User Color 2)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n13WdUser Color 2" >> $pcl_tmp
			)
			;;

		* )
			(
			echo "<< /MediaType (Plain)
			/Policies <</MediaType 2>> >> setpagedevice" >> $device_tmp
			printf "\033&n6WdPlain" >> $pcl_tmp
			)
			;;

		esac

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

		case "$staple" in
		off )
			(
			echo " <</Staple 0 /StapleDetails
			 <</Type 13 /Position 0 >>
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=OFF" >> $pjl_tmp
			)
			;;
		lsportrait )
			(
			echo " <</OutputType (Finisher Shift Tray)
			 /Staple 2/StapleDetails <</Type 13 /Position 1 >>
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFTTOPSLANTPORT" >> $pjl_tmp
			)
			;;
		lslandscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 2 >>
  			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFTTOPSLANTLAND" >> $pjl_tmp
			)
			;;
		lhportrait )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 5 >>
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFTTOPHORIZPORT" >> $pjl_tmp
			)
			;;
		lhlandscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 6 >>
  			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFTTOPHORIZLAND" >> $pjl_tmp
			)
			;;

		lvportrait )
			(
			echo "	<</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 3 >>
 			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFTTOPVERTPORT" >> $pjl_tmp
			)
			;;

		lvlandscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 4 >>
 			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFTTOPVERTLAND" >> $pjl_tmp
			)
			;;
		t2portrait )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 17 >>
			 >>setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=TOP2PORT" >> $pjl_tmp
			)
			;;
		t2landscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 18 >>
 			 >>setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=TOP2LAND" >> $pjl_tmp
			)
			;;
		l2portrait )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails
			 <</Type 13 /Position 7 >>
			 >>setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFT2PORT" >> $pjl_tmp
			)
			;;
		l2landscape )
			(
			echo " <</OutputType (Finisher Shift Tray)  /Staple 2 /StapleDetails
			 <</Type 13 /Position 8 >>
			 >>setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=LEFT2LAND" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " <</Staple 0 /StapleDetails
			 <</Type 13 /Position 0 >>
			 >> setpagedevice" >> $device_tmp
			echo "@PJL SET STAPLE=OFF" >> $pjl_tmp
			)
			;;
		esac

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

		case "$paper" in
		letter )
			(
			echo "<</PageSize [612 792] /ImagingBBox null
			 >> setpagedevice " >> $device_tmp
			printf "\033&l2A" >> $pcl_tmp
			)
			;;
		legal )
			(
			echo "<</PageSize [612 1008] /ImagingBBox null
			 >> setpagedevice " >> $device_tmp
			printf "\033&l3A" >> $pcl_tmp
			)
			;;
		A4 )
			(
			echo "<</PageSize [595 842] /ImagingBBox null
	 		>> setpagedevice " >> $device_tmp
			printf "\033&l26A" >> $pcl_tmp
			)
			;;
		A5 )
			(
			echo "<</PageSize [420 595] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l2000A" >> $pcl_tmp
			)
			;;
		A6 )
			(
			echo "<</PageSize [297 420] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			printf "\033&l2001A" >> $pcl_tmp
			)
			;;
		8x13 )
			(
			echo "<</PageSize [576 936] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l2011A" >> $pcl_tmp
			)
			;;
		foolscap|85x13 )
			(
			echo "<</PageSize [612 936] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l2007A" >> $pcl_tmp
			)
			;;
		folio|825x13 )
			(
			echo "<</PageSize [595 935] /ImagingBBox null
			 >> setpagedevice " >> $device_tmp
			printf "\033&l2012A" >> $pcl_tmp
			)
			;;
		B5 )
			(
			echo "<</PageSize [516 729] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			printf "\033&l45A" >> $pcl_tmp
			)
			;;
		11x17|ledger|tabloid )
			(
			echo "<</PageSize [792 1224] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l6A" >> $pcl_tmp
			)
			;;
		A3 )
			(
			echo "<</PageSize [842 1191] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			printf "\033&l27A" >> $pcl_tmp
			)
			;;
		B4 )
			(
			echo "<</PageSize [729 1032] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			printf "\033&l46A" >> $pcl_tmp
			)
			;;
		executive )
			(
			echo "<</PageSize [522 756] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l1A" >> $pcl_tmp
			)
			;;
		55x85|halfletter|statement )
			(
			echo "<</PageSize [396 612] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l2008A" >> $pcl_tmp
			)
			;;
		8k )
			(
			echo "<</PageSize [756 1105] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l2030A" >> $pcl_tmp
			)
			;;
		16k )
			(
			echo "<</PageSize [552 756] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			printf "\033&l2031A" >> $pcl_tmp
			)
			;;
		* )
			(
			echo "<</PageSize [612 792] /ImagingBBox null
			 >> setpagedevice " >> $device_tmp
			printf "\033&l2A" >> $pcl_tmp
			)
			;;
		esac

##########################
#
#  Jog/Job Separation (Default=Off)
#
##########################

		case "$separate" in
		off )
			(
			echo "<</Jog 0  >> setpagedevice" >> $device_tmp
			)
			;;
		on )
			(
			echo "<</OutputType (Finisher Shift Tray)
				/Jog 2	 >> setpagedevice" >> $device_tmp
			)
			;;
		* )
			(
			echo "<</Jog 0  >> setpagedevice" >> $device_tmp
			)
			;;
		esac

##########################
#
# LNX_TUB_E
# Print the file
#
##########################

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
#
##########################

cat $prtout_tmp | lp -d $REMOTE_PRINTER

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
