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

##########################
##
## Update History
##
##########################

##########################
##
## 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=
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
#echo "@PJL SET DISKIMAGE=OFF" >> $pjl_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  )
		orientation_port="yes"
		printf "\033&l0O" >> $pcl_tmp
		;;

	landscape )
		orientation_port="no"
		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
		;;

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

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

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

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

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

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

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

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

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

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

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

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

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

	esac

done

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

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

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

mydate=`date +'%Y/%m/%d'`
mytime=`date +'%H:%M:%S'`
ps_timeinfo=`date +'%Y%m%d%H%M'`

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

##########################
#
# Print Job Type
#
##########################

	case "$jobtype" in
	    sample )
		(
		echo "mark ($userid) ($ps_timeinfo) {proofprint} stopped cleartomark" >> $device_tmp

		echo "@PJL PROOFJOB" >> $pjl_tmp
		echo "@PJL SET USERID=\"$userid\"" >> $pjl_tmp
		echo "@PJL SET TIME=\"$mytime\"" >> $pjl_tmp
		echo "@PJL SET DATE=\"$mydate\"" >> $pjl_tmp
		echo "@PJL SET QTY=1" >> $pjl_tmp
		)
		;;

	    locked )
		(
		echo "mark ($userid) ($ps_timeinfo) ($password) {secureprint} stopped cleartomark" >> $device_tmp

		echo "@PJL SECUREJOB" >> $pjl_tmp
		echo "@PJL SET JOBPASSWORD=$password" >> $pjl_tmp
		echo "@PJL SET USERID=\"$userid\"" >> $pjl_tmp
		echo "@PJL SET TIME=\"$mytime\"" >> $pjl_tmp
		echo "@PJL SET DATE=\"$mydate\"" >> $pjl_tmp
#		echo "@PJL SET QTY=1" >> $pjl_tmp
		)
		;;

#	    docserver )
#		(
#		echo "mark ($userid) ($ps_timeinfo) ($password) {storedprint} stopped cleartomark" >> $device_tmp
#
#		echo "@PJL SET DISKIMAGE=ON" >> $pjl_tmp
#		echo "@PJL SET JOBPASSWORD=$password" >> $pjl_tmp
#		echo "@PJL SET USERID=\"$userid\"" >> $pjl_tmp
#		echo "@PJL SET TIME=\"$mytime\"" >> $pjl_tmp
#		echo "@PJL SET DATE=\"$mydate\"" >> $pjl_tmp
#		echo "@PJL SET QTY=1" >> $pjl_tmp
#		)
#		;;

	    * )
		;;
	esac

##########################
#
# Paper Size
#
##########################
	case "$paper" in
	    A3 )
		(
		echo "<< /DeferredMediaSelection true
  		 	/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [842 1191] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l27A" >> $pcl_tmp
		)
		;;

	    A4 )
		(
		echo "<< /DeferredMediaSelection true
 			/Policies << /PageSize 2 /MediaType 2 >>
 			/PageSize [595 842] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l26A" >> $pcl_tmp
		)
		;;

	    A5 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [421 595] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l2000A" >> $pcl_tmp
		)
		;;

	    A6 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [298 421] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l2001A" >> $pcl_tmp
		)
		;;

	    B4 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [728 1031] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l46A" >> $pcl_tmp
		)
		;;

	    B5 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [516 729] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l45A" >> $pcl_tmp
		)
		;;

	    legal )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [612 1008] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l3A" >> $pcl_tmp
		)
		;;

	    letter )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [612 792] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l2A" >> $pcl_tmp
		)
		;;

	    halfletter )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [396 612] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l2008A" >> $pcl_tmp
		)
		;;

	    11x17 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [792 1224] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l6A" >> $pcl_tmp
		)
		;;

	    executive )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [522 756] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l1A" >> $pcl_tmp
		)
		;;

	    8x13 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [576 936] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l2011A" >> $pcl_tmp
		)
		;;

	    folio )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [595 935] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l2012A" >> $pcl_tmp
		)
		;;

	    85x13 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [612 936] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l2007A" >> $pcl_tmp
		)
		;;

	    envelope )
		(
		echo "<< /DeferredMediaSelection true
			/Policies << /PageSize 2 /MediaType 2 >>
			/PageSize [297 684] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l81A" >> $pcl_tmp
		)
		;;

	    env-monarch )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [279 540] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l80A" >> $pcl_tmp
		)
		;;

	    env-c5 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [459 649] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l91A" >> $pcl_tmp
		)
		;;

	    env-c6 )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [323 459] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l2022A" >> $pcl_tmp
		)
		;;

	    env-dl )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [311 623] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l90A" >> $pcl_tmp
		)
		;;

	    8k )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [757 1106] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l2030A" >> $pcl_tmp
		)
		;;

	    16k )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [553 757] /ImagingBBox null>> setpagedevice" >> $device_tmp

		printf "\033&l2031A" >> $pcl_tmp
		)
		;;

	    * )
		(
		echo "<< /DeferredMediaSelection true
  			/Policies << /PageSize 2 /MediaType 2 >>
  			/PageSize [612 792] /ImagingBBox null >> setpagedevice" >> $device_tmp

		printf "\033&l2A" >> $pcl_tmp
		)
		;;
	esac

