#!/bin/sh
##############################################
##
## Ricoh Unix Printing Interface Program
## 	for Aficio 850/1050 EB-105e
##
## Last Update 12/09/2004
## Copyright 1999-2004 All rights reserved
## Ricoh Engineering
##
#############################################

##########################
##
## Version History
##
## 2.00 5/20/2002 Add text file support using PCL5e
## 1.00 7/20/2000 First release
##
##########################

##########################
##
## Debug Flag
##
##########################

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="$*"

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

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

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

##########################
#
# Store some default values
#
##########################

##########################
#
# Set default # of copies
#
##########################

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

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

	nofilebreak )
		nofilebreak="yes"
		;;

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

	debug )
		DEBUG=Y
		;;

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

	text )
		TXT=1
		;;

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

	ps )
		PS=1
		;;

##########################
#
# PCL/PJL option
#
##########################

	pcl|nofilter )
		PCL=1
		;;

##########################
# LNX_TUB_B
#
# Character compresser (16.5 or 16.7 per line)
#
##########################
        compress )
		printf "\033&k2S" >> $pcl_tmp
		;;

##########################
#
# Line Spacing
#
##########################
        lpi* )
		lpi=`expr $i : 'lpi\([0-9]*\)'`
#		printf "\033&l%dD" $lpi >> $pcl_tmp
		;;

##########################
#
# Pitch (character per inch)
#
##########################
	cpi* )
		pitch=`expr $i : 'cpi\([0-9.]*\)'`
		if [ -n "$pitch" ]
		then
			printf "\033(s%.2fH" $pitch >> $pcl_tmp
		fi
		;;

	duplex=* )
		duplex=`parse ${i}`
		;;

	staple=* )
		staple=`parse ${i}`
		;;

	itray=* )
		itray=`parse ${i}`
		;;

	punch=* )
		punch=`parse ${i}`
		;;

	mediatype=* )
		mediatype=`parse ${i}`
		;;

	collate=* )
		collate=`parse ${i}`
		;;

	outbin=* )
		outbin=`parse ${i}`
		;;

	first=* )
		first=`parse ${i}`
		;;

	slipsheet=* )
		slipsheet=`parse ${i}`
		;;

	firstpagetray=* )
		firstpagetray=`parse ${i}`
		;;

	firstpagemedia=* )
		firstpagemedia=`parse ${i}`
		;;

	landscape=* )
		landscape=`parse ${i}`
		;;

	rotate180=* )
		rotate180=`parse ${i}`
		;;

	smoothing=* )
		smoothing=`parse ${i}`
		;;

	toner=* )
		toner=`parse ${i}`
		;;

	paper=* )
		paper=`parse ${i}`
		;;
	esac
done

##############################################################
#
# Check some relational conditions here:
#
##############################################################

if [ "$itray" = "bypass" ]
then
	staple=off
	punch=off
fi

if [ -n "$staple" ]
then
	if [ "$staple" != "off" ]
	then
		collate="on"
	fi
fi

if [ -n "$firstpagetray" -o -n "$firstpagemedia" ]
then
	if [ -z "$first" ]
	then
		first="on"
	fi
fi

if [ -n "$slipsheet" -a "$slipsheet" != "off" ]
then
	itray=tray2
	mediatype=transparency
fi

##############################################################
#
# The order below is somehow important for some models:
# 	have to put lpi after orientation
#
##############################################################

##########################
#
# Paper Orientation (Default=Portrait)
#
##########################

	case "$landscape" in

	yes )
		case "$rotate180" in

		yes )
			printf "\033&l3O" >> $pcl_tmp
			;;

		* )
			printf "\033&l1O" >> $pcl_tmp
			;;

		esac
		;;

	* )
		case "$rotate180" in

		yes )
			printf "\033&l2O" >> $pcl_tmp
			;;

		* )
			printf "\033&l0o" >> $pcl_tmp
			;;

		esac
		;;

	esac

##########################
#
# Line Spacing
#
##########################

	if [ -n "$lpi" ]
	then
		printf "\033&l%dD" $lpi >> $pcl_tmp
	fi

