#!/bin/sh
##########################
##
## Aficio Color 3506/4506 RC-210 
## Unix Printing Interface Program
## Last Update 12/09/2004
## Copyright 1999-2004
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## Update History
##
## 8/10/2000 First release
##
##########################

##########################
##
## Debug Flag
##
##########################

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=
TXT=
PS=
is_PS=
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_$$"

##########################
#
# 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 ) 
		TXT=1
		;;
		
##########################
#
# PS option
#
##########################

	ps ) 
		PS=1
		;;		
					
##########################
# LNX_TUB_B
#
# Staple
#
##########################
	
	staple=* )
		staple=`parse ${i}`
		case "$staple" in
		off )
			(
			echo "<< /Staple 0 >> setpagedevice" >> $device_tmp
			)
			;;
		tleft )
			(
			echo "<< /Collate true /Staple 3
			  /StapleDetails << /Type 14 /Location 0>> >> setpagedevice" >> $device_tmp
			)
			;;
		tright )
			(
			echo "<< /Collate true /Staple 3
			  /StapleDetails << /Type 14 /Location 11>> >> setpagedevice" >> $device_tmp
			)
			;;			
		bleft )
			(
			echo "<< /Collate true /Staple 3
			  /StapleDetails << /Type 14 /Location 12>> >> setpagedevice" >> $device_tmp
			)
			;;
		bright )
			(
			echo "<< /Collate true /Staple 3
			  /StapleDetails << /Type 14 /Location 7>> >> setpagedevice" >> $device_tmp
			)
			;;						
		esac
		;;

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

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

		tray1 )
			(
			echo "<</DeferredMediaSelection false>>setpagedevice 
			  1 statusdict /setpapertray get exec" >> $device_tmp
			)
			;;

		tray2 )
			(
			echo "<</DeferredMediaSelection false>>setpagedevice
			  2 statusdict /setpapertray get {exec}stopped pop" >> $device_tmp
			)
			;;
			
		tray3 )
			(
			echo "<</DeferredMediaSelection false>>setpagedevice
			  3 statusdict /setpapertray get {exec}stopped pop" >> $device_tmp
			)
			;;

		tray4 )
			(
			echo "<</DeferredMediaSelection false>>setpagedevice
			  4 statusdict /setpapertray get {exec}stopped pop" >> $device_tmp
			)
			;;

		bypass )
			(
			echo "<</DeferredMediaSelection false>>setpagedevice
			  0 statusdict /setpapertray get {exec}stopped pop" >> $device_tmp
			)
			;;
		esac
		;;
		
##########################
#
#  Color / Grayscale
#
##########################		

	color=* )
		color=`parse ${i}`
		case "$color" in
		yes )
			(
			echo "<< /ProcessColorModel /DeviceCMYK >> setpagedevice" >> $device_tmp
			)
			;;
		no )
			(
			echo "<< /ProcessColorModel /DeviceGray >> setpagedevice" >> $device_tmp
			)
			;;			
		esac
		;;
		
		
##########################
#
#  Auto Trayswitch
#
##########################		

	trayswitch=* )
		trayswitch=`parse ${i}`
		case "$trayswitch" in
		on )
			(
			echo "<</TraySwitch true>>setpagedevice" >> $device_tmp
			)
			;;
		off )
			(
			echo "<</TraySwitch false>>setpagedevice" >> $device_tmp
			)
			;;			
		esac
		;;

##########################
#
#  Collate
#
##########################

	collate=* )
		collate=`parse ${i}`
		case "$collate" in
		off )
			(
			echo "<</Collate false>>setpagedevice" >> $device_tmp
			)
			;;
		collate )
			(
			echo "<</Collate true>>setpagedevice" >> $device_tmp
			)
			;;	
		stack )
			(
			echo "<</Collate 2>>setpagedevice" >> $device_tmp
			)
			;;	
		esac
		;;
		
##########################
#
#  Face Down Print
#
##########################

	face=* )
		face=`parse ${i}`
		case "$face" in
		up )
			(
			echo "<</OutputFaceUp true>>setpagedevice" >> $device_tmp
			)
			;;
		
		down )
			(
			echo "<</OutputFaceUp false>>setpagedevice" >> $device_tmp
			)
			;;
		esac
		;;

##########################
#
#  duplex Print
#
##########################

	duplex=* )
		duplex=`parse ${i}`
		case "$duplex" in
		off )
			(
			echo "<</Duplex false>>setpagedevice" >> $device_tmp
			)
			;;
		
		long )
			(
			echo "<</Duplex true /Tumble false>>setpagedevice" >> $device_tmp
			)
			;;
			
		short )
			(
			echo "<</Duplex true /Tumble true>>setpagedevice" >> $device_tmp
			)
			;;
		esac
		;;

##########################
#
# Color Settings
#
##########################
	
	colorsettings=* )
		colorsettings=`parse ${i}`
		case "$colorsettings" in
		off )
			(
			echo "<< /DeviceRenderingInfo << /CustomRGB 0 >> >> setpagedevice" >> $device_tmp
			)
			;;
		vivid )
			(
			echo "<< /DeviceRenderingInfo << /CustomRGB 1 /SBWeight [1.4 1.8] >> >>
			  setpagedevice" >> $device_tmp
			)
			;;	
		supervivid )
			(
			echo "<< /DeviceRenderingInfo << /CustomRGB 1 /SBWeight [1.7 2.2] >> >>
			  setpagedevice" >> $device_tmp
			)
			;;		
		fine )
			(
			echo "<< /DeviceRenderingInfo << /CustomRGB 2 /WhitePoint /6500K >> >>
			  setpagedevice" >> $device_tmp
			)
			;;	
		superfine )
			(
			echo "<< /DeviceRenderingInfo << /CustomRGB 2 /WhitePoint /5000K >> >>
			  setpagedevice" >> $device_tmp
			)
			;;									
		esac
		;;

