#!/bin/sh
##########################
##
## Aficio Color 3000/4000 E-300 
## Unix Printing Interface Program
## Last Update 12/09/2004
## Copyright 1999-2004 
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## Update History
##
## 7/20/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=
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_$$"

##########################
#
# 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 /Policies << /Duplex 1 >> >>
				setpagedevice " >> $device_tmp
			)
			;;
		side )
			(
			echo " << /Duplex true /Policies << /Duplex 0 >>  >>
			        setpagedevice << /Tumble false >> setpagedevice " 
			        >> $device_tmp
			)
			;;
		top )
			(
			echo " << /Duplex true /Policies << /Duplex 0 >>  >>
			        setpagedevice << /Tumble true >> setpagedevice " 
			        >> $device_tmp
			)
			;;			
		esac
		;;

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

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

		tray1 )
			(
			echo " /XJXsettraysel where { pop 1 XJXsettraysel } if" >> $device_tmp
			)
			;;

		tray2 )
			(
			echo " /XJXsettraysel where { pop 2 XJXsettraysel } if" >> $device_tmp
			)
			;;
			
		tray3 )
			(
			echo " /XJXsettraysel where { pop 3 XJXsettraysel } if" >> $device_tmp
			)
			;;

		tray4 )
			(
			echo " /XJXsettraysel where { pop 4 XJXsettraysel } if" >> $device_tmp
			)
			;;

		bypass )
			(
			echo "1 dict dup /ManualFeed true put setpagedevice" >> $device_tmp
			)
			;;

		bypassduplex )
			(
			echo "1 dict dup /ManualFeed true put setpagedevice
			    /XJXsettraysel where { pop 254 XJXsettraysel } if" >> $device_tmp
			)
			;;			
		esac
		;;
		
##########################
#
#  Color / Grayscale
#
##########################		

	color=* )
		color=`parse ${i}`
		case "$color" in
		yes )
			(
			echo "userdict /XJXsetcolormode known
			  { (CMYK) XJXsetcolormode } if " >> $device_tmp
			)
			;;
		no )
			(
			echo "userdict /XJXsetcolormode known
			  { (Grayscale) XJXsetcolormode} if " >> $device_tmp
			)
			;;			
		esac
		;;
		
		
##########################
#
#  Sorter Option
#
##########################

	sorter=* )
		sorter=`parse ${i}`
		case "$sorter" in
		sort )
			(
			echo " << /Collate true>> setpagedevice
			userdict /XJXsetsorter known
		              { 1 XJXsetsorter }
		              { (printerinfo sortmode 1) =string
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse 
			userdict /XJXsetstapler known
		              { 0 XJXsetstapler }
		              { (printerinfo staplemode 1) =string
	                FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse ">> $device_tmp
			)
			;;

		collate )
			(
			echo "<< /Collate true>> setpagedevice
		        userdict /XJXsetsorter known
		              { 0 XJXsetsorter }
		              { (printerinfo sortmode 0) =string
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse 
			userdict /XJXsetstapler known
		              { 0 XJXsetstapler }
		              { (printerinfo staplemode 1) =string
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse " >> $device_tmp
			)
			;;	
		staple )
			(
			echo " << /Collate true>> setpagedevice
			userdict /XJXsetsorter known
		              { 1 XJXsetsorter }
		              { (printerinfo sortmode 1) =string
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse 
			userdict /XJXsetstapler known
		              { 1 XJXsetstapler }
		              { (printerinfo staplemode 1) =string
	                FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse " >> $device_tmp
			)
			;;							
		off )
			(
			echo " << /Collate false>> setpagedevice
			userdict /XJXsetsorter known
		              { 0 XJXsetsorter }
		              { (printerinfo sortmode 0) =string
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse 
			userdict /XJXsetstapler known
		              { 0 XJXsetstapler }
		              { (printerinfo staplemode 1) =string
	                FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse " >> $device_tmp
			)
			;;
		esac
		;;
		
##########################
#
#  Print Order (Default=forward)
#
##########################

	order=* )
		order=`parse ${i}`
		case "$order" in
		forward )
			(
			echo " userdict /XJXsetprintorder known
			    { 0 XJXsetprintorder }
			    { (printerinfo PrintOrder 0) =string
			FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse  " >> $device_tmp
			)
			;;
		
		reverse )
			(
			echo " userdict /XJXsetprintorder known
			    { 1 XJXsetprintorder }
			    { (printerinfo PrintOrder 1) =string
		        FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse  " >> $device_tmp
			)
			;;
		esac
		;;

##########################
#
# Landscape (Default=off)
#
##########################
	
	landscape=* )
		landscape =`parse ${i}`
		case "$landscape" in
		off )
			(
			echo " userdict /EFLandscapeSetup known { 0 EFLandscapeSetup } if
   			userdict /EFUserRotate180 known { 0 EFUserRotate180 } if " >> $device_tmp
			)
			;;

		on )
			(
			echo " userdict /EFLandscapeSetup known { 1 EFLandscapeSetup } if 
			userdict /EFUserRotate180 known { 0 EFUserRotate180 } if " >> $device_tmp
			)
			;;	

		rotate180 )
			(
			echo " userdict /EFLandscapeSetup known { 1 EFLandscapeSetup } if
			userdict /EFUserRotate180 known { 1 EFUserRotate180 } if " >> $device_tmp
			)
			;;						

		esac
		;;

