#!/bin/sh
##########################
## Ricoh
## Unix Printing Interface Program.
## Last Update 12/14/2004
## Copyright 1999-2004
## All rights reserved
## Ricoh Corp.
##########################

##########################
## Debug Flag, set it to y to leave temp files in /tmp
##########################
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="$*"

#################################################
#
# Uedate in 06/23/2004
# For HP-UX11iv2, The problem is
# because of lpsched not passing the 
# copies argument to the interface script
# if the printer is configured as remote 
# printer. To find how many copies,It can be 
# find how many df* files in cfA* file, which is
# on /var/spool/lp/request/remote_printer
#
#################################################
if [ "$copies" = "" ]
then
    dfile=`basename $1`
    cfile=`echo ${dfile} | sed -e 's/^df./cfA/1' -e 's/^d[^f]/cA/g'`
    copies=`grep -e "[a-z]${dfile}$" /var/spool/lp/request/${printer}/${cfile} | wc -l`
fi

nobanner="no"
nofilebreak="no"
stty=
inlist=

Custom_Banner=0
Orientation=0
fontsize=0
papersize=0
duplex_type=0
layout=0
height=0
width=0
pitch=0
lpi=0
vmi=0
hmi=0

Text_type=0
PS_type=0
Other_type=0
is_DSC=0
is_PRNFile=0
need_file_type_detection=1
host=`hostname`


##########################
# Change the following line to match to
# the remote printer's name
##########################
REMOTE_PRINTER=

##########################
# device_tmp stores device option commands
# prtout_tmp holds actual printout to the printer
# ps_tmp is used to hold pre-processed input PS file
##########################
device_tmp="/tmp/afcdev_$$.tmp"
prtout_tmp="/tmp/afcprt_$$.tmp"
ps_tmp="/tmp/afcps_$$"
pjl_tmp="/tmp/afcpjl_$$"
pcl_tmp="/tmp/afcpcl_$$"
banner_text_tmp="/tmp/afcbanner_text_$$"
banner_ps_tmp="/tmp/afcbanner_ps_$$"

##########################
# Set default # of copies
##########################
echo "<</NumCopies $copies >>setpagedevice" >> $device_tmp
printf "\033&l%dx" $copies >> $pcl_tmp
echo "@PJL SET DISKIMAGE=OFF" >> $pjl_tmp

##########################
#
# Store some default values
#
##########################
PRINTER_NAME=
PRINTER_CONFIG_PATH=/var/spool/lpd/$printer/
PRINTER_CONFIG=$PRINTER_CONFIG_PATH/$printer.config

if [ -f "$PRINTER_CONFIG" ]
then
	. "$PRINTER_CONFIG"
fi

##########################
# Device options (e.g. -o value=a) are identified
# and processed here.
##########################
for i in ${option_list}
do
    case "${inlist}${i}" in
	nobanner|nb )
	    nobanner="yes"
	    ;;

        banner|yb )
		nobanner="no"
		;;
        
        nofilebreak )
            nofilebreak="yes"
            ;;


##########################
# Debug option
##########################
	debug )
	    DEBUG=Y
	    ;;

##########################
# Use Filter Banner Page
##########################
	fbanner )
	    Custom_Banner=1
	    ;;

##########################
# Text File Type option
##########################

	text|a|ascii )
	    Text_type=1
	    PS_type=0
	    NoFilter_type=0
	    need_file_type_detection=0
	    ;;

##########################
# PS File Type option
##########################
	ps|postscript|post )
	    Text_type=0
	    PS_type=1
	    NoFilter_type=0
	    need_file_type_detection=0
	    ;;

##########################
# Other Files Type option
##########################
	nofilter|pcl|relay )
	    Text_type=0
	    PS_type=0
	    NoFilter_type=1
	    need_file_type_detection=0
	    ;;

##########################
# End of HP_HEAD
##########################