##########################
#
# Slip Sheet (Default=Off)
#
##########################

		case "$slipsheet" in
		off )
			(
			echo " userdict /XJXsetSlipSheet known
			   { 0 XJXsetSlipSheet }
			   { (printerinfo SlipSheet 0) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SLIPSHEET=GENERICOFF" >> $pjl_tmp
			)
			;;
		blank )
			(
			echo " userdict /XJXsetSlipSheet known
			   { 1 XJXsetSlipSheet }
			   { (printerinfo SlipSheet 1) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SLIPSHEET=BLANK" >> $pjl_tmp
			)
			;;
		printed )
			(
			echo " userdict /XJXsetSlipSheet known
			   { 2 XJXsetSlipSheet }
			   { (printerinfo SlipSheet 2) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SLIPSHEET=PRINT" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " userdict /XJXsetSlipSheet known
			   { 0 XJXsetSlipSheet }
			   { (printerinfo SlipSheet 0) =string
			   FieryXJdict /ExtCmdGetExec get exec { pop } if } ifelse " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SLIPSHEET=GENERICOFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
# Landscape (Default=No)
#
##########################

		case "$landscape" in

		no )
			(
			echo "userdict /EFLandscapeSetup known { 0 EFLandscapeSetup } if " >> $device_tmp
			)
			;;

		yes )
			(
			echo "userdict /EFLandscapeSetup known { 1 EFLandscapeSetup } if " >> $device_tmp
			)
			;;

		* )
			(
			echo "userdict /EFLandscapeSetup known { 0 EFLandscapeSetup } if " >> $device_tmp
			)
			;;

		esac

##########################
#
# Input Tray Selection (Default=AutoSelect)
#
##########################

		case "$itray" in

		tray1 )
			(
			echo " /XJXsettraysel where
			   { pop 1 XJXsettraysel } if " >> $device_tmp
			printf "\033&l1H" >> $pcl_tmp
			)
			;;
		tray2 )
			(
			echo " /XJXsettraysel where
			   { pop 4 XJXsettraysel } if " >> $device_tmp
			printf "\033&l4H" >> $pcl_tmp
			)
			;;
		tray3 )
			(
			echo " /XJXsettraysel where
			   { pop 5 XJXsettraysel } if " >> $device_tmp
			printf "\033&l5H" >> $pcl_tmp
			)
			;;
		tray4 )
			(
			echo " /XJXsettraysel where
			   { pop 20 XJXsettraysel } if " >> $device_tmp
			printf "\033&l20H" >> $pcl_tmp
			)
			;;
		tray5 )
			(
			echo " /XJXsettraysel where
			   { pop 21 XJXsettraysel } if " >> $device_tmp
			printf "\033&l21H" >> $pcl_tmp
			)
			;;
		tray6 )
			(
			echo " /XJXsettraysel where
			   { pop 22 XJXsettraysel } if " >> $device_tmp
			printf "\033&l22H" >> $pcl_tmp
			)
			;;
		auto )
			(
			echo " /XJXsettraysel where
			   { pop 7 XJXsettraysel } if " >> $device_tmp
			printf "\033&l7H" >> $pcl_tmp
			)
			;;
		* )
			printf "\033&l7H" >> $pcl_tmp
			;;
		esac

##########################
#
# First Page (Default=Off)
#
##########################

		case "$first" in
		off )
			(
			echo " /XJXsetFirstPageOpt where { pop 0 XJXsetFirstPageOpt } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEOPT = GENERICOFF" >> $pjl_tmp
			)
			;;
		on )
			(
			echo " /XJXsetFirstPageOpt where { pop 1 XJXsetFirstPageOpt } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEOPT = GENERICON" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetFirstPageOpt where { pop 0 XJXsetFirstPageOpt } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEOPT = GENERICOFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
# First Page Tray (Default=Auto)
#
##########################

		case "$firstpagetray" in
		auto )
			(
			echo " /XJXsetFirstPageTray where { pop 7 XJXsetFirstPageTray } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=7" >> $pjl_tmp
			)
			;;
		tray1 )
			(
			echo " /XJXsetFirstPageTray where { pop 1 XJXsetFirstPageTray } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=1" >> $pjl_tmp
			)
			;;
		tray2 )
			(
			echo " /XJXsetFirstPageTray where { pop 4 XJXsetFirstPageTray } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=4" >> $pjl_tmp
			)
			;;
		tray3 )
			(
			echo " /XJXsetFirstPageTray where { pop 5 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=5" >> $pjl_tmp
			)
			;;
		tray4 )
			(
			echo " /XJXsetFirstPageTray where { pop 20 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=20" >> $pjl_tmp
			)
			;;
		tray5 )
			(
			echo " /XJXsetFirstPageTray where { pop 21 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=21" >> $pjl_tmp
			)
			;;
		tray6 )
			(
			echo " /XJXsetFirstPageTray where { pop 22 XJXsetFirstPageTray } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=22" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetFirstPageTray where { pop 7 XJXsetFirstPageTray } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGETRAY=7" >> $pjl_tmp
			)
			;;
		esac

