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

##########################
##
## Update History
##
## 01/14/2002 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
#
# Duplex (Default=OFF)
#
##########################
	
	duplex=* )
		duplex=`parse ${i}`
		case "$duplex" in
		off )
			(
			echo "false XJXsetduplex userdict /XJXsetManualDuplex known
                        { 0 XJXsetManualDuplex } if " >> $device_tmp
			)
			;;
		side )
			(
			echo "true XJXsetduplex << /Tumble false >> setpagedevice
                        userdict /XJXsetManualDuplex known
{ 0 XJXsetManualDuplex } if " >> $device_tmp
			)
			;;
		top )
			(
			echo "true XJXsetduplex << /Tumble true >> setpagedevice
                        userdict /XJXsetManualDuplex known { 0 XJXsetManualDuplex } if " >> $device_tmp
			)
			;;			
		esac
		;;

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

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

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

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

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

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

		traymd )
			(
			echo " /XJXsettraysel where { pop 254 XJXsettraysel } if " >> $device_tmp
			)
			;;			
		esac
		;;
		
##########################
#
# Rotate (Default=No)
#
##########################
	
	rotate=* )
		rotate=`parse ${i}`
		case "$rotate" in
		yes )
			(
			echo " userdict /EFUserRotate180 known
   			{ 1 EFUserRotate180 } if " >> $device_tmp
			)
			;;						
		no )
			(
			echo " userdict /EFUserRotate180 known
   			{ 0 EFUserRotate180 } 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
		;;
		
		
##########################
#
#  Staple Option
#
##########################

	staple=* )
		staple=`parse ${i}`
		case "$staple" in
		off )
			(
			echo " userdict /XJXsetstapler known
   			{ 0 XJXsetstapler } if ">> $device_tmp
			)
			;;

		on )
			(
			echo " /XJXsetstaple where {pop 1 XJXsetstaple} if " >> $device_tmp
			)
			;;
		esac
		;;

##########################
#
#  Sorter Option
#
##########################

	sorter=* )
		sorter=`parse ${i}`
		case "$sorter" in
		off )
			(
			echo " << /Collate false>> setpagedevice
                        /XJXsetsorter where { pop 0 XJXsetsorter } if ">> $device_tmp
			)
			;;

		collate )
			(
			echo " << /Collate true >> setpagedevice
                        /XJXsetsorter where { pop 0 XJXsetsorter } if " >> $device_tmp
			)
			;;	
		group )
			(
			echo "
<< /Collate false>> setpagedevice
   			/XJXsetsorter where { pop 1 XJXsetsorter } if " >> $device_tmp
			)
			;;							
		sort )
			(
			echo " << /Collate true >> setpagedevice
   			/XJXsetsorter where { pop 1 XJXsetsorter } if " >> $device_tmp
			)
			;;
		esac
		;;
		
##########################
#
#  Print Order (Default=forward)
#
##########################

	order=* )
		order=`parse ${i}`
		case "$order" in
		forward )
			(
			echo " userdict /XJXsetprintorder known
			    { 0 XJXsetprintorder } if " >> $device_tmp
			)
			;;
		
		reverse )
			(
			echo " userdict /XJXsetprintorder known
			    { 1 XJXsetprintorder } if " >> $device_tmp
			)
			;;
		esac
		;;


##########################
#
# Paper Size (Default=Letter)
#
##########################
	
	paper=* )
		paper=`parse ${i}`
		case "$paper" in
		letter )
			(
			echo " /XJXsetpagesize where { pop (Letter) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		legal )
			(
			echo " /XJXsetpagesize where { pop (Legal) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A4 )
			(
			echo " /XJXsetpagesize where { pop (A4) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A3 )
			(
			echo " /XJXsetpagesize where { pop (A3) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A5 )
			(
			echo " /XJXsetpagesize where { pop (A5) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		tabloid )
			(
			echo " /XJXsetpagesize where { pop (Tabloid) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		halfletter )
			(
			echo " /XJXsetpagesize where { pop (SEFLetter) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A5SEF )
			(
			echo "
/XJXsetpagesize where { pop (SEFA5) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A6 )
			(
			echo "
/XJXsetpagesize where { pop (A6) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A6SEF )
			(
			echo " /XJXsetpagesize where { pop (A6) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		legal13 )
			(
			echo " /XJXsetpagesize where { pop (Legal13) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		letterSEF )
			(
			echo " /XJXsetpagesize where { pop (SEFLetter) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A4SEF )
			(
			echo " /XJXsetpagesize where { pop (SEFA4) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		B4 )
			(
			echo " /XJXsetpagesize where { pop (B4) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		B5 )
			(
			echo " /XJXsetpagesize where { pop (B5) XJXsetpagesize } if " >> $device_tmp
			)
			;;		
		B5SEF )
			(
			echo " /XJXsetpagesize where { pop (B5SEF) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		B6SEF )
			(
			echo " /XJXsetpagesize where { pop (SEFB6) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		postcard )
			(
			echo " /XJXsetpagesize where { pop (PostcardJ) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		doublepostcard )
			(
			echo " /XJXsetpagesize where { pop (DoublePostcardRotated) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		tabloidextra )
			(
			echo " /XJXsetpagesize where { pop (TabloidExtra) XJXsetpagesize } if " >> $device_tmp
			)
			;;		
						
		esac
		;;

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

	media=* )
		media=`parse ${i}`
		case "$media" in
		plain )
			(
			echo " userdict /XJXsetmediatype known
			    { 1 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		thick1 )
			(
			echo " userdict /XJXsetmediatype known
                        { 3 XJXsetmediatype } if " >> $device_tmp
			)
			;;
			
		transparent )
			(
			echo " userdict /XJXsetmediatype known
                        { 2 XJXsetmediatype } 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
			)
			;;	
		absolute )
			(
			echo " userdict /XJXsetrenderingintent known
   			{ (Absolute) XJXsetrenderingintent } if " >> $device_tmp
			)
			;;
		relative )
			(
			echo " userdict /XJXsetrenderingintent known
   			{ (Relative) XJXsetrenderingintent } if " >> $device_tmp
			)
			;;						
		esac
		;;
		
##########################
#
#  Copier Mode (Default=Auto)
#
##########################

	mode=* )
		mode=`parse ${i}`
		case "$mode" in
		map )
			(
			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
#
##########################

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

#i=1
#while [ $i -le ${copies} ]
#do
#(
#cat $prtout_tmp
#) | lp -d $REMOTE_PRINTER 
#
#i=`expr $i + 1`
#done
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