##########################
#
# Input Tray Selection (Default=AutoSelect)
#
##########################
	case "$itray" in
	    bypass )
		(
		echo " 
			/MEDIAPOSITION {/MediaPosition 0} def
			0 statusdict /setpapertray get exec" >> $device_tmp

		printf "\033&l2H" >> $pcl_tmp
		)
		;;

	    tray1 )
		(
		echo "/MEDIAPOSITION {/MediaPosition 1} def" >> $device_tmp

		printf "\033&l8H" >> $pcl_tmp
		)
		;;

	    tray2 )
		(
		echo "<</MediaPosition 2>> setpagedevice" >> $device_tmp

		printf "\033&l1H" >> $pcl_tmp
		)
		;;

	    tray3 )
		(
		echo "<</MediaPosition 3>> setpagedevice" >> $device_tmp

		printf "\033&l4H" >> $pcl_tmp
		)
		;;

	    tray4 )
		(
		echo "<</MediaPosition 4>> setpagedevice" >> $device_tmp

		printf "\033&l30H" >> $pcl_tmp
		)
		;;

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

##########################
#
# Auto Tray Switching (Default=On)
#
##########################
	case "$trayswitch" in
	    on )
		(

		echo "@PJL SET AUTOTRAYCHANGE=ON" >> $pjl_tmp
		)
		;;

	    off )
		(

		echo "@PJL SET AUTOTRAYCHANGE=OFF" >> $pjl_tmp
		)
		;;

	    * )
		(

		echo "@PJL SET AUTOTRAYCHANGE=ON" >> $pjl_tmp
		)
		;;
	esac

##########################
#
# Resolution (Default=600dpi)
#
##########################
	case "$resolution" in
	    600 )
		(
		echo "<</HWResolution[600 600]>>setpagedevice" >> $device_tmp
		
		echo "@PJL SET RESOLUTION=600" >> $pjl_tmp		
		)
		;;

	    300 )
		(
		echo "<</HWResolution[300 300]>>setpagedevice" >> $device_tmp

		echo "@PJL SET RESOLUTION=300" >> $pjl_tmp
		)
		;;

	    * )
		(
		echo "<</HWResolution[600 600]>>setpagedevice" >> $device_tmp

		echo "@PJL SET RESOLUTION=600" >> $pjl_tmp
		)
		;;
	esac

##########################
#
# Edge Smoothing (Default=ON)
#
##########################
	case "$edgesmooth" in
	    on )
		(
		echo "<</PostRenderingEnhance true
			/PostRenderingEnhanceDetails <</Type 34 /OutputMode 0>>
			>> setpagedevice " >> $device_tmp

		echo "@PJL SET SMOOTHING=ON" >> $pjl_tmp
		)
		;;

	    off )
		(
		echo "<< /PostRenderingEnhance false >> setpagedevice " >> $device_tmp

		echo "@PJL SET SMOOTHING=OFF" >> $pjl_tmp
		)
		;;

	    * )
		(
		echo "<</PostRenderingEnhance true
			/PostRenderingEnhanceDetails <</Type 34 /OutputMode 0>>
			>> setpagedevice " >> $device_tmp

		echo "@PJL SET SMOOTHING=ON" >> $pjl_tmp
		)
		;;
	esac


##########################
#
# Toner Saver(Default=OFF)
#
##########################
	case "$tonersav" in
	    off )
		(
		echo "<< /PostRenderingEnhance false >> setpagedevice " >> $device_tmp

		echo "@PJL SET ECONOMODE=OFF" >> $pjl_tmp
		)
		;;
	    on )
		(
		echo "<</PostRenderingEnhance true
			/PostRenderingEnhanceDetails <</Type 34 /OutputMode 3>>
			>> setpagedevice " >> $device_tmp

		echo "@PJL SET ECONOMODE=ON" >> $pjl_tmp
		)
		;;
	    * )
		(
		echo "<< /PostRenderingEnhance false >> setpagedevice " >> $device_tmp

		echo "@PJL SET ECONOMODE=OFF" >> $pjl_tmp
		)
		;;
	esac