##########################
#
#  First Page Media (Default=Plain)
#
##########################

		case "$firstpagemedia" in
		plain )
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 0 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=0" >> $pjl_tmp
			)
			;;
		recycled )
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 105 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=105" >> $pjl_tmp
			)
			;;
		special )
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 2 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=2" >> $pjl_tmp
			)
			;;
		transparency )
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 4 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=4" >> $pjl_tmp
			)
			;;
		translucent)
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 140 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=140" >> $pjl_tmp
			)
			;;
		thick )
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 108 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=108" >> $pjl_tmp
			)
			;;
		tabstock)
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 130 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=130" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " userdict /XJXsetFirstmediatype known
			   { 0 XJXsetFirstmediatype } if" >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET FIRSTPAGEMEDIATYPE=0" >> $pjl_tmp
			)
			;;
		esac

##########################
#
#  Out Bin (Default=Printer's Default)
#
##########################

		case "$outbin" in
		default )
			(
			echo " /XJXsetOutputBin where { pop 0 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		shift )
			(
			echo "/XJXsetOutputBin where { pop 1 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=UPPER" >> $pjl_tmp
			)
			;;
		proof )
			(
			echo " /XJXsetOutputBin where { pop 2 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=LOWER" >> $pjl_tmp
			)
			;;
		booklet )
			(
			echo " /XJXsetOutputBin where { pop 4 XJXsetOutputBin } if " >> $device_tmp
			echo "@PJL SET OUTBIN=OPTIONALOUTBIN1" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetOutputBin where { pop 0 XJXsetOutputBin } if " >> $device_tmp
			)
			;;
		esac

