#!/bin/sh

################################################################################
# Only for debugging purposes. You can find debugging output in
# the logfile (see /etc/printcap definition lf=/var/spool/.../log)
################################################################################
#
set -x
export PKFONTS=/usr/TeX/lib/tex/fonts/pk
export TEXPOOL=/usr/TeX/lib/tex
export MFPOOL=/usr/TeX/lib/mf
export TEXINPUTS=/usr/TeX/lib/tex/inputs
export MFBASES=/usr/TeX/lib/mf/bases
export TEXFONTS=/usr/TeX/lib/tex/fonts/tfm
# ______________________________________________________________________________
#
# apsfilter-1.11 ------ TEXT / PS / DVI input filter  -----------
#                       ============================
#
#		for Unix Systems with BSD alike print mechanism (lpd, printcap)
#
#		by Andreas Klemm <andreas@knobel.knirsch.de>
#
# Sun Mar 20 11:46:43 GMT+0100 1994
# ______________________________________________________________________________
#
#
# Features:	- Enables non-Postscript printers to print PS files
# --------	- automatically recognizes filetypes
#		  (currently supported ascii / data / postscript / TeX DVI)
#		  using the Unix command file(1) ( -> /etc/magic)
#		- uses gs and dvips as filter to process PS && DVI files
#		- initializes some printer to fine default values 
#		- if a print fault occurs when using dvips or gs:
#			-> ejects loaded paper automatically
#			-> root will be notified via e-mail (NOTIFY variable)
#			-> the tmp file is left in $TMP_DIR
#		- resets printer after a printjob
#		- resolution and papersize
#		  are selected automatically for gs and dvips.
#		- allows printing of ascii text with ``german umlauts''
#		  and "raw" Postscript files without calling gs
#		  if apsfilter is called under the name "aps-forceascii" 
#		- uses lpr's command line arguments -wwidth -iindent -llength
#		- uses pr for printing ascii texts (as an option you
#		  can disable the ps header)
#
# Installation:
# ------------	- HP Deskjet Family and Canon BJ   -> plug and play ;-)
#		- other printer supported by gs	   -> configure and play ;->
#
#		Normally **only two** steps are necessary
#		=========================================
#
#		- modify the PRINTER shell variable in this script
#		  (see my comments which types/names are supported)
#
#		- modify /etc/printcap: 
#			set ``if'' (path to input filter) variable
#			:if=_Complete_Pathname_to_apsfilter_:
#			(see example in this file)
#
#_______________________________________________________________________________
#
# Andreas Klemm                 /\/\____ Wiechers & Partner Datentechnik GmbH 
# andreas@knobel.knirsch.de ___/\/\/     andreas@wupmon.wup.de (Unix Support)
#_______________________________________________________________________________
#

################################################################################
#
# Changes 1.10 -> 1.11
#
################################################################################
#
# A last minute hack caused file type autorecognition to fail :-(
# The culprit: I only appended $PATH to the search path for machine 
# specific search paths. And boom :-(
# After removing $PATH from the PATH= line everything works perfectly now.
# Oh folks .. the next time I don't include wishes from people without 
# testing, even if the changes are soooo minimal as in this case '-)
#
# No line in this script exceeds 80 columns any more,
# as suggested by Stephen Hsieh <sch2@crux5.cit.cornell.edu>
#
# Modified the way how apsfilters command line arguments are stored
# in environment variables, 
# as suggested by Dirk Sturzebecher <dirk@flop.escape.de>
#

################################################################################
#
# Changes 1.9 -> 1.10
#
################################################################################
#
# introduced shell functions print_ps, print_dvi, print_ascii, ....
# is needed for the new program functions to force printing of ascii, which
# is needed for printing "deutsche Umlaute"
#
# problems with "deutschen Umlauten"
# Text containing "Deutsche Umlaute" may be interpreted as data, so cr/nl 
# conversion for deskjet and possibly other printer doesn't work.
# A possible solution is to force the script to handle imput data as text or
# something else.
#
# exit 1 on print fault caused lpd to print the file again and again
# and lots of tmp files made TMP_DIR full.. so we have to exit 0 in any case
#
# Introduced MAILX variable. /bin/mail = elm on Linux. Elm didn't find
# ~/Mail directory, so it failed to send $NOTIFY a mail when a print error
# occurred.
#
# David Frey <dfrey@stud.ee.ethz.ch>:
# use lpr's command line arguments -wwidth -iindent -llength, pipe ascii
# through pr
#
# and some more ...
#

