#! /bin/sh
# apsfilter - Line Printer Input Filter
#
# Copyright (C) 1996 by Florian La Roche	florian@suse.de
#						florian@knorke.saar.de
#
# Copyright by Andreas Klemm <andreas@knobel.gun.de> 1993, 1994, 1995
# co-author: Thomas Bueschgens <sledge@hammer.oche.de>
#
# You are permitted to modify and distribute apsfilter in the
# terms of the GNU Public License (GPL) see below.
#
# You are permitted to use this script in the terms of the 
#
# 		    GNU GENERAL PUBLIC LICENSE
# 		       Version 2, June 1991
# 
#
#  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
#                           675 Mass Ave, Cambridge, MA 02139, USA
#  Everyone is permitted to copy and distribute verbatim copies
#  of this license document, but changing it is not allowed.

# these progs must be present:
# basename, cut, tr, sed, gs, a2ps

# Some things to remember:
# grog -Tlj4,ps,dvi,latin1,ascii file
# groff -T... -t -mandoc file

#
# give email adress of a person to notify in case of printer faults
#
NOTIFY="root"

#
# enable the "set -x" to get debug information in your logfile
#
#set -x

#
# search in path $3:$PATH for program $1 and return path in HAVE_$2
#
findpath()
{
	for path in `echo $3:$PATH | sed -e "s/:/\ /g"` ; do
		test -x $path/$1 && eval "HAVE_$2=$path/$1" && return
	done
	fault_filetype
	exit 1
}

# if apsfilter is called as:
#
#	aps-djet500-a4-{auto,ascii,raw}-{color,mono}
#	$1   $2     $3      $4          $5
#	then we get printer type and papersize from the apsfilter call itself
#	after some trickery with cut and sed
#	METHOD=auto	enables filetype auto recognition
#	METHOD=ascii	force printing method print ascii
#	METHOD=raw	write to print device unfiltered ....
#	COLOR=color	use color-postscript
# 	COLOR=mono	transform color to b&w postscript... so one is
# 			able to save colored pens on a colorprinter
set -- `basename $0 | cut -d\  -f1 - | sed -e 's/-/ /g'`
LABEL=$1; PRINTER=$2; PAPERSIZE=$3; METHOD=$4; COLOR=$5

case $LABEL in
	*aps)	# jup, seems to be that we are installed properly
		;;
	*)	# unknown filter - therefore EXIT APSFILTER
		echo "$1 wrong input filter" >&2
		exit 0 ;;
esac

case $COLOR in
	color)	# set some options so color makes it to the postscript-printer
		PNMTOPS="pnmtops"
		RAS2PS_OPTS="-C"
		DJPEG_OPTS="-colors 265" ;;
	*)	# unset all color-dependent flags
		PNMTOPS="( ppmtopgm | pnmtops )"
		DJPEG_OPTS="-grayscale" ;;
esac


#
# read the global configuration file
#
if [ -f /etc/apsfilterrc ]; then
	. /etc/apsfilterrc
fi


PATH=/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin
export PATH


#
# your favourite mail program that understands the "-s 'Subject'" option
#
MAILX=mailx


#
# options for dvips: depending on PRINTER and PAPERSIZE, some options
# are set automatically
#
# possible settings:
#	-M	don't create fonts automatically using MakeTeXPK
#		since there is a bug in it causing apsfilter to exit
#		after creating the first missing Font.
#	-q	quiet mode
#	-r	print last page first
#	-t papertype (look at /usr/lib/teTeX/texmf/dvips/config.ps)
#	-D num	horizontal and vertical resolution in dpi 
#	-X num	horizontal resolution in dpi 
#	-Y num	vertical resolution in dpi 
#	-Z	compress bitmap fonts (used for >= 400 dpi)
#
case $PRINTER in
	PS_300dpi)		DVIPS_OPTS="-q -D 300 -Z"     ;;
	PS_400dpi)		DVIPS_OPTS="-q -D 400"        ;;
	PS_600dpi)		DVIPS_OPTS="-q -D 600"        ;;
	PS_800dpi)		DVIPS_OPTS="-q -D 600"        ;;
	cdj*|*desk*|djet*)	DVIPS_OPTS="-q -D 300 -r -Z"  ;;
	ljet4l)			DVIPS_OPTS="-q -D 300"        ;;
	ljet4)			DVIPS_OPTS="-q -D 600 -Z"     ;;
	laserjet|ljet[23]*)	DVIPS_OPTS="-q -D 300 -Z"     ;;
	paintjet|pj*)		DVIPS_OPTS="-q -D 300"        ;;
	necp6)			DVIPS_OPTS="-q -D 360"        ;;
	bj200|escp2|st800)	DVIPS_OPTS="-q -D 360 -r"     ;;
	epson)			DVIPS_OPTS="-q -P360x180 -r"  ;;
	bj10e)			DVIPS_OPTS="-q -D 300 -r"     ;;
	*)			DVIPS_OPTS="-q -D 300 -r"     ;;
