#!/bin/ksh
##########################
#
# Unix Printing Interface Program for IBM AIX.
# COPYRIGHT (C) 1999-2009 RICOH COMPANY,LTD.
# All rights reserved
#
##########################

##########################
##
## Update History
##
## 07/28/2005 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=
banner_pjl_tmp=
pcl_tmp=
banner_pcl_tmp=
TXT=
PS=
PCL=
is_PS=
is_DSC=
is_TXT=
copies=1

is_PRNFile=0
is_PDFFile=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
vsi=0
hsi=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
#
##########################
##########################
#
# Update 05/18/2006
#
##########################
prt_date=`date +%y%m%d%H%M%S`

device_tmp="/tmp/afcdev_$prt_date$$.tmp"
prtout_tmp="/tmp/afcprt_$prt_date$$.tmp"
ps_tmp="/tmp/afcps_$prt_date$$"
pjl_tmp="/tmp/afcpjl_$prt_date$$"
pcl_tmp="/tmp/afcpcl_$prt_date$$"
banner_text_tmp="/tmp/afcbanner_text_$prt_date$$"
banner_ps_tmp="/tmp/afcbanner_ps_$prt_date$$"

##########################
#
# Update 07/28/2005
# add buffers for Banner page
#
##########################
banner_pjl_tmp="/tmp/afcbanner_pjl_$prt_date$$"
banner_pcl_tmp="/tmp/afcbanner_pcl_$prt_date$$"
banner_page_tmp="/tmp/afcbanner_page_$prt_date$$"

##########################
#
# 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|pdf|raw )
		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
##############################

##########################
# End of aix_head
##########################