################################################################################
#
# Changes 1.8 -> 1.9
#
################################################################################
#
# Thu Feb 24 18:45:00 GMT+0100 1994
#
# removed compression option that was introduced with 1.8
# since dvips can't read from a pipe...
#
# canon bj printer don't have a common command set ...
# So the bj* commands I introduced were only valid for the bj200.
#

################################################################################
#
# Changes 1.7 -> 1.8
#
################################################################################
#
# Sun Feb 20 14:02:29 GMT+0100 1994
#
# Optionally (default=yes) compresses temporary files used by this script
# See COMPRESS option. On slow systems wit low memory you may want to disable
# this feature. (Suggestions from Bill - physics.purdue.edu!bcr)
#
# Rewrote parts of my comments and shortened the script by simly referencing
# the GNU GPL.
#


################################################################################
#
# Changes 1.6 -> 1.7
#
################################################################################
#
# Wed Feb 16 21:57:57 GMT+0100 1994
#
#	- Support for Canon Bubblejets (Bill - physics.purdue.edu!bcr)
#	- fixed nec resolution from 300 to 360 dpi in dvips part
#


################################################################################
#
# Changes 1.5 -> 1.6
#
################################################################################
#
# Sat Feb  5 13:10:33 GMT+0100 1994 (AKL)
#
#	rearranged this script to be an input filter
#	use if=/usr/local/bin/apsfilter in /etc/printcap
#	advantage: you can mix the file types in on command line
#			lpr postscript_file ascii_file ....
#		
# ------------------------------------------------------------------------------
#
# Sat Feb  5 13:29:24 GMT+0100 1994 (AKL)
#		
#	removed quit.ps, because it caused trouble for some people.
#
# ------------------------------------------------------------------------------
#
# Sat Feb  5 13:39:18 GMT+0100 1994 (AKL)
#
#	dvips: added useful command line options (see: section 10)
#
#	- run dvips in quiet mode
#	- print last page first for some printers
#	- compress bitmap fonts when resolution is greater or equal 400 dpi
#	- trying to set printer resolution for dvips && gs correctly
#
#	suggestion from: sar@beehive.mn.org (Steven A. Reisman)
#	set paper type for dvips && gs correctly
#
# ------------------------------------------------------------------------------
#
# Sat Feb  5 16:17:31 GMT+0100 1994 (AKL)
#
#	from: sch2@crux5.cit.cornell.edu (Stephen Hsieh)
#	      - cr/nl conversion needed for laserjets when printing ascii
#
################################################################################