##########################
#
# Staple (Default=off)
#
##########################

		case "$staple" in

		off )
			(
			echo " userdict /XJXsetstapler known
			   { 0 XJXsetstapler } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=GENERICOFF" >> $pjl_tmp
			)
			;;
		lefts )
			(
			echo " /XJXsetstaple where {pop 8 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPLEFT_SLANT" >> $pjl_tmp
			)
			;;
		leftv )
			(
			echo " /XJXsetstaple where {pop 6 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPLEFT_VER" >> $pjl_tmp
			)
			;;

		lefth )
			(
			echo " /XJXsetstaple where {pop 1 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPLEFT_HOR" >> $pjl_tmp
			)
			;;
		left2 )
			(
			echo " /XJXsetstaple where {pop 3 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=TWOLEFT" >> $pjl_tmp
			)
			;;
		top2 )
			(
			echo " /XJXsetstaple where {pop 5 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=TWOUP" >> $pjl_tmp
			)
			;;
		rights )
			(
			echo " /XJXsetstaple where {pop 9 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPRIGHT_SLANT" >> $pjl_tmp
			)
			;;
		rightv )
			(
			echo " /XJXsetstaple where {pop 7 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPRIGHT_VER" >> $pjl_tmp
			)
			;;
		righth )
			(
			echo " /XJXsetstaple where {pop 2 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=ONEUPRIGHT_HOR" >> $pjl_tmp
			)
			;;
		right2 )
			(
			echo " /XJXsetstaple where {pop 4 XJXsetstaple} if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=TWORIGHT" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " userdict /XJXsetstapler known
			   { 0 XJXsetstapler } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET STAPLE=GENERICOFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
#  Punch (Default=Punch)
#
##########################

		case "$punch" in
		off )
			(
			echo " /XJXsetpunch where { pop 0 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=GENERICOFF" >> $pjl_tmp
			)
			;;
		left )
			(
			echo " /XJXsetpunch where { pop 1 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=LEFT" >> $pjl_tmp
			)
			;;
		top )
			(
			echo " /XJXsetpunch where { pop 2 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=TOP" >> $pjl_tmp
			)
			;;
		right )
			(
			echo " /XJXsetpunch where { pop 3 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=RIGHT" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetpunch where { pop 0 XJXsetpunch } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET PUNCH=GENERICOFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
# Toner Saver (Default=Off)
#
##########################

		case "$toner" in
		off )
			(
			echo " /XJXsetTonerSave where { pop false XJXsetTonerSave } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET TONERREDUCTION = GENERICOFF" >> $pjl_tmp
			)
			;;
		on )
			(
			echo " /XJXsetTonerSave where { pop true XJXsetTonerSave } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET TONERREDUCTION = GENERICON" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetTonerSave where { pop false XJXsetTonerSave } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET TONERREDUCTION = GENERICOFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
# Edge Smoothing (Default=Off)
#
##########################

		case "$smoothing" in
		off )
			(
			echo " /XJXsetrefine where { pop 0 XJXsetrefine } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET REFINE = GENERICOFF" >> $pjl_tmp
			)
			;;
		on )
			(
			echo " /XJXsetrefine where { pop 1 XJXsetrefine } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET REFINE = GENERICON" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetrefine where { pop 0 XJXsetrefine } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET REFINE = GENERICOFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
#  Collate Option (Default=Printer's Default)
#
##########################

		case "$collate" in
		default )
			(
			echo " userdict /FieryXJdict known
			   {
			   FieryXJdict /XJDefaultCollate known
			   {
			   /XJXsetcollate where { pop FieryXJdict /XJDefaultCollate get exec XJXsetcollate } if
			   } if } if " >> $device_tmp
			)
			;;
		off )
			(
			echo " /XJXsetcollate where { pop false XJXsetcollate } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SORTERMODE=GENERICOFF" >> $pjl_tmp
			)
			;;
		on )
			(
			echo " /XJXsetcollate where { pop true XJXsetcollate } if " >> $device_tmp
			echo "@PJL COMMENT EFIPJL SET SORTERMODE=COLLATE" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " userdict /FieryXJdict known
			   {
			   FieryXJdict /XJDefaultCollate known
			   {
			   /XJXsetcollate where { pop FieryXJdict /XJDefaultCollate get exec XJXsetcollate } if
			   } if } if " >> $device_tmp
			)
			;;
		esac

##########################
#
# Duplex (Default=off)
#
##########################

		case "$duplex" in
		off )
			(
			echo "false XJXsetduplex
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if " >> $device_tmp
			echo "@PJL SET DUPLEX=OFF" >> $pjl_tmp
			)
			;;
		side )
			(
			echo "true XJXsetduplex
			   << /Tumble false >> setpagedevice
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if " >> $device_tmp
			)
				echo "@PJL SET DUPLEX=ON" >> $pjl_tmp
			case "$landscape" in
			yes )
				echo "@PJL SET BINDING=SHORTEDGE" >> $pjl_tmp
				;;
			* )
				echo "@PJL SET BINDING=LONGEDGE" >> $pjl_tmp
				;;
			esac
			;;
		top )
			(
			echo "true XJXsetduplex
			   << /Tumble true >> setpagedevice
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if " >> $device_tmp
			)
				echo "@PJL SET DUPLEX=ON" >> $pjl_tmp
			case "$landscape" in
			yes )
				echo "@PJL SET BINDING=LONGEDGE" >> $pjl_tmp
				;;
			* )
				echo "@PJL SET BINDING=SHORTEDGE" >> $pjl_tmp
				;;
			esac
			;;
		* )
			(
			echo "false XJXsetduplex
			   userdict /XJXsetManualDuplex known
			   { 0 XJXsetManualDuplex } if " >> $device_tmp
			echo "@PJL SET DUPLEX=OFF" >> $pjl_tmp
			)
			;;
		esac

