#!/bin/sh
##########################
##
## Ricoh Unix Printing Interface Program
## 	for Aficio 551/700 EB-70
##
## 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 "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
		;;

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

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

		off )
			(
			echo " userdict /XJXsetstapler known
{ 0 XJXsetstapler } if " >> $device_tmp
			)
			;;
		booklet )
			(
			echo " /XJXsetstaple where {pop 10 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		left )
			(
			echo " /XJXsetstaple where {pop 11 XJXsetstaple} if " >> $device_tmp
			)
			;;							
		lefts )
			(
			echo " /XJXsetstaple where {pop 8 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		leftv )
			(
			echo " /XJXsetstaple where {pop 6 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		lefth )
			(
			echo " /XJXsetstaple where {pop 1 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		left2 )
			(
			echo " /XJXsetstaple where {pop 3 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		top2 )
			(
			echo " /XJXsetstaple where {pop 5 XJXsetstaple} if " >> $device_tmp
			)
			;;
		right )
			(
			echo " /XJXsetstaple where {pop 12 XJXsetstaple} if " >> $device_tmp
			)
			;;					
		rights )
			(
			echo " /XJXsetstaple where {pop 9 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		rightv )
			(
			echo " /XJXsetstaple where {pop 7 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		righth )
			(
			echo " /XJXsetstaple where {pop 2 XJXsetstaple} if " >> $device_tmp
			)
			;;	
		right2 )
			(
			echo " /XJXsetstaple where {pop 4 XJXsetstaple} if " >> $device_tmp
			)
			;;							
		esac
		;;	
		
########################## 
#
# Input Tray Selection					
#
##########################

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

		tray1 )
			(
			echo " /XJXsettraysel where
{ pop 1 XJXsettraysel } if" >> $device_tmp
			)
			;;
		tray2 )
			(
			echo " /XJXsettraysel where
{ pop 4 XJXsettraysel } if" >> $device_tmp
			)
			;;
		tray3 )
			(
			echo " /XJXsettraysel where
{ pop 5 XJXsettraysel } if" >> $device_tmp
			)
			;;
		lct )
			(
			echo " /XJXsettraysel where { pop 20 XJXsettraysel } if" >> $device_tmp
			)
			;;
		bypass )
			(
			echo " /XJXsettraysel where
{ pop 8 XJXsettraysel } if" >> $device_tmp
			)
			;;
		auto )
			(
			echo " /XJXsettraysel where
{ pop 7 XJXsettraysel } if" >> $device_tmp
			)
			;;			
		esac
		;;
		
##########################
#
#  Punch
#
##########################		

	punch=* )
		punch=`parse ${i}`
		case "$punch" in
		off )
			(
			echo "
/XJXsetpunch where { pop 0 XJXsetpunch } if " >> $device_tmp
			)
			;;
		left )
			(
			echo "
/XJXsetpunch where { pop 1 XJXsetpunch } if " >> $device_tmp
			)
			;;			
		top )
			(
			echo "
/XJXsetpunch where { pop 2 XJXsetpunch } if " >> $device_tmp
			)
			;;
		right )
			(
			echo "
/XJXsetpunch where { pop 3 XJXsetpunch } if " >> $device_tmp
			)
			;;	
		esac
		;;

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

	mediatype=* )
		mediatype=`parse ${i}`
		case "$mediatype" in
		plain )
			(
			echo " userdict /XJXsetmediatype known
   			{ 0 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		recycled )
			(
			echo " userdict /XJXsetmediatype known
 			  { 105 XJXsetmediatype } if" >> $device_tmp
			)
			;;	
		special )
			(
			echo " userdict /XJXsetmediatype known
  			 { 2 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		transparency )
			(
			echo " userdict /XJXsetmediatype known
  			 { 4 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		translucent )
			(
			echo " userdict /XJXsetmediatype known
   			{ 140 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		thick )
			(
			echo " userdict /XJXsetmediatype known
			  { 108 XJXsetmediatype } if" >> $device_tmp
			)
			;;
		tabstock )
			(
			echo " userdict /XJXsetmediatype known
 			  { 130 XJXsetmediatype } if" >> $device_tmp
			)
			;;	
		user1 )
			(
			echo " userdict /XJXsetmediatype known
 			  { 162 XJXsetmediatype } if" >> $device_tmp
			)
			;;	
		user2 )
			(
			echo " userdict /XJXsetmediatype known
 			  { 163 XJXsetmediatype } if" >> $device_tmp
			)
			;;											
		esac
		;;
				