################################################################################
#
# *** INSTALLATION ****
#
#		a) modify your file: /etc/printcap
#
#		b) and install apsfilter with proper permissions
#
# 		c) modify the PRINTER environment variable in this script
#		   config section (2)
#
#		d) optional: change other parameters in config section (3) ff.
#
################################################################################
#
# Example:	HP Deskjet connected to a PC's parallel Centronix interface
#		(Linux Slackware 1.1.1)
#
# lp|deskjet|HP Deskjet 500:\
#	:lp=/dev/lp1:\				<== printer device
#	:sd=/var/spool/lp1:\			<== spool directory
#	:lf=/usr/spool/lp1/log:\		<== logfile
#	:af=/usr/spool/lp1/acct:\		<== accounting file
#	:if=/usr/local/bin/apsfilter:\		<== full path to apsfilter
#	:mx#0:\					<== no size restriction
#	:sh:					<== suppress banner page !
#
#
# And a 2nd entry to have the possibility to force the script to treat binary
# data to be printed as ascii text (since german texts with umlauts are often
# recognized as data files...)
#
# lp|deskjet|HP Deskjet 500:\
#	:lp=/dev/lp1:\				<== printer device
#	:sd=/var/spool/lp1:\			<== spool directory
#	:lf=/usr/spool/lp1/log:\		<== logfile
#	:af=/usr/spool/lp1/acct:\		<== accounting file
#	:if=/usr/local/bin/aps-forceascii:\	<== ascii_only filter
#						    symlink to apsfilter
#	:mx#0:\					<== no size restrictions
#	:sh:					<== suppress banner page !
#
# 	copy apsfilter to /usr/local/bin/apsfilter and make it executable
#	and make a symbolic link to aps-forceascii
#
#		mkdir /usr/local/bin || mkdir -p /usr/local/bin
#		cp apsfilter /usr/local/bin
#		ln -s /usr/local/bin/apsfilter /usr/local/bin/aps-forceascii
#		chmod 555 /usr/local/bin/apsfilter
#		chown lp /usr/local/bin/apsfilter
#		chgrp lp /usr/local/bin/apsfilter
#
# double check the permissions of spool directory....
#
# I) For the printer itself (sd=/var/spool/lp1):
#
#	drwxrwx---   2 root     lp           1024 Feb  5 16:53 /var/spool/lp1
#
#		shell commands to do this (as root):
#			mkdir /var/spool/lp1
#			chown root.lp /var/spool/lp1
#			chmod 770 /var/spool/lp1
#
# II) For the line printer daemon lpd:
#
#     Permissions of lpd (on Linux Slackware 1.1.1):
#
#		-r-x------   1 root  lp    39696 Aug 12 17:44 /usr/sbin/lpd
#
#     Needed Permissions for lpd spool dir:
#
#		drwxrwx---   2 root  lp     1024 Dec 31 15:59 /var/spool/lpd
#
#		shell commands to do this (as root):
#			mkdir /var/spool/lpd
#			chown root.lp /var/spool/lpd
#			chmod 770 /var/spool/lpd
#
# III) Check your /etc/magic, see file(1) magic(5)
#      maybe you have to add some lines for Postscript and dvi recognition
#
#      # magic.postscript: Magic for postscript files
#      0	string		%!		PostScript document
#      >2	string	PS-Adobe-		conforming
#      >11	string	1.0			at level %s
#      >11	string	2.0			at level %s
#      # magic.tex: 
#      0	short	0173402	DVI File
#      >16	string	>\0	(%s)
#      0	short	0173531	Packed TeX Font
#      >4	string	>\0	(%s)
#      2	string	\000\022	Metafont Font Metric
#      >34	string	>\0	(%s)
#

################################################################################
#
# D e b u g g i n g
# - - - - - - - - -
#
# Activate the debug switch at the beginning of the script (set -x).
# Then you can find debugging output in the printers logfile.
# Don't forget to comment out the debug switch later otherwise
# your logfile will grow very quickly ;-)
#
################################################################################

################################################################################
#
# Supported printers by Ghostscript - important for printing ps files
#
#	[ pick one name and modify the PRINTER variable below ]
#
################################################################################
#
# Version dependend -- Ghostscript 2.6.1 PL 4 supports:
#
# *	appledmp  Apple Dot Matrix Printer (should also work with Imagewriter)
#	bj10e	  Canon BubbleJet BJ10e
# *	bj200	  Canon BubbleJet BJ200
# *	cdeskjet  H-P DeskJet 500C with 1 bit/pixel color
# *	cdjcolor  H-P DeskJet 500C with 24 bit/pixel color and
#		      high-quality color (Floyd-Steinberg) dithering
# *	cdjmono   H-P DeskJet 500C printing black only
# *	cdj500	  H-P DeskJet 500C (same as cdjcolor)
# *	cdj550	  H-P DeskJet 550C
# *	declj250  alternate DEC LJ250 driver
# +	deskjet   H-P DeskJet and DeskJet Plus
# *	dfaxhigh  DigiBoard, Inc.'s DigiFAX software format (high resolution)
# *	dfaxlow   DigiFAX low (normal) resolution
#	djet500   H-P DeskJet 500
# *	djet500c  H-P DeskJet 500C
#	epson	  Epson-compatible dot matrix printers (9- or 24-pin)
# +	eps9high  Epson-compatible 9-pin, interleaved lines
#		       (triple resolution)
# *	epsonc	  Epson LQ-2550 and Fujitsu 3400/2400/1200 color printers
# *	escp2	  Epson ESC/P 2 language printers, including Stylus 800
# +     ibmpro    IBM 9-pin Proprinter
# *	jetp3852  IBM Jetprinter ink-jet color printer (Model #3852)
# +	laserjet  H-P LaserJet
# *	la50	  DEC LA50 printer
# *	la75	  DEC LA75 printer
# *	lbp8	  Canon LBP-8II laser printer
# *	ln03	  DEC LN03 printer
# *	lj250	  DEC LJ250 Companion color printer
# +	ljet2p	  H-P LaserJet IId/IIp/III* with TIFF compression
# +	ljet3	  H-P LaserJet III* with Delta Row compression
# +	ljet4	  H-P LaserJet 4 (defaults to 600 dpi)
# +	ljetplus  H-P LaserJet Plus
# *	m8510	  C.Itoh M8510 printer
# *	necp6	  NEC P6/P6+/P60 printers at 360 x 360 DPI resolution
# *	nwp533    Sony Microsystems NWP533 laser printer   [Sony only]
# *	oki182	  Okidata MicroLine 182
#	paintjet  H-P PaintJet color printer
# *	pj	  alternate PaintJet XL driver 
# *	pjxl	  H-P PaintJet XL color printer
# *	pjxl300   H-P PaintJet XL300 color printer
# *	r4081	  Ricoh 4081 laser printer
# *	sparc	  SPARCprinter
# *	t4693d2   Tektronix 4693d color printer, 2 bits per R/G/B component
# *	t4693d4   Tektronix 4693d color printer, 4 bits per R/G/B component
# *	t4693d8   Tektronix 4693d color printer, 8 bits per R/G/B component
# *	tek4696   Tektronix 4695/4696 inkjet plotter
#

