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

##########################
##
## Update History
##
## 12/10/2000 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_PS=
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_$$"

##########################
#
# Auto-detect input file type here
#
# If is_PS=1, input file is PostScript
# If is_PS=0, input file is text
# is_PS is 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
#
# Resolution
#
##########################
	
	resolution=* )
		resolution=`parse ${i}`
		case "$resolution" in

		1200 )
			(
			echo "<</HWResolution[1200 1200]>>setpagedevice" >> $device_tmp
			)
			;;
			
		600 )
			(
			echo "<</HWResolution[600 600]>>setpagedevice" >> $device_tmp
			)
			;;
		esac
		;;

##########################
#
# Duplex
#
##########################
	
	duplex=* )
		duplex=`parse ${i}`
		case "$duplex" in

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

##########################
#
# Collate
#
##########################
		
	collate=* )
		collate=`parse ${i}`
		case "$collate" in

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

		on )
			(
			echo "<</Collate true /CollateDetails <</Type 6 /AlignSet true>>
			  >>setpagedevice" >> $device_tmp
			)
			;;
		esac
		;;		

########################## 
#
# Media Type						
#
##########################

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

		plain )
			(
			echo "<< /MediaType (Plain) >> setpagedevice" >> $device_tmp
			)
			;;
		recycled )
			(
			echo "<< /MediaType (Recycled) >> setpagedevice" >> $device_tmp
			)
			;;
		special )
			(
			echo "<< /MediaType (Special) >> setpagedevice" >> $device_tmp
			)
			;;
		color )
			(
			echo "<< /MediaType (Color) >> setpagedevice" >> $device_tmp
			)
			;;			
		letterhead )
			(
			echo "<< /MediaType (Letterhead) >> setpagedevice" >> $device_tmp
			)
			;;
		preprinted )
			(
			echo "<< /MediaType (Preprinted) >> setpagedevice" >> $device_tmp
			)
			;;
		prepunched )
			(
			echo "<< /MediaType (Prepunched) >> setpagedevice" >> $device_tmp
			)
			;;
		labels )
			(
			echo "<< /MediaType (Labels) >> setpagedevice" >> $device_tmp
			)
			;;
		bond )
			(
			echo "<< /MediaType (Bond) >> setpagedevice" >> $device_tmp
			)
			;;			
		cardstock )
			(
			echo "<< /MediaType (Cardstock) >> setpagedevice" >> $device_tmp
			)
			;;
		transparency )
			(
			echo "<< /MediaType (Transparency) >> setpagedevice" >> $device_tmp
			)
			;;			
		thick )
			(
			echo "<< /MediaType (Thick) >> setpagedevice" >> $device_tmp
			)
			;;			
		esac
		;;
		

########################## 
#
# Output Tray Selection						
#
##########################

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

		default )
			(
			echo "<</OutputType null>>setpagedevice" >> $device_tmp
			)
			;;
		standard )
			(
			echo "<</OutputType (Standard)>>setpagedevice" >> $device_tmp
			)
			;;
		upper )
			(
			echo "<</OutputType (1Bin)>>setpagedevice" >> $device_tmp
			)
			;;
		mbin1 )
			(
			echo "<</OutputType (MailBin1)>>setpagedevice" >> $device_tmp
			)
			;;			
		mbin2 )
			(
			echo "<</OutputType (MailBin2)>>setpagedevice" >> $device_tmp
			)
			;;
		mbin3 )
			(
			echo "<</OutputType (MailBin3)>>setpagedevice" >> $device_tmp
			)
			;;			
		mbin4 )
			(
			echo "<</OutputType (MailBin4)>>setpagedevice" >> $device_tmp
			)
			;;			
		esac
		;;
		
########################## 
#
# Input Tray Selection						
#
##########################

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

		bypass )
			(
			echo "<</MediaPosition 0>> setpagedevice" >> $device_tmp
			)
			;;
		tray1 )
			(
			echo "<</MediaPosition 1>> setpagedevice" >> $device_tmp
			)
			;;
		tray2 )
			(
			echo "<</MediaPosition 2>> setpagedevice" >> $device_tmp
			)
			;;
		tray3 )
			(
			echo "<</MediaPosition 3>> setpagedevice" >> $device_tmp
			)
			;;			
		envelope )
			(
			echo "<</ManualFeed false /TraySwitch false 
			/MediaPosition 2>> setpagedevice" >> $device_tmp
			)
			;;
		esac
		;;
		

		
##########################
#
# Paper Size (Default=Letter)
#
##########################
	
	paper=* )
		paper=`parse ${i}`
		case "$paper" in
		
		B5 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [516 729] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;
			
		A4 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [595 842] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;
			
		A5 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [421 595] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;
			
		A6 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [298 421] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;
			
		legal )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [612 1008] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;		

		A3 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [842 1191] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;		
			
		B4 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [728 1031] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;					
			
		tabloid )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [792 1224] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;		
						
		letter )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [612 792] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;
			
		halfletter )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [396 612] /ImagingBBox null >> setpagedevice" >> $device_tmp
			)
			;;
					
		executive )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [522 756] /ImagingBBox null>> setpagedevice" >> $device_tmp
			)
			;;
			
		folio )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [595 935] /ImagingBBox null>> setpagedevice" >> $device_tmp
			)
			;;
			
		85x13 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [612 936] /ImagingBBox null>> setpagedevice" >> $device_tmp
			)
			;;

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

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

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

		env-monarch )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [279 540] /ImagingBBox null>> setpagedevice" >> $device_tmp
			)
			;;	
		env-c5 )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [459 649] /ImagingBBox null>> setpagedevice" >> $device_tmp
			)
			;;						
		8k )
			(
			echo "<< /DeferredMediaSelection true /Policies << /PageSize 2 /MediaType 2 >>
			  /PageSize [757 1106] /ImagingBBox null>> setpagedevice" >> $device_tmp
			)
			;;									

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

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

if [ "$is_PS" -eq 0 ]
then

	##########################
	#
	# If text file, apply a filter to remove stair case effect
	#
	##########################
	
	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 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
