#!/bin/sh
##########################
##
## Aficio 6513 E-710
## 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
			)
			;;
			
		sidebinding )
			(
			echo "true XJXsetduplex
   			<< /Tumble false >> setpagedevice
   			userdict /XJXsetManualDuplex known
   			{ 0 XJXsetManualDuplex } if " >> $device_tmp
			)
			;;
			
		topbinding )
			(
			echo "true XJXsetduplex
   			<< /Tumble true >> setpagedevice
   			userdict /XJXsetManualDuplex known
   			{ 0 XJXsetManualDuplex } if " >> $device_tmp
			)
			;;
						
		esac
		;;

##########################
#
# Staple (Default=off)
#
##########################

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

		off )
			(
			echo " userdict /XJXsetstapler known { 0 XJXsetstapler } if " >> $device_tmp
			)
			;;

		tright )
			(
			echo " userdict /XJXsetstapler known { 3 XJXsetstapler } if " >> $device_tmp
			)
			;;	
			
		tleft )
			(
			echo " userdict /XJXsetstapler known { 4 XJXsetstapler } if " >> $device_tmp
			)
			;;	

		2centers )
			(
			echo " userdict /XJXsetstapler known { 7 XJXsetstapler } 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
			)
			;;

		lct )
			(
			echo " /XJXsettraysel where { pop 253 XJXsettraysel } if " >> $device_tmp
			)
			;;

		traymanual )
			(
			echo " /XJXsettraysel where { pop 2 XJXsettraysel } if " >> $device_tmp
			)
			;;
			
		traymd )
			(
			echo " /XJXsettraysel
where { pop 254 XJXsettraysel } if " >> $device_tmp
			)
			;;
						
		auto )
			(
			echo " /XJXsettraysel where
   			{ pop 7 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
		;;

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

	mediatype=* )
		mediatype=`parse ${i}`
		case "$mediatype" in
		plain )
			(
			echo " userdict /XJXsetmediatype known
			{ 1 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		thick1 )
			(
			echo " userdict /XJXsetmediatype known
                        { 3 XJXsetmediatype } if " >> $device_tmp
			)
			;;	
		thick2 )
			(
			echo " userdict /XJXsetmediatype known
                        { 7 XJXsetmediatype } if " >> $device_tmp
			)
			;;
		transparency )
			(
			echo " userdict /XJXsetmediatype known
                        { 2 XJXsetmediatype } if " >> $device_tmp
			)
			;;
		esac
		;;
				
##########################
#
#  Sort / Collate Option
#
##########################

	sort=* )
		sort=`parse ${i}`
		case "$sort" in
		collate )
			(
			echo " << /Collate true >> setpagedevice
                        /XJXsetsorter where { pop 0 XJXsetsorter } if " >> $device_tmp
			)
			;;
		sort )
			(
			echo "
<< /Collate true >> setpagedevice
   			/XJXsetsorter where { pop 1 XJXsetsorter } if " >> $device_tmp
			)
			;;
		stack )
			(
			echo "
<< /Collate false>> setpagedevice
   			/XJXsetsorter where { pop 1 XJXsetsorter } if " >> $device_tmp
			)
			;;
		no )
			(
			echo "
<< /Collate false>> setpagedevice
			/XJXsetsorter where { pop 0 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
		;;

##########################
#
# Rotate 180 (Default=off)
#
##########################
	
	rotate=* )
		rotate=`parse ${i}`
		case "$rotate" in
		on )
			(
			echo " userdict /EFUserRotate180 known
   			{ 1 EFUserRotate180 } if " >> $device_tmp
			)
			;;						
		off )
			(
			echo " userdict /EFUserRotate180 known
   			{ 0 EFUserRotate180 } if " >> $device_tmp
			)
			;;
		esac
		;;

##########################
#
# Rendering Dictionary
#
##########################
	
	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
		;;

##########################
#
# Paper Size 
#
##########################
	
	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
			)
			;;
		tabloid )
			(
			echo " /XJXsetpagesize where { pop (Tabloid) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A3 )
			(
			echo " /XJXsetpagesize where { pop (A3) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A5 )
			(
			echo " /XJXsetpagesize where { pop (A5) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A5SEF )
			(
			echo " /XJXsetpagesize where { pop (SEFA5) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		A6 )
			(
			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 " userdict /XJXsetpagesize known { (B5) XJXsetpagesize } if " >> $device_tmp
			)
			;;		
			
		B5SEF )
			(
			echo " userdict /XJXsetpagesize known { (B5SEF) XJXsetpagesize } if " >> $device_tmp
			)
			;;		
			
		B6SEF )
			(
			echo " /XJXsetpagesize where { pop (SEFB6) XJXsetpagesize } if " >> $device_tmp
			)
			;;		

		postcard )
			(
			echo " userdict /XJXsetpagesize known { (PostcardJ) XJXsetpagesize } if " >> $device_tmp
			)
			;;											
			
		doublepostcard )
			(
			echo " /XJXsetpagesize where { pop (DoublePostcardRotated) XJXsetpagesize } if " >> $device_tmp
			)
			;;					

		tabloidextra )
			(
			echo " userdict /XJXsetpagesize known { (TabloidExtra) XJXsetpagesize } if " >> $device_tmp
			)
			;;	
	
		statement )
			(
			echo " userdict /XJXsetpagesize known { (Statement) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		
		statementr )
			(
			echo " /XJXsetpagesize where { pop (SEFStatement) XJXsetpagesize } if " >> $device_tmp
			)
			;;
		
		13x19 )
			(
			echo " /XJXsetpagesize where { pop (A3Nobi) XJXsetpagesize } if " >> $device_tmp
			)
			;;				
			
		esac
		;;

##########################
#
#  Color Mode
#
##########################

	mode=* )
		mode=`parse ${i}`
		case "$mode" in
		cmyk )
			(
			echo " userdict /XJXsetcolormode known
                        { (CMYK) XJXsetcolormode} if "	 >> $device_tmp
			)
			;;
			
		gray )
			(
			echo " userdict /XJXsetcolormode known
   			{ (Grayscale) XJXsetcolormode} if "	 >> $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