################################################################################
#
# ENVIRONMENT SECTION - Global setting
#
################################################################################
#
# Reference of Shell Variables:
# -----------------------------
#
#	(1)  PATH		<- Command search path
#				   should fit everybodies needs
# 	(2)  PRINTER		<- Type of Printer
#				   - set it manually or by install script
#				   - is *BASE* for other environment variables
#	(3)  TMP_DIR		<- directory for temporary files
#				   (/var/tmp should be ok for everybody)
#	(4)  NOTIFY		<- who gets mail when printer fault occurrs
#				   root is a sane default
#	(5)  MAILX		<- Your mailx program (can -s "Subject" option)
# 	(6)  GS_RESOL		<- printer resolution, used for gs(1)
#				   computed automatically for some printers
#				   depends on PRINTER variable
#	(7)  PAPERSIZE		<- correct papersize for gs
#				   computed automatically for some printers
#				   depends on PRINTER variable
#	(8)  DVIPS_OPTS		<- options for dvips
#				   some options are defaults some are computed
#				   automatically. Depends on the following
#				   variables:
#					PRINTER, GS_RESOL, PAPERSIZE
#	(9)  WANT_PS_HEADER	<- Do you want a pr(1) header in ascii output ?
#	(10) ECHO		<- the echo command, that does control character
#				   expansion
#
################################################################################

#-------------------------------------------------------------------------------
#
# (1)  PATH - SET SEARCH PATH
#	
#-------------------------------------------------------------------------------

# PATH=......:$PATH
# Note !       ^------- This goes wrong, don't know why, perhaps a Linux
#			release specific problem
#
PATH=/usr/bin:/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/TeX/bin:/local/bin:/usr/bin/X11:/usr/ghostscript/bin

#-------------------------------------------------------------------------------
#
# (2)  PRINTER - SET PRINTER TYPE
#	
#-------------------------------------------------------------------------------

PRINTER=laserjet

#-------------------------------------------------------------------------------
#
# (3)  TMP_DIR - SET LOCATION FOR TEMPORARY FILES
#
#-------------------------------------------------------------------------------

case `uname -s` in
	Linux|linux)		TMP_DIR=/var/tmp	;;
	Solaris|solaris)	TMP_DIR=/var/tmp	;;
	Sun*|sun*)		TMP_DIR=/var/tmp	;;
	*)			TMP_DIR=/tmp		;;
esac

#-------------------------------------------------------------------------------
#
# (4)  NOTIFY - SET E-MAIL ADRESS OF PRINTER ADMINISTRATOR
# 
#-------------------------------------------------------------------------------

