#!/bin/sh
##########################
##
##Aficio 350/350e/450/450e/550/650,
##	551/700 Type 700 and AP4500
## Unix Printing Interface Program.
## Last Update 12/09/2004
## Copyright 1999-2004 
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## Supported products
##
## Aficio 350/350e/450/450e
## Aficio 550/650
## Aficio 551/700 Type 700
## AP 4500
##
##########################

##########################
##
## Update History
##
## 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=
is_DSC=

##########################
#
# Change the following printer name accordingly
#
##########################

REMOTE_PRINTER=

##########################
#
# device_tmp stores device option commands
# prtout_tmp holds actual printout to the printer
#
##########################

device_tmp="/tmp/afcdev_$$.tmp"
prtout_tmp="/tmp/afcprt_$$.tmp"
ps_tmp="/tmp/afcps_$$"

##########################
#
# Store some default values - removed 8/10/00
#
##########################
#
#echo " <</HWResolution [600 600]
#       /Policies <</HWResolution 2>> 
#       >> setpagedevice" >> $device_tmp
#
#echo " <</OutputType (Tray1)
#       >> setpagedevice" >> $device_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 " <</TraySwitch true
#       >> setpagedevice" >> $device_tmp
#
#echo " <</Duplex false /Tumble false
#       /Policies <</Duplex 2>> 
#       >> setpagedevice" >> $device_tmp
#
#echo " <</Staple 0 /StapleDetails <</Type 13 /Position 0 >>
#       >> setpagedevice" >> $device_tmp
#
#echo " <</Punch 0 /PunchDetails <</Type 1 /Position 0 >>
#       >> setpagedevice" >> $device_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 " <</Collate false
#       >> setpagedevice" >> $device_tmp
#
#echo " <</IdiomRecognition true
#       >> setuserparams" >> $device_tmp
#
####################################################
#
# Commented out 6/20/2000 by HK
#
#echo " <</PageSize [612 792] /ImagingBBox null
#       >> setpagedevice" >> $device_tmp
####################################################
#
#echo " <</Jog 0 >> setpagedevice" >> $device_tmp
#
####################################################

##########################
#
# Autodetect input file type here
#
# If is_PS=1, input file is PostScript
# If is_PS=0, input file is text
# is_PS will be overwritten, if user specified
# 	input file type with "-o text" or "-o ps" option
#
##########################

OS_NAME=`uname -s`

case $OS_NAME in
SunOS)
	is_PS=`file $* | grep -c PostScript`
	;;
HP-UX)
	is_PS=`file $* | grep  -c postscript`
	;;
*)
	is_PS=`file $* | grep -c PostScript`
	;;
esac

##########################
#
# Device options (e.g. -o value=a) are identified 
# and processed here.
#
##########################
##########################
# added for collate option. 
# number of copies

echo "<</NumCopies $copies>>setpagedevice" >> $device_tmp


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


	nobanner )
		nobanner="yes"
		;;

	nofilebreak )
		nofilebreak="yes"
		;;

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

	debug ) 
		DEBUG=Y
		;;
		
##########################
#
# Text option
#
##########################

	text ) 
		is_PS=0
		;;
		
##########################
#
# PS option
#
##########################

	ps ) 
		is_PS=1
		;;		
					
##########################
#
# LNX_TUB_B
# Duplex (Default=OFF)
#
##########################
	
	duplex=* )
		duplex=`parse ${i}`
		case "$duplex" in
		off )
			(
			echo "<</Duplex false 
				/Tumble false /Policies <</Duplex 2>> 
				>> setpagedevice" >> $device_tmp
			)
			;;
		long )
			(
			echo "<</Duplex true 
				/Tumble false /Policies <</Duplex 2>> 
				>> setpagedevice" >> $device_tmp
			)
			;;
		short )
			(
			echo "<</Duplex true 
				/Tumble true /Policies <</Duplex 2>> 
				>> setpagedevice" >> $device_tmp
			)
			;;			
		esac
		;;

