#!/bin/sh

##########################
##
## Ricoh UNIX Printing Solution 
## For RedHat 7 LINUX
## Copyright 1999-2001
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## Update History
##
## 8/15/2001 First Release
##
##########################

##########################
##
## Debug Flag, set to y to leave temp files in /tmp
##
##########################

DEBUG=

##########################
##
## Function parse()
## Modified for LINUX
##
##########################
parse (){
	echo $1 |awk -F= '{print $2}'
} 

copies=1

##########################
##
## -Z option handling
##
##########################

#while getopts "Z:" option_name
#while getopts "J:Z:" option_name
while getopts "A:C:D:F:H:J:L:N:P:Q:S:Z:a:b:d:e:f:h:j:k:l:n:p:r:s:t:w:x:y:" option_name
do
        case $option_name in
        Z) zopt=$OPTARG ;;
	J) infilen=$OPTARG ;;
	h) host=$OPTARG ;;
	n) user_name=$OPTARG ;;
	P) printer=$OPTARG ;;
        *) ;;
        esac
done
shift $(($OPTIND - 1))

##########################
##
## End of modification 
## For RedHat 7 LINUX
##
##########################
REMOTE_PRINTER=

device_tmp=
prtout_tmp=
ps_tmp=

is_PS=0
is_TXT=0
is_DSC=0
#is_PS=`file $infilen | grep -c PostScript`
# ps file detection in Linux sporadically fail... unknown reason
# this bug is fixed with the 15 lines below.

##########################
#
# 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_$$"

cat - > $ps_tmp
is_PS=`file $ps_tmp | grep -c PostScript`
is_TXT=`file $ps_tmp | grep -c text`

##########################
#
# Device Options Processing
# -Z options are stored in the variable $zopt
# Format: optname=value,optname=value,...(no white space)
#
##########################

for i in `echo $zopt | tr "," " "`
do
	case $i in

	nobanner )
		nobanner="yes"
		;;

	nofilebreak )
		nofilebreak="yes"
		;;

##########################
#
# Debug option
#
##########################

	debug ) 
		DEBUG=Y
		;;		

##########################
#
# Text option
#
##########################

	text ) 
		is_TXT=1
		is_PS=0
		;;

##########################
#
# PS option
#
##########################

	ps )
		is_PS=1
		is_TXT=0
		;;

##########################
#
# PCL option
#
##########################

	pcl|nofilter )
		is_TXT=0
		is_PS=0
		;;

# number of copies

	nn=* )
		copies=`parse ${i}`
		echo "<</NumCopies $copies>>setpagedevice" >> $device_tmp
		printf "\033&l%dx" $copies >> $pcl_tmp
		;;