NOTIFY=root

#-------------------------------------------------------------------------------
#
# (5)  MAILX - your favourite mail program that understands "-s" (except elm !)
#      
#	typical Linux problems: /bin/mail is often a symlink to elm which
#	doesn't work since it misses the Mail folder directory, etc ....
#	Use the mailx program instead. Another advantage of mailx is, that
#	you can give a Subject with -s on the command line.
#	SunOS 4.1.X, Solaris 1 note: I think there is a program "Mail" which does
#	the job.
#
#-------------------------------------------------------------------------------

# note: hope it's correct ... otherwise 1.11, puh ;-)

case `uname -s` in
	Linux|linux)		MAILX=mailx	;;
	Solaris|solaris)	MAILX=Mail	;;
	Sun*|sun*)		MAILX=Mail	;;
	*)			MAILX=mailx	;;
esac


#-------------------------------------------------------------------------------
#
# (6) PRINTER RESOLUTION - GS_RESOL
#
#		if you have other Printers than HP's with 300x300 pixel or 
#		nec's with 360x360 then you have to add something reasonable 
#		for your printer !!!
#
#-------------------------------------------------------------------------------

case $PRINTER in
	cdesk*|cdj*|desk*|djet*)
				GS_RESOL=300x300
				;;
	laserjet|ljet*)
				GS_RESOL=300x300
				;;
	paintjet|pj*)
				GS_RESOL=300x300
				;;
	necp6|bj200)
				GS_RESOL=360x360
				;;
	*)
				#
				# when printer is not known exactly
				# we assume a default of 300x300
				#
				GS_RESOL=300x300
				;;
esac

#-------------------------------------------------------------------------------
#
# (7) PAPERSIZE - FOR GS (GHOSTSCRIPT)
#
#	Letter note legal
#	a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
#	b0 b1 b2 b3 b4 b5
#	archE archD archC archB archA
#	flsa flse
#	halfletter
#	11x17
#	ledger
#
#-------------------------------------------------------------------------------

PAPERSIZE=a4


#-------------------------------------------------------------------------------
#
# (8) OPTIONS for dvips - DVIPS_OPTS
#
#		-q quiet mode
#		-r print last page first
#		-t papertype
#			(look known formats in the config file config.ps
#			 on Linux Slackware usually in /usr/TeX/lib/tex/ps)
#		-D num horizontal + vertical resolution in dpi 
#		   or:	-X num horizontal resolution in dpi 
#			-Y num vertical   resolution in dpi 
#		-Z compress bitmap fonts
#		   usually only when resolution is >= 400
#
#-------------------------------------------------------------------------------

case $PRINTER in
	cdesk*|cdj*|desk*|djet*)
				DVIPS_OPTS="-q -D 300 -r -Z"
				;;
	laserjet|ljet*)
				DVIPS_OPTS="-q -D 300"
				;;
	paintjet|pj*)
				DVIPS_OPTS="-q -D 300"
				;;
	necp6|bj200)
				DVIPS_OPTS="-q -D 360 -r"
				;;
	*)
				#
				# when printer is not known exactly
				# we assume a default of 300x300
				#
				DVIPS_OPTS="-q -D 300 -r"
				;;
esac

case $GS_RESOL in
	360x360|400x400|600x600)		
				#
				# compress bitmap fonts if you have a 
				# high resolution printer
				#
				DVIPS_OPTS="$DVIPS_OPTS -Z"
				;;
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 letter"
				;;
esac


#-------------------------------------------------------------------------------
#
# (9) WANT_PR_HEADER - do you want a pr header when printing ascii ?
#
#-------------------------------------------------------------------------------

#WANT_PR_HEADER=Yes
WANT_PR_HEADER=No

#-------------------------------------------------------------------------------
#
# (10) Your echo command that handels control characters in strings
#
#-------------------------------------------------------------------------------

# Linux - bash
ECHO="printf "

# Rest of the world...?
#ECHO=echo


################################################################################
################################################################################
################################################################################
####
#### C O P Y R I G H T 
####
################################################################################
################################################################################
################################################################################
#
#
# 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.
# 