##########################
#
#  Media Type (Default=Plain)
#
##########################

		case "$mediatype" in
		plain )
			(
			echo " userdict /XJXsetmediatype known { 0 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=PAPER" >> $pjl_tmp
			)
			;;
		recycled )
			(
			echo " userdict /XJXsetmediatype known
			   { 105 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=RECYCLED" >> $pjl_tmp
			)
			;;
		special )
			(
			echo " userdict /XJXsetmediatype known
			   { 2 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=SPECIAL" >> $pjl_tmp
			)
			;;
		transparency )
			(
			echo " userdict /XJXsetmediatype known
			   { 4 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=TRANSPARENCY" >> $pjl_tmp
			)
			;;
		translucent)
			(
			echo " userdict /XJXsetmediatype known
			   { 140 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=TRANSLUCENT" >> $pjl_tmp
			)
			;;
		thick )
			(
			echo " userdict /XJXsetmediatype known
			   { 108 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=HEAVY" >> $pjl_tmp
			)
			;;
		tabstock)
			(
			echo " userdict /XJXsetmediatype known
			   { 130 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=TABSTOCK" >> $pjl_tmp
			)
			;;
		* )
			(
			echo " userdict /XJXsetmediatype known { 0 XJXsetmediatype } if " >> $device_tmp
			echo "@PJL SET MEDIATYPE=PAPER" >> $pjl_tmp
			)
			;;
		esac

##########################
#
# Rotate180 (Default=No)
#
##########################

		case "$rotate180" in

		no )
			(
			echo "userdict /EFUserRotate180 known
			   { 0 EFUserRotate180 } if" >> $device_tmp
			)
			;;

		yes )
			(
			echo "userdict /EFUserRotate180 known
			   { 1 EFUserRotate180 } if" >> $device_tmp
			)
			;;

		* )
			(
			echo "userdict /EFUserRotate180 known
			   { 0 EFUserRotate180 } if" >> $device_tmp
			)
			;;

		esac

##########################
#
# Paper Size (Default=Letter)
#
##########################

		case "$paper" in
		letter )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                        /XJXsetPrintSize where { pop (Letter) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l2a" >> $pcl_tmp
			)
			;;
		legal )
			(
			echo "/XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                        /XJXsetPrintSize where { pop (SEFLegal) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l3a" >> $pcl_tmp
			)
			;;
		11x17|tabloid|ledger )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                        /XJXsetPrintSize where { pop (SEFTabloid) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l6a" >> $pcl_tmp
			)
			;;
		executive )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                       /XJXsetPrintSize where { pop (SEFExecutive) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l1a" >> $pcl_tmp
			)
			;;
		A4 )
			(
			echo "/XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                        /XJXsetPrintSize where { pop (A4) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l26a" >> $pcl_tmp
			)
			;;
		B5 )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                        /XJXsetPrintSize where { pop (B5) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l45a" >> $pcl_tmp
			)
			;;
		A3 )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
   			/XJXsetPrintSize where { pop (SEFA3) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l27a" >> $pcl_tmp
			)
			;;
		A5 )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                       /XJXsetPrintSize where { pop (A5) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l25a" >> $pcl_tmp
			)
			;;
		B4 )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
   			/XJXsetPrintSize where { pop (SEFB4) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l46a" >> $pcl_tmp
			)
			;;
		55x85|halfletter|statement )
			(
			echo "/XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
   			/XJXsetPrintSize where { pop (Statement) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l11a" >> $pcl_tmp
			)
			;;
		8x13 )
			(
			echo "/XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
   			/XJXsetPrintSize where { pop (SEFF4) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l12a" >> $pcl_tmp
			)
			;;
		825x13|folio )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
   			/XJXsetPrintSize where { pop (SEFFolio) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l13a" >> $pcl_tmp
			)
			;;
		85x13|foolscap )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
   			/XJXsetPrintSize where { pop (SEFFanFoldGermanLegal) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l14a" >> $pcl_tmp
			)
			;;
		* )
			(
			echo " /XJXsetFullBleedPaper where { pop 1 XJXsetFullBleedPaper } if
                        /XJXsetPrintSize where { pop (Letter) XJXsetPrintSize } if " >> $device_tmp
			printf "\033&l2a" >> $pcl_tmp
			)
			;;
		esac