esac

case $PAPERSIZE in
	letter|legal|ledger)	DVIPS_OPTS="$DVIPS_OPTS -t $PAPERSIZE"	;;
	a3)			DVIPS_OPTS="$DVIPS_OPTS -t A3"		;;
	a4)			DVIPS_OPTS="$DVIPS_OPTS -t A4"		;;
	*)			DVIPS_OPTS="$DVIPS_OPTS -t A4"		;;
esac

#
# how to print ps files
#
case $PRINTER in
	PS_*dpi)
		PRINT_PS="cat -" ;;
	*)
		if [ -n "$GS_RESOL" ]; then
			GS_RESOL="-r$GS_RESOL"
		fi
		PRINT_PS="gs -q -sDEVICE=$PRINTER $GS_RESOL \
			-sPAPERSIZE=$PAPERSIZE -dNOPAUSE -dSAFER \
			-sOutputFile=- -" ;;
esac


#
# print xfig files
#
print_fig()
{
	findpath fig2dev FIG2DEV usr/X11R6/bin
	$DECOMPRESS | $HAVE_FIG2DEV -Lps -P -c | $PRINT_PS 
}


#
# print PBM / PNM pictures
#
print_pnm()
{
	findpath pnmtops PNMTOPS usr/X11R6/bin
	$DECOMPRESS | $PNMTOPS | $PRINT_PS 
}


#
# print TIFF pictures
#
print_tiff()
{
	findpath tifftopnm TIFFTOPNM usr/X11R6/bin
	findpath pnmtops PNMTOPS usr/X11R6/bin
	$DECOMPRESS | $HAVE_TIFFTOPNM | $PNMTOPS | $PRINT_PS 
}


#
# print JPEG pictures
#
print_jpeg()
{
	findpath djpeg DJPEG usr/X11R6/bin
	findpath pnmtops PNMTOPS usr/X11R6/bin
	$DECOMPRESS | $HAVE_DJPEG $DJPEG_OPTS | $PNMTOPS | $PRINT_PS 
}


#
# print GIF pictures
#
print_gif()
{
	findpath giftopnm GIFTOPNM usr/X11R6/bin
	findpath pnmtops PNMTOPS usr/X11R6/bin
	$DECOMPRESS | $HAVE_GIFTOPNM | $PNMTOPS | $PRINT_PS 
}


#
# print sun rasterfile 
#
print_sunraster()
{
	if [ -x /usr/X11R6/bin/ras2ps ]; then
		$DECOMPRESS | ras2ps $RAS2PS_OPTS | $PRINT_PS 
	elif [ -x /usr/X11R6/bin/rasttopnm ];then
		$DECOMPRESS | rasttopnm | $PNMTOPS | $PRINT_PS 
	else
		fault_filetype
	fi
}


#
# print postscript
#
print_ps()
{
	$DECOMPRESS | $PRINT_PS
}


#
# print dvi files
#
# PRINT_DVI = full command to print dvi files...
#
print_dvi()
{
	if [ -z "$PRINT_DVI" ]; then
		findpath dvips DVIPS usr/bin
		TMP2_FILE=${SPOOL_DIR}tmp2_file.$$
		$DECOMPRESS > $TMP2_FILE
		$HAVE_DVIPS -f $DVIPS_OPTS $TMP2_FILE | $PRINT_PS
		rm -f $TMP2_FILE
	else
		$DECOMPRESS | $PRINT_DVI
	fi
}


#
# print raw ascii
#
print_raw()
{
	case $PRINTER in
		*desk*|*djet*|laserjet|ljet*)
			# Translate LF to CR+LF and FF to CR+FF
			printf "\033&k2G"
                        # Perforation Skip Mode off (DIN A4: 66 lines/page)
                        printf "\033&l0L" ;;
	esac

	if [ $PRINT_RAW_SETUP_PRINTER ]; then
		printf $PRINT_RAW_SETUP_PRINTER
	fi

	$DECOMPRESS

	if [ -z "$PRINT_RAW_SUPPRESS_FORMFEED" ]; then
		printf "\014"
	fi
}