##########################
#
#  Collate Option
#
##########################

	collate=* )
		collate=`parse ${i}`
		case "$collate" in
		default )
			(
			echo " userdict /FieryXJdict known
{
FieryXJdict /XJDefaultCollate known
{
			/XJXsetcollate where { pop FieryXJdict /XJDefaultCollate get exec XJXsetcollate } if
} if } if " >> $device_tmp
			)
			;;
		off )
			(
			echo " /XJXsetcollate where { pop false XJXsetcollate } if " >> $device_tmp
			)
			;;	
		on )
			(
			echo " /XJXsetcollate where { pop true XJXsetcollate } if " >> $device_tmp
			)
			;;
		esac
		;;
		
##########################
#
#  Out Bin
#
##########################

	outbin=* )
		outbin=`parse ${i}`
		case "$outbin" in
		default )
			(
			echo "
/XJXsetOutputBin where { pop 0 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		external )
			(
			echo "
/XJXsetOutputBin where { pop 2 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		shift )
			(
			echo "
/XJXsetOutputBin where { pop 1 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		proof )
			(
			echo "
/XJXsetOutputBin where { pop 2 XJXsetOutputBin } if " >> $device_tmp
			)
			;;	
		mboxproof )
			(
			echo "
/XJXsetOutputBin where { pop 4 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		mbin1 )
			(
			echo "
/XJXsetOutputBin where { pop 5 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		mbin2 )
			(
			echo "
/XJXsetOutputBin where { pop 6 XJXsetOutputBin } if " >> $device_tmp
			)
			;;		
		mbin3 )
			(
			echo "
/XJXsetOutputBin where { pop 7 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		mbin4 )
			(
			echo "
/XJXsetOutputBin where { pop 8 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		mbin5 )
			(
			echo "
/XJXsetOutputBin where { pop 9 XJXsetOutputBin } if " >> $device_tmp
			)
			;;		
		mbin6 )
			(
			echo "
/XJXsetOutputBin where { pop 10 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		mbin7 )
			(
			echo "
/XJXsetOutputBin where { pop 11 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		mbin8 )
			(
			echo "
/XJXsetOutputBin where { pop 12 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		mbin9 )
			(
			echo "
/XJXsetOutputBin where { pop 13 XJXsetOutputBin } if " >> $device_tmp
			)
			;;																
		esac
		;;

##########################
#
# First Page
#
##########################
	
	first=* )
		first=`parse ${i}`
		case "$first" in
		off )
			(
			echo "
/XJXsetFirstPageOpt where { pop 0 XJXsetFirstPageOpt } if " >> $device_tmp
			)
			;;
		on )
			(
			echo "
/XJXsetFirstPageOpt where { pop 1 XJXsetFirstPageOpt } if " >> $device_tmp
			)
			;;			
		esac
		;;

##########################
#
# Slip Sheet
#
##########################
	
	slipsheet=* )
		slipsheet=`parse ${i}`
		case "$slipsheet" in
		off )
			(
			echo " userdict /XJXsetSlipSheet known
 			  { 0 XJXsetSlipSheet }
 			  { (printerinfo SlipSheet 0) =string
 			  FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			)
			;;
		blank )
			(
			echo " userdict /XJXsetSlipSheet known
 			  { 1 XJXsetSlipSheet }
 			  { (printerinfo SlipSheet 1) =string
 			  FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			)
			;;	
		printed )
			(
			echo " userdict /XJXsetSlipSheet known
 			  { 2 XJXsetSlipSheet }
 			  { (printerinfo SlipSheet 2) =string
 			  FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			)
			;;					
		esac
		;;

##########################
#
# First Page Tray
#
##########################
	
	firstpagetray=* )
		firstpagetray=`parse ${i}`
		case "$firstpagetray" in
		auto )
			(
			echo "
/XJXsetFirstPageTray where { pop 7 XJXsetFirstPageTray } if" >> $device_tmp
			)
			;;
		tray1 )
			(
			echo "
/XJXsetFirstPageTray where { pop 1 XJXsetFirstPageTray } if" >> $device_tmp
			)
			;;	
		tray2 )
			(
			echo "
/XJXsetFirstPageTray where { pop 4 XJXsetFirstPageTray } if" >> $device_tmp
			)
			;;					
		tray3 )
			(
			echo "
/XJXsetFirstPageTray where { pop 5 XJXsetFirstPageTray } if" >> $device_tmp
			)
			;;	
		lct )
			(
			echo "
/XJXsetFirstPageTray where { pop 20 XJXsetFirstPageTray } if" >> $device_tmp
			)
			;;		
		bypass )
			(
			echo "
/XJXsetFirstPageTray where { pop 8 XJXsetFirstPageTray } if" >> $device_tmp
			)
			;;	
		esac
		;;

