#! /bin/sh
:	"@(#)/usr/spool/filters/pcl.sh	1.0"
#
#	pcl.sh  -  LPRng filter for PCL printers
#
#	Author:	Marco Greco, <marcog@ctonline.it>, Catania, Italy
#
#	Initial release: Jun 97
#	Current release: Aug 98
#
#	Absolutely no warranty -- use at your own risk
#
#	Notes: processes a number of parameters (port, land, a4,
#	duplex, etc - see below) passed via -Z (lpr) or -o (lp)
#
#	To reiterate one of the points expressed in the LPRng
#	docs, avoid using LPD server of network printers, and
#	use a lp=network_printer%port printcap entry instead.
#	Life's so much easier. Be warned that if you do insist in
#	using a lp/bq entry, multiple copies will not be honoured.
#
#	Derived from: my own SysV pcl printer model, 1990 vintage

# Cure echo idiosyncrasies
if [ `echo '\101'` != A ]
then
    ECHO='echo -e'
else
    ECHO=echo
fi

# cr toggles nl to cr/nl conversion
# Default:
CR=on

# >>>>> Use RAW mode to refrain this filter from setting up defaults for CR, 
# 	PAPER, BIN and ORIEN 

# next few parameters control support selection
# choose one out of
#	letter
#	legal
#	comm	(commercial)
#	a4
#	mon	(envelope, monarch)
#	c10	(envelope, commercial 10)
#	dl	(envelope, international dl)
#	c5	(envelope, international c5)
# Default:
PAPER=26; SKIP=1		#A4

# next few parameters control bin selection
# choose one out of
#	uc	(upper tray)
#	lc	(lower tray)
#	env	(envelope feeder)
# Manual feed and manual envelope feed are NOT supported
# Default:
BIN=1				#Upper tray

# next few parameters control orientation
# choose one out of
#	port	(portrait)
#	land	(landscape)
# Default:
ORIEN=0				#Portrait

# Duplex controls duplex mode
# choose one out of
#	long	(long side bind)
#	short	(short side bind)
# Default:
DUPLEX=				#Simple print

# process parameters
for i in "$@"
do	case $i in
	-Z*)	opts=`expr "$i" : '-Z\(.*\)'`
		for j in $opts
		do	case $j in
				cr|CR) CR=on ;;
				-cr|-CR) CR=off ;;
				uc|UC) BIN=1 ;;
				lc|LC) BIN=4 ;;
				env|ENV) BIN=6 ;;
				legal|LEGAL) PAPER=1 ; SKIP=1 ;;
				letter|LETTER) PAPER=2 ; SKIP=2 ;;
				comm|COMM) PAPER=3 ; SKIP=2 ;;
				A4|a4) PAPER=26 ; SKIP=1 ;;
				mon|MON) PAPER=80 ; SKIP=2 ;;
				c10|C10) PAPER=81 ; SKIP=2 ;;
				dl|DL) PAPER=90 ; SKIP=2 ;;
				c5|C5) PAPER=91 ; SKIP=2 ;;
				long|LONG) DUPLEX=1 ;;
				short|SHORT) DUPLEX=2 ;;
				port|PORT) ORIEN=0 ;;
				land|LAND) ORIEN=1 ;;
				raw|RAW) CR=off ; BIN= ; SKIP= ;
					 PAPER= ; ORIEN= ;;
			esac
		done
		;;
	esac
done

# Init printer, start printing on a new page
$ECHO "\033E\033&l0H\c"

if [ -n "$BIN" ]
then $ECHO "\033&"l"$BIN"H"\c"
fi

if [ -n "$PAPER" ]
then	$ECHO "\033&"l"$PAPER"A"\c"
	if [ -n "$SKIP" ]
	then	$ECHO "\033&"l"$SKIP"E"\c"
	fi
fi

if [ $CR = on ]
then	$ECHO "\033&k2G\c"
fi

if [ -n "$ORIEN" ]
then	$ECHO "\033&"l"$ORIEN"O"\c"
	if [ -n "$SKIP" ]
	then	SKIP=`expr $SKIP + $ORIEN`
		$ECHO "\033&"l"$SKIP"E"\c"
	fi
fi

# Try to enforce 1 copy only - LPRng does the actual multiple copy handling
$ECHO "\033&l1X\c"

if [ -n "$DUPLEX" ]
then	$ECHO "\033&"l"$DUPLEX"S"\c"
fi

echo -e $opts '\r'
# actual file printing
gawk '{print}'

if [ -n "$DUPLEX" ]
then	$ECHO "\033&l0S\c"
fi

if [ $CR = on ]
then	$ECHO "\033&k0G\c"
fi


# Deinit printer
$ECHO "\033E\c"

exit 0