##########################
#
# Paper Size (Default=Letter)
#
##########################
	
	paper=* )
		paper=`parse ${i}`
		case "$paper" in
		letter )
			(
			echo " userdict /XJXsetpagesize known
			    { (Letter) XJXsetpagesize }
			    { << /PageSize [612 792] /MediaType null 
        		    /Policies << /PageSize 7 >> >> setpagedevice
			    } ifelse " >> $device_tmp
			)
			;;
		legal )
			(
			echo " userdict /XJXsetpagesize known
			    { (Legal)XJXsetpagesize } { legal } ifelse " >> $device_tmp
			)
			;;
		A4 )
			(
			echo " userdict /XJXsetpagesize known
			    { (A4)XJXsetpagesize }
			    { << /PageSize [595 842] /MediaType null 
		            /Policies << /PageSize 7 >> >> setpagedevice
			    } ifelse " >> $device_tmp
			)
			;;
		A3 )
			(
			echo " userdict /XJXsetpagesize known
			    { (A3) XJXsetpagesize } { a3 } ifelse " >> $device_tmp
			)
			;;
		A5 )
			(
			echo " userdict /XJXsetpagesize known
			    { (A5) XJXsetpagesize } { a5 } ifelse " >> $device_tmp
			)
			;;
		tabloid )
			(
			echo " userdict /XJXsetpagesize known
			    { (Tabloid) XJXsetpagesize } { 11x17 } ifelse " >> $device_tmp
			)
			;;
		A5SEF )
			(
			echo " userdict /XJXsetpagesize known
			    { (SEFA5) XJXsetpagesize } { a5 } ifelse " >> $device_tmp
			)
			;;
		A6SEF )
			(
			echo " userdict /XJXsetpagesize known
			    { (A6) XJXsetpagesize } { a6 } ifelse " >> $device_tmp
			)
			;;
		legal13 )
			(
			echo " userdict /XJXsetpagesize known
			    { (Legal13) XJXsetpagesize } { legal13 } ifelse " >> $device_tmp
			)
			;;
		letterSEF )
			(
			echo " userdict /XJXsetpagesize known
			    { (LetterSEF) XJXsetpagesize }
			    { << /PageSize [612 792] /MediaType (ShortEdgeFeed) 
		            /Policies << /PageSize 7 >> >> setpagedevice
			    } ifelse " >> $device_tmp
			)
			;;
		A4SEF )
			(
			echo " userdict /XJXsetpagesize known 
			    { (A4SEF) XJXsetpagesize }
			    { << /PageSize [595 842] /MediaType (ShortEdgeFeed) 
		            /Policies << /PageSize 7 >> >> setpagedevice
			    } ifelse " >> $device_tmp
			)
			;;
		B4 )
			(
			echo "userdict /XJXsetpagesize known
			    { (B4) XJXsetpagesize } { a4 } ifelse" >> $device_tmp
			)
			;;
		B5 )
			(
			echo "userdict /XJXsetpagesize known
			    { (B5) XJXsetpagesize } { a4 } ifelse" >> $device_tmp
			)
			;;		
		B5SEF )
			(
			echo "userdict /XJXsetpagesize known
			    { (B5SEF) XJXsetpagesize } { a4 } ifelse" >> $device_tmp
			)
			;;
		B6SEF )
			(
			echo "userdict /XJXsetpagesize known
			    { (SEFB6) XJXsetpagesize } { a4 } ifelse" >> $device_tmp
			)
			;;
		postcard )
			(
			echo "userdict /XJXsetpagesize known
			    { (PostcardJ) XJXsetpagesize } { a4 } ifelse" >> $device_tmp
			)
			;;
		doublepostcard )
			(
			echo "userdict /XJXsetpagesize known
			    { (DoublePostcardRotated) XJXsetpagesize } { a4 } ifelse" >> $device_tmp
			)
			;;
		tabloidextra )
			(
			echo "userdict /XJXsetpagesize known
			    { (TabloidExtra) XJXsetpagesize } { 11x17 } ifelse" >> $device_tmp
			)
			;;		
						
		esac
		;;