################################################################################
################################################################################
################################################################################
####
#### POSSIBLE MODIFICATIONS / additional contributors
####
####		suggestions from other creative people
####		which use another configuration than I
####
################################################################################
################################################################################
################################################################################
#
# DO YOU WANT HEADER PAGES ?!?!
#
#			Jeff Stern <jstern@eclectic.ss.uci.edu>
#			Mon Nov  1 21:21:45 GMT 1993
#
# "... I changed to using it as an input filter instead of an output 
#  filter. This way I can still print header pages without confusing 
#  the filter. This necessarily required adding a command to tell 
#  printcap to send a formfeed at the beginning of opening the device 
#  and also specifying that that formfeed really also includes the 
#  LF-> CR+LF translation: this is so that the translation also will 
#  work for the header page, too."
#
#  Necessary printcap entry instead of the default for HP Deskjet's:
#
#	lp:lp=/dev/lp1:sd=/var/spool/lp1:if=/usr/local/bin/apsfilter:mx#0:\
#       :lf=/usr/adm/lpd-errs:ff=\033E\033&k2G\033&a10L:fo
#


################################################################################
################################################################################
################################################################################
#####
##### S h e l l  -  F u n c t i o n s
#####
################################################################################
################################################################################
################################################################################


################################################################################
#
# eject() - EJECT PAGE COMMAND
#
#			- if an error condition occurrs when gs is working
#			  then the possibly partial printed page isn't 
#			  ejected automatically. So we have to do it !
#
################################################################################

eject() {

	case $PRINTER in

		cdesk*|cdj*|desk*|djet*)
					# send pcl command for page eject
					$ECHO "\033&l0H\c"
					;;
		laserjet|ljet*)
					# send pcl command for page eject
					# hope that's correct for them, too ;->
					$ECHO "\033&l0H\110"
					;;
		paintjet|pj*)
					# send pcl command for page eject
					# hope that's correct for them, too ;->
					$ECHO "\033&l0H\c"
					;;
                bj200)
                                        # page eject
                                        $ECHO "\0334\c"
                                        ;;
	esac

	#------------------------------------------------------------
	# If an error occurrs, then preserve tmp file
	#------------------------------------------------------------

	Preserve=1
}


################################################################################
#
# FUNCTION reset() - RESET PRINTER
#
#		(shell function - printer dependend)
#
################################################################################

reset() {

	case $PRINTER in

		cdesk*|cdj*|desk*|djet*)
					# send pcl command for Printer Reset
					$ECHO "\033E\c"
					;;
		laserjet|ljet*)
					# send pcl command for Printer Reset
					# should be correct for them, too ;->
					$ECHO "\033E"
					;;
		paintjet|pj*)
					# send pcl command for Printer Reset
					# should be correct for them, too ;->
					$ECHO "\033E\c"
					;;
                bj200)
                                        # send pcl command for Printer Reset
                                        $ECHO "\033E\c"
                                        ;;
	esac
}


################################################################################
#####################  N E W   W I T H   1.10  #################################
################################################################################
####
####           The shell functions for printing ps, dvi, ....
####
################################################################################
################################################################################
################################################################################


#===============================================================================
#
# pipe ps data through ghostscript
#
#===============================================================================

print_ps()
{
	cat $TMP_FILE			\
	| gs				\
		-q			\
		-sDEVICE=${PRINTER}	\
		-r${GS_RESOL}		\
		-sPAPERSIZE=${PAPERSIZE}\
		-dNOPAUSE		\
		-dSAFER			\
		-sOutputFile=-		\
		-			\
	|| eject
	#    ^  if an error occurs, then
	#	eject already loaded paper
	#	calls printer dependend function
}


#===============================================================================
#
# print dvi data using dvips->gs
#
#===============================================================================

print_dvi()
{
	dvips -f $DVIPS_OPTS		\
	< $TMP_FILE			\
	| gs				\
		-q			\
		-sDEVICE=${PRINTER}	\
		-r${GS_RESOL}		\
		-sPAPERSIZE=${PAPERSIZE}\
		-dNOPAUSE		\
		-dSAFER			\
		-sOutputFile=-		\
		-			\
	|| eject
	#    ^  if an error occurs, then
	#	eject already loaded paper
	#	calls printer dependend function

}	# EOF print_dvi()


#===============================================================================
#
# print ascii and fine tune printer settings ...
#
#===============================================================================