##########################
#
#  First Page Media
#
##########################

	firstpagemedia=* )
		firstpagemedia=`parse ${i}`
		case "$firstpagemedia" in
		plain )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 0 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;
		recycled )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 105 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;	
		special )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 2 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;
		transparency )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 4 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;
		translucent )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 140 XJXsetFirstmediatype } if"
 >> $device_tmp
			)
			;;
		thick )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 108 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;
		tabstock )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 130 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;		
		user1 )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 162 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;
		user2 )
			(
			echo " userdict /XJXsetFirstmediatype known
{ 163 XJXsetFirstmediatype } if" >> $device_tmp
			)
			;;								
		esac
		;;
						
##########################
#
# Paper Size 
#
##########################
	
	paper=* )
		paper=`parse ${i}`
		case "$paper" in
		letter )
			(
			echo "
/XJXsetPrintSize where { pop (Letter) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		legal )
			(
			echo "
/XJXsetPrintSize where { pop (Legal) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		tabloid )
			(
			echo "
/XJXsetPrintSize where { pop (Tabloid) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		executive )
			(
			echo "
/XJXsetPrintSize where { pop (SEFExecutive) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		A4 )
			(
			echo "
/XJXsetPrintSize where { pop (A4) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		B5 )
			(
			echo "
/XJXsetPrintSize where { pop (B5) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		A3 )
			(
			echo "
/XJXsetPrintSize where { pop (A3) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		A5 )
			(
			echo "
/XJXsetPrintSize where { pop (A5) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		B4 )
			(
			echo "
/XJXsetPrintSize where { pop (B4) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		statement )
			(
			echo "
/XJXsetPrintSize where { pop (Statement) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		8x13 )
			(
			echo "
/XJXsetPrintSize where { pop (SEF4) XJXsetPrintSize } if " >> $device_tmp
			)
			;;
		825x13 )
			(
			echo "
/XJXsetPrintSize where { pop (SEFFolio) XJXsetPrintSize } if " $device_tmp
			)
			;;
		85x13 )
			(
			echo "
/XJXsetPrintSize where { pop (SEFFanFoldGermanLegal) XJXsetPrintSize } if " >> $device_tmp
			)
			;;		
		8k )
			(
			echo "
/XJXsetPrintSize where { pop (R8KSEF) XJXsetPrintSize } if " >> $device_tmp
			)
			;;		
			
		16k )
			(
			echo "
/XJXsetPrintSize where { pop (R16K) XJXsetPrintSize } if " >> $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
#
##########################

cat $prtout_tmp | lp -d $REMOTE_PRINTER 

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