##########################
#
# Paper Size (Default=Letter)
#
##########################
	
	paper=* )
		paper=`parse ${i}`
		case "$paper" in
		letter )
			(
			echo "{statusdict /lettertray get exec}stopped {letter}if" >> $device_tmp
			)
			;;
		legal )
			(
			echo "{statusdict /legaltray get exec}stopped {legal}if" >> $device_tmp
			)
			;;
		A4 )
			(
			echo "{statusdict /a4tray get exec}stopped {a4}if" >> $device_tmp
			)
			;;
		A3 )
			(
			echo "{statusdict /a3tray get exec}stopped {a3}if" >> $device_tmp
			)
			;;
		A5 )
			(
			echo "{statusdict /a5tray get exec}stopped {a5}if" >> $device_tmp
			)
			;;
		tabloid )
			(
			echo "{statusdict /11x17tray get exec}stopped {11x17}if" >> $device_tmp
			)
			;;
		A6 )
			(
			echo "{statusdict /a6tray get exec}stopped {a6}if" >> $device_tmp
			)
			;;
		halfletter )
			(
			echo "{statusdict /halflettertray get exec}stopped {halfletter}if" >> $device_tmp
			)
			;;
		8x13 )
			(
			echo "{statusdict /8x13tray get exec}stopped {8x13}if" >> $device_tmp
			)
			;;
		folio )
			(
			echo "{statusdict /foliotray get exec}stopped {folio}if" >> $device_tmp
			)
			;;
		B4 )
			(
			echo "{statusdict /b4tray get exec}stopped {b4}if" >> $device_tmp
			)
			;;
		B5 )
			(
			echo "{statusdict /b5tray get exec}stopped {b5}if" >> $device_tmp
			)
			;;		
		B6 )
			(
			echo "{statusdict /b6tray get exec}stopped {b6}if" >> $device_tmp
			)
			;;
		postcard )
			(
			echo "0 statusdict /setpapertray get {exec}stopped pop" >> $device_tmp
			)
			;;
		85x13 )
			(
			echo "{statusdict /8.5x13tray get exec}stopped {8.5x13}if" >> $device_tmp
			)
			;;
		12x18 )
			(
			echo "{statusdict /12x18tray get exec}stopped {12x18}if" >> $device_tmp
			)
			;;		
		esac
		;;

##########################
#
#  Paper Type 
#
##########################

	papertype=* )
		papertype=`parse ${i}`
		case "$papertype" in
		plain )
			(
			echo "<< /MediaType (Plain) >> setpagedevice" >> $device_tmp
			)
			;;
		dupthick )
			(
			echo " statusdict begin 0 setpapertray end << /MediaType (Thick)
              /DeviceRenderingInfo << /ManualDuplexMode true >> >> setpagedevice" >> $device_tmp
			)
			;;
		thick )
			(
			echo "<< /MediaType (Thick) >> setpagedevice" >> $device_tmp
			)
			;;	
		dupplain )
			(
			echo " statusdict begin 0 setpapertray end
  			<< /MediaType (Plain)
  		/DeviceRenderingInfo << /ManualDuplexMode true >> >> setpagedevice" >> $device_tmp
			)
			;;	
		transparency )
			(
			echo "
<< /MediaType (Transparency)
     		/DeviceRenderingInfo << /CRDType /Transparency >> >> setpagedevice" >> $device_tmp
			)
			;;									
		esac
		;;
		
##########################
#
#  Color Profile
#
##########################

	profile=* )
		profile=`parse ${i}`
		case "$profile" in
		photo )
			(
			echo "<</DeviceRenderingInfo << /CRDType /Photograph >> >> setpagedevice" >> $device_tmp
			)
			;;			
		presentation )
			(
			echo "<</DeviceRenderingInfo << /CRDType /Business >> >> setpagedevice" >> $device_tmp
			)
			;;	
		solid )
			(
			echo "<</DeviceRenderingInfo << /CRDType /Colorimetric >> >> setpagedevice" >> $device_tmp
			)
			;;						
		esac
		;;
		
##########################
#
#  Dithering Type
#
##########################

	dithering=* )
		dithering=`parse ${i}`
		case "$dithering" in
		text )
			(
			echo "<< /DeviceRenderingInfo << /DitherType 2 >> >> setpagedevice" >> $device_tmp
			)
			;;
		photo )
			(
			echo "<< /DeviceRenderingInfo << /DitherType 1 >> >> setpagedevice" >> $device_tmp
			)
			;;	
		auto )
			(
			echo "<< /DeviceRenderingInfo << /DitherType 0 >> >> setpagedevice" >> $device_tmp
			)
			;;						
		esac
		;;
	esac
done		

##########################
# 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_PS=0, input file is text
# is_PS is overwritten, if user specified
#	input file type with "-o text" or "-o ps" option
#
##########################

if [ -n "$PS" ]
then
	is_PS=1
elif [ -n "$TXT" ]
then
	is_PS=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
fi

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

else

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

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

cat $prtout_tmp | lp -d $REMOTE_PRINTER 

done

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