print_ascii()
{

	#------------------------------------------------------------
	# print ascii - BEGIN of Printer Dependend Part
	#------------------------------------------------------------

	case $PRINTER in 

		cdesk*|cdj*|desk*|djet*)

			#------------------------------------
			# Something special for Deskjets ...
			#------------------------------------
			#
			# print normal text with
			#	- cr/nl conversion
			#	- letter quality
			#	- 66 lines/page
			#	- left margin
			#	- 12 cpi pitch
		
			# LINE TERMINATION	\E & k # G
			#	0 = CR=CR   ; LF=LF   ; FF=FF (default)
			#	1 = CR=CR+LF; LF=LF   ; FF=FF
			# **	2 = CR=CR   ; LF=CR+LF; FF=CR+FF
			#	3 = CR=CR+LF; LF=CR+LF; FF=CR+FF
			$ECHO "\033&k2G"

			# QUALITY		\E ( s # Q
			#   	1 = draft	= 240cps
			# **	2 = letter	= 120cps (default)
			$ECHO "\033(s1Q"

			# Text Scale Mode		\E & k # W
			#   	5 = off (default)
			#     	6 = on
			#	ignored in landscape mode
			$ECHO "\033&k5W"

			# Perforation Skip Mode		\E & l # L
			#   	0 = off = 0.0 in Top Margin
			#   	1 = on  = 1/2 in Top Margin (default)
			$ECHO "\033&l0L"

			## pr does the job now !
			## Left Margin			\E & a # L
			## 	# Value = column number
			##	default = 0 = 1/8 in left margin
			##	used = 7 for 1" left margin
			## $ECHO "\033&a7L"

			# Pitch				\E ( s # H
			#	# = cpi
			#	default = 10 characters per inch
			# **	12 cpi
			$ECHO "\033(s12H"
			;;

		laserjet|ljet*)
			#------------------------------------
			# Something special for Laserjets ...
			#------------------------------------
			#
			# print normal text with
			#	- cr/nl conversion

			# LINE TERMINATION	\E & k # G
			#	0 = CR=CR   ; LF=LF   ; FF=FF (default)
			#	1 = CR=CR+LF; LF=LF   ; FF=FF
			# **	2 = CR=CR   ; LF=CR+LF; FF=CR+FF
			#	3 = CR=CR+LF; LF=CR+LF; FF=CR+FF
			$ECHO "\033&k2G"
			;;

		bj200)
			# LINE TERMINATION      \E #
			#       0 = CR=CR   ; LF=LF
			# **    1 = CR=CR   ; LF=CR+LF
			$ECHO "\033\000\000"

			# QUALITY               \E %
			#       G = double strike
			# **    H = single strike
			$ECHO "\033H\000"

			# Set proportional printing     \E #
			# **    0 = off (default)
			#       1 = on
			$ECHO "\033\000\000"

			# Perferation Skip Mode         \E % (#)
			#       N (#) =  # lines
			#       O = off
			$ECHO "\033O\000"

			## pr does the job now !!!
			## Horizontal Margin             \E X # #
			##       # # == left and right (units?)
			## $ECHO "\033X\010\127\000"

			# Pitch                         \E :
			# **    12 cpi                  \E :
			#       default = 10 characters per inch
			$ECHO "\033:\000"
			;;

		*)	#
			# Any other printer: do nothing, sorry ;-)
			# Please give me a note what to include
			# here for your special printer folks ...
			#
			;;
	esac
	#EOF Printer Dependend Part

	#
	# And now print ascii tmp file
	#

	if [ "$WANT_PR_HEADER" = "Yes" ]
	then
		pr -r -w $WIDTH -l $LENGTH -o $INDENT -h $LOGINNAME@$HOST \
#		pr -r -f -w $WIDTH -l $LENGTH -o $INDENT -h $LOGINNAME@$HOST \
		$TMP_FILE
	else
		pr -t -r -w $WIDTH -l $LENGTH -o $INDENT $TMP_FILE
	fi
	

}	#EOF print_ascii()


#===============================================================================
#
# print data files and fine tune printer settings ...
#
#===============================================================================