##########################
#
#  Media Type (Default=Plain)
#
##########################

	media=* )
		media=`parse ${i}`
		case "$media" in
		plain )
			(
			echo " userdict /XJXsetmediatype known
			    { 1 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		thick )
			(
			echo " userdict /XJXsetmediatype known
			    { 2 XJXsetmediatype } if" >> $device_tmp
			)
			;;	
		transparency )
			(
			echo " userdict /XJXsetmediatype known
			    { 3 XJXsetmediatype } if" >> $device_tmp
			)
			;;	
		transparentslip )
			(
			echo " userdict /XJXsetmediatype known
			    { 3 XJXsetmediatype } if
			    /XJXsetSlipSheet where { pop 1 XJXsetSlipSheet } if " >> $device_tmp
			)
			;;										
		esac
		;;
		
##########################
#
#  Rendering Dictionary (Default=Photographic)
#
##########################

	rendering=* )
		rendering=`parse ${i}`
		case "$rendering" in
		photo )
			(
			echo " userdict /XJXsetrenderingintent known
			{ (Photographic) XJXsetrenderingintent } if " >> $device_tmp
			)
			;;
		presentation )
			(
			echo " userdict /XJXsetrenderingintent known
			{ (Presentation) XJXsetrenderingintent } if " >> $device_tmp
			)
			;;	
		solid )
			(
			echo " userdict /XJXsetrenderingintent known
			{ (Solid) XJXsetrenderingintent } if " >> $device_tmp
			)
			;;						
		esac
		;;
		
##########################
#
#  Copier Mode (Default=Auto)
#
##########################

	mode=* )
		mode=`parse ${i}`
		case "$mode" in
		letter )
			(
			echo " userdict /XJXsetmapmode known 
			    { 1 XJXsetmapmode } 
			    { (printerinfo setmapmode 1) =string 
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse " >> $device_tmp
			)
			;;
		photo )
			(
			echo " userdict /XJXsetmapmode known 
			    { 2 XJXsetmapmode } 
			    { (printerinfo setmapmode 0) =string 
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse " >> $device_tmp
			)
			;;	
		auto )
			(
			echo " userdict /XJXsetmapmode known
			    { 4 XJXsetmapmode }
			    { (printerinfo setmapmode 1) =string
		        FieryXJdict /ExtCmdGetExec get exec {pop} if } ifelse " >> $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
#
##########################

i=1
while [ $i -le ${copies} ]
do
(
cat $prtout_tmp
) | lp -d $REMOTE_PRINTER 

i=`expr $i + 1`
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