##########################
#
# Duplex (Default=OFF)
#
##########################
	case "$duplex" in
	    off )
		(
		echo "<</Duplex false>>setpagedevice" >> $device_tmp

		echo "@PJL SET DUPLEX=OFF" >> $pjl_tmp
		)
		;;

	    long )
		(
		echo "<</Duplex true /Tumble false>>setpagedevice" >> $device_tmp

		echo "@PJL SET DUPLEX=ON" >> $pjl_tmp
		echo "@PJL SET BINDING=LONGEDGE" >> $pjl_tmp
		)
		;;

	    short )
		(
		echo "<</Duplex true /Tumble true>>setpagedevice" >> $device_tmp

		echo "@PJL SET DUPLEX=ON" >> $pjl_tmp
		echo "@PJL SET BINDING=SHORTEDGE" >> $pjl_tmp
		)
		;;

	    * )
		(
		echo "<</Duplex false>>setpagedevice" >> $device_tmp

		echo "@PJL SET DUPLEX=OFF" >> $pjl_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 /CollateDetails <</Type 6 /AlignSet 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

##########################
#
# Media Type (Default=Plain)
#
##########################
	case "$mediatype" in
	    plain )
		(
		echo "<< /MediaType (Plain) >> setpagedevice" >> $device_tmp

		printf "\033&n6WdPlain" >> $pcl_tmp
		)
		;;

	    recycled )
		(
		echo "<< /MediaType (Recycled) >> setpagedevice" >> $device_tmp

		printf "\033&n9WdRecycled" >> $pcl_tmp
		)
		;;

	    special )
		(
		echo "<< /MediaType (Special) >> setpagedevice" >> $device_tmp

		printf "\033&n8WdSpecial" >> $pcl_tmp
		)
		;;

#	    color )
#		(
#		echo "<< /MediaType (Color) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n6WdColor" >> $pcl_tmp
#		)
#		;;
#
#	    letterhead )
#		(
#		echo "<< /MediaType (Letterhead) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n11WdLetterhead" >> $pcl_tmp
#		)
#		;;
#
#	    preprinted )
#		(
#		echo "<< /MediaType (Preprinted) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n11WdPreprinted" >> $pcl_tmp
#		)
#		;;
#
#	    prepunched )
#		(
#		echo "<< /MediaType (Prepunched) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n11WdPrepunched" >> $pcl_tmp
#		)
#		;;
#
#	    labels )
#		(
#		echo "<< /MediaType (Labels) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n7WdLabels" >> $pcl_tmp
#		)
#		;;
#
#	    bond )
#		(
#		echo "<< /MediaType (Bond) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n5WdBond" >> $pcl_tmp
#		)
#		;;
#
#	    cardstock )
#		(
#		echo "<< /MediaType (Cardstock) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n10WdCardstock" >> $pcl_tmp
#		)
#		;;
#
#	    transparency )
#		(
#		echo "<< /MediaType (Transparency) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n13WdTransparency" >> $pcl_tmp
#		)
#		;;
#
#	    thick )
#		(
#		echo "<< /MediaType (Thick) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n6WdThick" >> $pcl_tmp
#		)
#		;;
#
#	    thin )
#		(
#		echo "<< /MediaType (Thin) >> setpagedevice" >> $device_tmp
#
#		printf "\033&n5WdThin" >> $pcl_tmp
#		)
#		;;

	    * )
		(
		echo "<< /MediaType (Plain) >> setpagedevice" >> $device_tmp

		printf "\033&n6WdPlain" >> $pcl_tmp
		)
		;;
	esac

##########################
#
#  Output Bin (Default= default)
#
##########################
	case "$outbin" in
	    default )
		(
		echo "<</OutputType null>>setpagedevice" >> $device_tmp
		)
		;;

	    standard )
		(
		echo "<</OutputType (Standard)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=UPPER" >> $pjl_tmp
		)
		;;

	    standard1 )
		(
		echo "<</OutputType (Standard)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN1" >> $pjl_tmp
		)
		;;

	    finishershift )
		(
		echo "<</OutputType (Fin1000Shift)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=FINISHERSHIFT" >> $pjl_tmp
		)
		;;

	    finishershift1 )
		(
		echo "<</OutputType (Fin500Shift)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=FINISHERSHIFT" >> $pjl_tmp
		)
		;;

	    mbin1 )
		(
		echo "<</OutputType (MailBin1)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN2" >> $pjl_tmp
		)
		;;

	    mbin2 )
		(
		echo "<</OutputType (MailBin2)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN3" >> $pjl_tmp
		)
		;;

	    mbin3 )
		(
		echo "<</OutputType (MailBin3)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN4" >> $pjl_tmp
		)
		;;

	    mbin4 )
		(
		echo "<</OutputType (MailBin4)>>setpagedevice" >> $device_tmp

		echo "@PJL SET OUTBIN=OPTIONALOUTPUTBIN5" >> $pjl_tmp
		)
		;;

	    * )
		(
		echo "<</OutputType null>>setpagedevice" >> $device_tmp
		)
		;;
	esac