##########################
#
# Staple (Default=OFF)
#
##########################
		
	staple=* )
		staple=`parse ${i}`
		case "$staple" in
		off )
			(
			echo " <</Staple 0 /StapleDetails 
			 <</Type 13 /Position 0 >>
			 >> setpagedevice" >> $device_tmp
			)
			;;
		lsportrait )
			(
			echo " <</OutputType (Finisher Shift Tray) 
			 /Staple 2/StapleDetails <</Type 13 /Position 1 >>
			 >> setpagedevice" >> $device_tmp
			)
			;;
		lslandscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 2 >>
  			 >> setpagedevice" >> $device_tmp
			)
			;;
		lhportrait )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 5 >>
			 >> setpagedevice" >> $device_tmp
			)
			;;	
		lhlandscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 6 >>
  			 >> setpagedevice" >> $device_tmp
			)
			;;
		
		lvportrait )
			(
			echo "	<</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 3 >>
 			 >> setpagedevice" >> $device_tmp
			)
			;;

		lvlandscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 4 >>
 			 >> setpagedevice" >> $device_tmp
			)
			;;			
		t2portrait )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 17 >>
			 >>setpagedevice" >> $device_tmp
			)
			;;
		t2landscape )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 18 >>
 			 >>setpagedevice" >> $device_tmp
			)
			;;			
		l2portrait )
			(
			echo " <</OutputType (Finisher Shift Tray) /Staple 2 /StapleDetails 
			 <</Type 13 /Position 7 >>
			 >>setpagedevice" >> $device_tmp
			)
			;;
		l2landscape )
			(
			echo " <</OutputType (Finisher Shift Tray)  /Staple 2 /StapleDetails 
			 <</Type 13 /Position 8 >>
			 >>setpagedevice" >> $device_tmp
			)
			;;
		esac
		;;		

##########################
#
# Punch (Default=OFF)
#
##########################
	
	punch=* )
		punch=`parse ${i}`
		case "$punch" in
		off )
			(
			echo "<</Punch 0/PunchDetails 
			 <</Type 1 /Position 0 >>
			 >> setpagedevice">> $device_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
			)
			;;
		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
			)
			;;

		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
			)
			;;
		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
			)
			;;
		esac
		;;
		
##########################
#
# Resolution (Default=600dpi)
#
##########################

	resolution=* )
		resolution=`parse ${i}`
		case "$resolution" in

		600 )
			(
			echo "<</HWResolution [600 600] /Policies <</HWResolution 2>> 
			>> setpagedevice" >> $device_tmp
			)
			;;

		400 )
			(
			echo " <</HWResolution [400 400] /Policies <</HWResolution 2>> 
			 >> setpagedevice" >> $device_tmp
			)
			;;	

		esac
		;;	
			
########################## 
#
# Input Tray Selection	(Default=LCT)					
#
##########################

	itray=* )
		itray=`parse ${i}`
		case "$itray" in

		LCT )
			(
			echo "<</ManualFeed false /MediaPosition 8 
			 >> setpagedevice " >> $device_tmp
			)
			;;

		tray1 )
			(
			echo "<</ManualFeed false /MediaPosition 0 
			 >> setpagedevice " >> $device_tmp
			)
			;;

		tray2 )
			(
			echo "<</ManualFeed false /MediaPosition 1 
			 >> setpagedevice " >> $device_tmp
			)
			;;
			
		tray3 )
			(
			echo "<</ManualFeed false /MediaPosition 2 
			 >> setpagedevice " >> $device_tmp
			)
			;;

		tray4 )
			(
			echo "<</ManualFeed false /MediaPosition 3 
			 >> setpagedevice " >> $device_tmp
			)
			;;

		bypass )
			(
			echo "<< /ManualFeed true >> setpagedevice " >> $device_tmp
			)
			;;
		esac
		;;
		
##########################
#
#  Edge Smoothing (Default=False)	
#
##########################		

	smoothing=* )
		smoothing=`parse ${i}`
		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
			)
			;;
		on )
			(
			echo "<</PostRenderingEnhance true /PostRenderingEnhanceDetails 
 			 <</Type 18 /REValue 1 /TonerSaver currentpagedevice 
			 /PostRenderingEnhanceDetails get /TonerSaver get >> 
			 >> setpagedevice " >> $device_tmp
			)
			;;			
		esac
		;;
		
		