print_data()
{

	#------------------------------------------------------------
	# print DATA - BEGIN of Printer Dependend Part
	#------------------------------------------------------------

	case $PRINTER in 

		cdesk*|cdj*|desk*|djet*|laserjet|ljet*)

			# LINE TERMINATION	\E & k # G
			# **	0 = CR=CR   ; LF=LF   ; FF=FF (default)
			#	1 = CR=CR+LF; LF=LF   ; FF=FF
			#   	2 = CR=CR   ; LF=CR+LF; FF=CR+FF
			#	3 = CR=CR+LF; LF=CR+LF; FF=CR+FF
			$ECHO "\033&k0G"
			;;

		bj200)
     			# LINE TERMINATION      \E #
			#       0 = CR=CR   ; LF=LF
			# **    1 = CR=CR   ; LF=CR+LF
			$ECHO "\0331\000\c"
			;;

		*)
			;;
	esac
	#------------------------------------------------------------
	# print DATA - END of Printer Dependend Part
	#------------------------------------------------------------

	#------------------------------------------------------------
	# And now print DATA tmp file
	#------------------------------------------------------------

	cat $TMP_FILE

}	#EOF print_data()


#===============================================================================
#
# UNKNOWN file type, too bad ;->
#
#===============================================================================

fault_filetype()
{
	# make noise on system console
	echo "apsfilter: unknown filetype $FILE_TYPE" > /dev/console

	# mail to printer admin
	echo "apsfilter: $TMP_FILE: unknown filetype $FILE_TYPE" \
	| $MAILX -s "apsfilter: printer fault" $NOTIFY \
	2> /dev/null

	# don't remove print file
	Preserve=Yes

}	#EOF fault_filetype()
	

#  / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
# / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
#  / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
#
#   M A I N  ( )
#
#  / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
# / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
#  / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /


#\
# >	Temp. filename
#/

TMP_FILE=$TMP_DIR/apsfilter.$$

#\
# >	write print data from stdin to temp file
#/

cat - > $TMP_FILE

#\
# >	reset printer (shell function - printer dependend)
#/

reset

#\	evaluate command line arguments that are given to the
# \	input filter 
#  \  
#   >	apsfilter [-c] -wwidth -llength -iindent -n login -h host acct-file     
#  /			  $1      $2       $3    $4   $5  $6  $7     $8
# /	defaults: width=0, length=66, indent=0
#/

WIDTH=`echo $1 | awk '{ print substr($1,3) }'`
if [ ! $WIDTH ]; then WIDTH=80; fi
LENGTH=`echo $2 | awk '{ print substr($1,3) }'`
if [ ! $LENGTH ]; then LENGTH=66; fi
INDENT=`echo $3 | awk '{ print 1+substr($1,3) }'`
if [ ! $INDENT ]; then INDENT=0; fi
LOGINNAME=$5
HOST=$7


#\
# \	if apsfilter is called/used under the name "aps-forceascii", then
#  >	the script skips "filetype autorecognition" 
# /	necessary to print files with "deutschen Umlauten" ;-)
#/

BASENAME=`basename $0`

case $BASENAME in
    *ascii*) 
		print_ascii ;;

    *)

	#\
	# \	**** FILE TYPE AUTO_RECOGNITION *****
	#  \	
	#   \	first set output of the ``file'' command to new script arguments
	#    >	$1, $2, ..., $n
	#   /
	#  /	then throw away $1 since that is the *file name*
	# /	the rest is a "string" which consist of one or multiple words 
	#/	describing the file type. For example: "Korn Shell Script"

	set -- `file $TMP_FILE`
	shift ; FILE_TYPE=$*

	case $FILE_TYPE in
		PostScript*)			print_ps	;;
		*DVI*)				print_dvi	;;
		*ascii*|*text*|*script*)	print_ascii	;;
		*data*)				print_data	;;
		*)				fault_filetype	;;
	esac
esac

#\
# \	Preserve tmp File in $TMP_DIR for later examination
#  >	if error conndition occurred.
# /	Perhaps you have to modify /etc/magic ... who knows ...
#/

if [ "$Preserve" = "Yes" ]
then 
	# don't remove tmp file
	exit 0
else
	# reset printer (shell function - printer dependend)
	rm -f $TMP_FILE
	reset
	exit 0
fi