#
# print ascii using a2ps
#
# other possibilities:
# enscript -B -p-
# a2x -p -dps -man
# a2ps -p -nh -nn -f12 -b
#
print_ascii()
{
	JOB=`echo "$CONTROL" | egrep '^J' | tail +2c`
	if [ "$JOB" = stdin ]; then JOB=""; fi
	USER=`echo "$CONTROL" | egrep '^P' | tail +2c`
	HOST=`echo "$CONTROL" | egrep '^H' | tail +2c`

	case $PRINTER in
		cdesk*|cdj*|desk*|djet*)	A2PS_MARGIN=1.6 ;;
		*)				A2PS_MARGIN=1.4 ;;
	esac
	
	A2PS_BASIC_OPTIONS="-X$PAPERSIZE -M$A2PS_MARGIN -H$JOB -Q$USER -Z$HOST"
	A2PS_STD_OPTIONS="-8 -m -r -ns -nu -nP"

	case $FEATURE in
		1)	A2PS_FEATURES="-p -1 -F9.0"		;;
		2)	A2PS_FEATURES="-F6.9"			;;
		1n)	A2PS_FEATURES="-p -1 -F9.0 -nH -nL"	;;
		2n)	A2PS_FEATURES="-F6.9 -nH -nL"		;;
		1l)	A2PS_FEATURES="-l -1 -F9.0"		;;
		1ln)	A2PS_FEATURES="-l -1 -nH -nL -F9.0"	;;
		*)	A2PS_FEATURES="-F6.9 -nH"		;;
	esac

	if [ -z "$A2PS_OPTS" ]; then
		A2PS_OPTS="${A2PS_BASIC_OPTIONS} ${A2PS_STD_OPTIONS} \
			   ${A2PS_FEATURES}"
	else
		# user defined, but keep BASIC options
		A2PS_OPTS="${A2PS_BASIC_OPTIONS} ${A2PS_OPTS}"
	fi

	case $PRINTER in
	    djet*|pj*)
		if [ -z "$GS_FEATURES" ]; then
		    # sane defaults for color printer
		    # print ascii texts black&white
		    GS_FEATURES="-dBitsPerPixel=1"
		fi ;;
	esac

	findpath a2ps A2PS usr/bin
	$DECOMPRESS | $HAVE_A2PS ${A2PS_OPTS} 2>/dev/null | $PRINT_PS
}


#
# print data files and fine tune printer settings ...
#
print_data()
{
	$DECOMPRESS
}


#
# UNKNOWN file or method type, too bad ;->
#
fault_filetype()
{
	cat > /dev/console <<EOF
apsfilter: unsupported filetype

$FILE_TYPE

or missing filter !

or perhaps you have to type lpr -Pascii to print an ascii

file containing control characters or lpr -Praw to print 

a file in your printers native language, when printing data 

files (pcl3, pcl5, ...) ?!
EOF

	$MAILX -s "apsfilter: printer fault" $NOTIFY 2> /dev/null <<EOF
apsfilter: unsupported filetype

$FILE_TYPE

or missing filter !

or perhaps you have to type lpr -Pascii to print an ascii

file containing control characters or lpr -Praw to print 

a file in your printers native language, when printing data 

files (pcl3, pcl5, ...) ?!
EOF
}

fault_method()
{
	echo "apsfilter: unknown print method $METHOD" > /dev/console
	echo "apsfilter: unknown print method $METHOD" \
	| $MAILX -s "apsfilter: printer fault" $NOTIFY 2> /dev/null
}

fault_unpacker()
{
	echo "apsfilter: missing $UNPACKER utility to print file" > /dev/console
	echo "apsfilter: missing $UNPACKER utility to print file" \
	| $MAILX -s "apsfilter: printer fault" $NOTIFY 2> /dev/null
}

# We always want to decompress gzipped or otherwise compressed data
# first.
TMP_FILE=${SPOOL_DIR}tmp_file.$$
cat - > $TMP_FILE
set -- `file $TMP_FILE`
shift
FILE_TYPE=$*
case `echo $FILE_TYPE | tr 'A-Z' 'a-z'` in
	*gzip*)		DECOMPRESS="gzip -dc $TMP_FILE";;
	*compress*)	DECOMPRESS="zcat $TMP_FILE";;
	*packed*)	DECOMPRESS="zcat $TMP_FILE";;
	*)		DECOMPRESS="cat $TMP_FILE";;
esac

case $METHOD in
raw)	print_raw ;;
ascii)	print_ascii ;;
auto)	HUGO=`$DECOMPRESS 2>/dev/null | file -`
	set -- `echo $HUGO`
	shift ; shift
	FILE_TYPE=$* 

	case `echo $FILE_TYPE | tr 'A-Z' 'a-z'` in
		*fig*)		print_fig ;;
		*tiff*)		print_tiff ;;
		*gif*)		print_gif ;;
		*jpeg*)		print_jpeg ;;
		*pnm*|*pbm*|*ppm*)
				print_pnm ;;
		*rasterfile*)
				print_sunraster ;;
		postscript*)	print_ps ;;
		*dvi*)		print_dvi ;;
		*data*|*escape*)
				print_data ;;
		*ascii*|*text*|*english*|*script*)
				print_ascii ;;
 		*)
				fault_filetype ;;
	esac ;;

*)	fault_method ;;

esac

rm -f $TMP_FILE