##########################
# LNX_TUB_E
#
# Print the file
#
##########################

for i in $*
do

##########################
#
# Auto-detect input file type here
#
# If is_PS=1, input file is PostScript
# If is_TXT=1, input file is text
# else, input file is PCL/PJL
# is_PS is overwritten, if user specified
#	input file type with "-o text" or "-o ps" option
#
##########################

if [ -n "$PS" ]
then
	is_PS=1
	is_TXT=0
elif [ -n "$TXT" ]
then
	is_TXT=1
	is_PS=0
elif [ -n "$PCL" ]
then
	is_PS=0;
	is_TXT=0;
else

	OS_NAME=`uname -s`

	case $OS_NAME in
	SunOS)
		is_PS=`file $i | grep -c PostScript`
		;;
	HP-UX)
		is_PS=`file $i | grep  -c postscript`
		;;
	*)
		is_PS=`file $i | grep -c PostScript`
		;;
	esac
		file $i | egrep -i 'ASCII|text|script' > /dev/null
		is_TXT=`expr "$?" = 0`
fi

if [ "$is_PS" -eq 1 ]
then

	##########################
	#
	# Check if the input file has %%EndSetup string in it
	#
	##########################

	is_DSC=`cat $i | grep -c "%EndSetup"`

	##########################
	#
	# Check if is_DSC variable is empty
	#
	##########################

	case $is_DSC in

	1)

		if grep '^setup' $i > /dev/null
		then
			csplit -f $ps_tmp $i "/^setup/"
		else
			csplit -f $ps_tmp $i "/%%EndSetup/"
		fi
		cat $device_tmp >> $ps_tmp"00"
		cat $ps_tmp"00" $ps_tmp"01" > $prtout_tmp

		;;
	*)
		echo "%!PS-Adobe-3.0" > $prtout_tmp
		echo "%%BeginSetup" >> $prtout_tmp
		cat $device_tmp >> $prtout_tmp
		echo "%%EndSetup" >> $prtout_tmp
		cat $i >> $prtout_tmp
		;;
	esac

elif [ "$is_TXT" -eq 1 ]
then

	##########################
	#
	# If text file, apply a filter to remove stair case effect
	#
	##########################

	printf "\033%%-12345X" > $prtout_tmp

	echo "@PJL SET JOB NAME=\"$title\"" >> $prtout_tmp

	cat $pjl_tmp >> $prtout_tmp

	echo "@PJL ENTER LANGUAGE=PCL" >> $prtout_tmp

	printf "\033E" >> $prtout_tmp

	cat $pcl_tmp >> $prtout_tmp; echo >> $prtout_tmp

	cat $i | awk '{print $0, "\r"}' >> $prtout_tmp

	printf "\033%%-12345X" >> $prtout_tmp

else

	##########################
	#
	# If PCL file, send to print directly
	#
	##########################

	cat $i > $prtout_tmp

fi

##########################
#
# Loop for printing
#
##########################

if [ "$nobanner" = "yes" ]
then
	case `uname -s` in
	HP-UX)
	        cat $prtout_tmp | lp -d $REMOTE_PRINTER -o nb
		;;
	*)
	        cat $prtout_tmp | lp -d $REMOTE_PRINTER -o nobanner
		;;
	esac
else
        cat $prtout_tmp | lp -d $REMOTE_PRINTER
fi

done

##########################
#
# Remove temporary files if DEBUG=empty
#
##########################

if test -z "$DEBUG"
then
	if test -f "$device_tmp"
	then
		/usr/bin/rm $device_tmp
	fi

	if test -f "$prtout_tmp"
	then
		/usr/bin/rm $prtout_tmp
	fi

	if test -f $ps_tmp"00"
	then
		/usr/bin/rm $ps_tmp"00"
	fi

	if test -f $ps_tmp"01"
	then
		/usr/bin/rm $ps_tmp"01"
	fi

	if test -f $pjl_tmp
	then
		/usr/bin/rm $pjl_tmp
	fi

	if test -f $pcl_tmp
	then
		/usr/bin/rm $pcl_tmp
	fi

fi

exit_code=0 exit 0
