#!/bin/ksh
##########################
##
## Unix Printing Interface Program for IBM AIX.
## Last Update  08/19/2003
## Copyright 1999-2003
## All rights reserved
## Ricoh Engineering
##
##########################

##########################
##
## Update History
##
## 10/31/2001 First Release
##
##########################

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

DEBUG=

##########################
##
## Parse arguments passed as [attribute]=[value]
##
##########################

parse () {
	echo "`expr \"$1\" : \"^[^=]*=\(.*\)\"`"
}

##########################
#
# This program is invoked as
#
# [name-of-this-filter] option1, option2, option3 ... filename
#
##########################

host=`hostname`
printer=`basename \`expr $0 : '\(.*\)\.'\``
request_id=
user_name=`whoami`
title=
copies=
option_list=
OPTIONS=

#######################
#
# option handling 08/19/2003
#
#######################

set -- `getopt S:P:RN:Lqx#:u:Xo:T:CD: $*`
if [ $? != 0 ];
then
	exit 1
fi

while [ "$1" != "--" ]
do
	case $1 in
	'-#')
		request_id=$2
		OPTIONS="$OPTIONS $1 $2"
		shift
		;;
	-o)     
		option_list="$option_list $2"
		shift
		;;
	*)
		OPTIONS="$OPTIONS $1"
		if [ -n "$2" ]; then
			OPTIONS="$OPTIONS $2"
			shift
		fi
		;;
	esac
	shift
done
shift   	# skip --
files=$*

#############################
#
# Control & Request 08/11/2003
#
#############################

if [ -z "$files" ]
then
	/usr/lib/lpd/rembak $OPTIONS $prtout_tmp
	exit $?
fi

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

device_tmp=
prtout_tmp=
ps_tmp=
pjl_tmp=
pcl_tmp=
TXT=
PS=
PCL=
is_PS=
is_DSC=
is_TXT=
copies=1

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

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

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

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

#OS_NAME=`uname -s`

#is_PS=1
#is_PS=`file $* | grep -c PostScript`

##########################
##########################
#
# Device options (e.g. -o value=a) are identified 
# and processed here.
#
##########################
for i in ${option_list} 
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 ) 
		TXT=1
		;;

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

	ps|postscript|post )
		PS=1
		;;

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

	pcl|nofilter|relay )
		PCL=1
		;;
##########################
#
# number of copies
#
##########################
	nn=* )
		copies=`parse ${i}`

		echo "<</NumCopies $copies>>setpagedevice" >> $device_tmp
		printf "\033&l%dx" $copies >> $pcl_tmp
		;;
#end of copies
##############################
