#!/bin/sh

##########################
##
## Ricoh UNIX Printing Solution 
## For RedHat 7 and SuSe 9.x LINUX
## Copyright 1999-2004
## For RE_0215 Case
## Up to date at 02/24/2004
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## 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:T: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
	J) infilen=$OPTARG ;;
	P) printer=$OPTARG ;;
	T) title=$OPTARG ;;
        Z) zopt=$OPTARG ;;
	f) files=$OPTARG ;;
	h) host=$OPTARG ;;
	j) request_id=$OPTARG ;;
	n) user_name=$OPTARG ;;
        *) ;;
        esac
done
shift $(($OPTIND - 1))

##########################
##
## End of modification 
## For RedHat 7 LINUX
##  Seong: For 2up implementation, several variables are added.
##
##########################
REMOTE_PRINTER=

is_PS=0
is_TXT=0
is_DSC=0
is_PRNFile=0
Custom_Banner=0
Orientation=0
papersize=0
fontsize=0
duplex_type=0
layout=0
height=0
width=0
pitch=0
lpi=0
vmi=0
hmi=0

##########################
#
# Store some default values
#  Seong: Added for Configuration(6/3/2003).
#
##########################
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_tmp stores device option commands
# prtout_tmp holds actual printout to the printer
# ps_tmp is used to hold pre-processed input PS file
#  Seong: The ESC control code has been added in UEL command.
#
##########################
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_$$"

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

is_PRNFile=`cat $ps_tmp | grep -ic "%-12345X"`
##########################
#
# 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|nb )
		nobanner="yes"
		;;

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

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

	debug ) 
		DEBUG=Y
		;;		

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

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

	text|a|ascii ) 
		is_TXT=1
		is_PS=0
		;;

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

	ps|postscript|post )
		is_PS=1
		is_TXT=0
		;;

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

	nofilter|pcl|relay )
		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
		;;