##########################
#
#  Output Bin (Default= InnerTray)
#
##########################

	outbin=* )
		outbin=`parse ${i}`
		case "$outbin" in
		itray )
			(
			echo " <</OutputType (Tray1)>> setpagedevice " 
			 >> $device_tmp
			)
			;;
		
		etray )
			(
			echo "<</OutputType  (Inner Joint Unit) 
			 >> setpagedevice " >> $device_tmp
			)
			;;
		main )
			(
			echo "<</OutputType (Finisher Main Tray) 
			 >> setpagedevice " >> $device_tmp
			)
			;;
		shift )
			(
			echo "<</OutputType (Finisher Shift Tray) 
			 >> setpagedevice " >> $device_tmp
			)
			;;
		bin7 )
			(
			echo "<</OutputType (Mailbox Main Tray) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin1 )
			(
			echo "<</OutputType (Mailbox Bin 1) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin2 )
			(
			echo "<</OutputType (Mailbox Bin 2) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin3 )
			(
			echo "<</OutputType (Mailbox Bin 3) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin4 )
			(
			echo "<</OutputType (Mailbox Bin 4) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin5 )
			(
			echo "<</OutputType (Mailbox Bin 5) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin6 )
			(
			echo "<</OutputType (Mailbox Bin 6) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin7 )
			(
			echo "<</OutputType (Mailbox Bin 7) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin8 )
			(
			echo "<</OutputType (Mailbox Bin 8) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		mbin9 )
			(
			echo "<</OutputType (Mailbox Bin 9) 
			 >> setpagedevice" >> $device_tmp
			)
			;;
		booklet )
			(
			echo "<</OutputType (Booklet Finisher Stack)>> setpagedevice" >> $device_tmp
			)
			;;			
				
		esac
		;;

##########################
#
# Toner Saver (Default=False)
#
##########################
	
	toner=* )
		toner=`parse ${i}`
		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
			)
			;;
		on )
			(
			echo "<</PostRenderingEnhance true /PostRenderingEnhanceDetails
 			 <</Type 18 /TonerSaver 1 /REValue currentpagedevice
 			 /PostRenderingEnhanceDetails get/REValue get >> 
 			 >> setpagedevice " >> $device_tmp
			)
			;;			
		esac
		;;
		
##########################
#
# Paper Size (Default=Letter)
#
##########################
	
	paper=* )
		paper=`parse ${i}`
		case "$paper" in
		letter )
			(
			echo "<</PageSize [612 792] /ImagingBBox null 
			 >> setpagedevice " >> $device_tmp
			)
			;;
		legal )
			(
			echo "<</PageSize [612 1008] /ImagingBBox null 
	 		 >> setpagedevice " >> $device_tmp
			)
			;;
		A4 )
			(
			echo "<</PageSize [595 842] /ImagingBBox null 
	 		>> setpagedevice " >> $device_tmp
			)
			;;
		A5 )
			(
			echo "<</PageSize [420 595] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;
		A6 )
			(
			echo "<</PageSize [297 420] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			)
			;;
		8x13 )
			(
			echo "<</PageSize [576 936] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;
		foolscap )
			(
			echo "<</PageSize [612 936] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;
		folio )
			(
			echo "<</PageSize [595 935] /ImagingBBox null
			 >> setpagedevice " >> $device_tmp
			)
			;;
		B5 )
			(
			echo "<</PageSize [516 729] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			)
			;;
		ledger )
			(
			echo "<</PageSize [792 1224] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;
		A3 )
			(
			echo "<</PageSize [842 1191] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			)
			;;
		B4 )
			(
			echo "<</PageSize [729 1032] /ImagingBBox null
			 >> setpagedevice" >> $device_tmp
			)
			;;
		executive )
			(
			echo "<</PageSize [522 756] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;		
		85x13 )
			(
			echo "<</PageSize [612 936] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;		
		8k )
			(
			echo "<</PageSize [756 1105] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;		
		16k )
			(
			echo "<</PageSize [552 756] /ImagingBBox null
 			 >> setpagedevice" >> $device_tmp
			)
			;;											
		esac
		;;

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

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

	esac
done		

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

if [ "$is_PS" -eq 0 ]
then
	cat $* | awk '{print $0, "\r"}' > $prtout_tmp

else

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

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

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

	case $is_DSC in 

	1)
		if grep '^setup' $* > /dev/null
		then
			csplit -f $ps_tmp $* "/^setup/"
		else
			csplit -f $ps_tmp $* "/%%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 $* >> $prtout_tmp
		;;	
	esac
fi
##########################
#
# Loop for printing
#
##########################

cat $prtout_tmp | lp -d $REMOTE_PRINTER 

##########################
#
# Remove temporary device command 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
fi

exit_code=0 exit 0