##########################
#
# Staple (Default=OFF)
#
##########################
	case "$staple" in
	    off )
		(
		echo "<< /Staple 0 >> setpagedevice" >> $device_tmp

		echo "@PJL SET STAPLE=OFF" >> $pjl_tmp
		)
		;;

	    tleft )
		(
		if [ "$outbin" = "finishershift" ]
		then
		    echo "<< /OutputType (Fin1000Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 0 >>
			/LeadingEdge 1
			>> setpagedevice" >> $device_tmp

		    if [ -z "$orientation_port" -o "$orientation_port" = "yes" ]
		    then
			echo "@PJL SET STAPLE=LEFTTOPSLANTPORT" >> $pjl_tmp
		    else
			echo "@PJL SET STAPLE=LEFTTOPSLANTLAND" >> $pjl_tmp
		    fi
		    echo "@PJL SET QTY=$copies" >> $pjl_tmp
		    printf "\033&l1x" >> $pcl_tmp
		else
		    echo "<< /OutputType (Fin500Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 0 >>
			/LeadingEdge 1
			>> setpagedevice" >> $device_tmp

		    if [ -z "$orientation_port" -o "$orientation_port" = "yes" ]
		    then
			echo "@PJL SET STAPLE=LEFTTOPHORIZPORT" >> $pjl_tmp
		    else
			echo "@PJL SET STAPLE=LEFTTOPHORIZLAND" >> $pjl_tmp
		    fi
		    echo "@PJL SET QTY=$copies" >> $pjl_tmp
		    printf "\033&l1x" >> $pcl_tmp
		fi
		);;

	    tright )
		(
		if [ "$outbin" = "finishershift" ]
		then
		    echo "<< /OutputType (Fin1000Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 6 >>
			/LeadingEdge 0
			>> setpagedevice" >> $device_tmp

		    if [ -z "$orientation_port" -o "$orientation_port" = "yes" ]
		    then
			echo "@PJL SET STAPLE=RIGHTTOPSLANTPORT" >> $pjl_tmp
		    else
			echo "@PJL SET STAPLE=RIGHTTOPSLANTLAND" >> $pjl_tmp
		    fi
		    echo "@PJL SET QTY=$copies" >> $pjl_tmp
		    printf "\033&l1x" >> $pcl_tmp
		else
		    echo "<< /OutputType (Fin500Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 6 >>
			/LeadingEdge 0
			>> setpagedevice" >> $device_tmp

		    if [ -z "$orientation_port" -o "$orientation_port" = "yes" ]
		    then
			echo "@PJL SET STAPLE=RIGHTTOPVERTPORT" >> $pjl_tmp
		    else
			echo "@PJL SET STAPLE=RIGHTTOPVERTLAND" >> $pjl_tmp
		    fi
		    echo "@PJL SET QTY=$copies" >> $pjl_tmp
		    printf "\033&l1x" >> $pcl_tmp
		fi
		)
		;;

	    bleft )
		(
		if [ "$outbin" = "finishershift" ]
		then
		    echo "<< /OutputType (Fin1000Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 2 >>
			/LeadingEdge 0
			>> setpagedevice" >> $device_tmp
		else
		    echo "<< /OutputType (Fin500Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 2 >>
			/LeadingEdge 0
			>> setpagedevice" >> $device_tmp
		fi
		)
		;;

	    bright )
		(
		if [ "$outbin" = "finishershift" ]
		then
		    echo "<< /OutputType (Fin1000Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 4 >>
			/LeadingEdge 1
			>> setpagedevice" >> $device_tmp
		else
		    echo "<< /OutputType (Fin500Shift)
			/Collate true  /CollateDetails <</Type 6 /AlignSet true>>
			/Staple 2  /StapleDetails << /Type 14 /Angle 0 /Position 4 >>
			/LeadingEdge 1
			>> setpagedevice" >> $device_tmp
		fi
		)
		;;

	    * )
		(
		echo "<< /Staple 0 >> setpagedevice" >> $device_tmp

		echo "@PJL SET STAPLE=OFF" >> $pjl_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_PS=0, 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_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


elif [ "$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
else
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
