#!/bin/csh -f
#	macfilter
#  Filter for Macintosh-generated PostScript code.
#  By Ron Hitchens & Brian Powell
#	hitchens@sally.utexas.edu	brian@sally.utexas.edu
#	hitchens@ut-sally.UUCP		brian@ut-sally.UUCP
#
#  Modification History
#	BHP	March, 1987	Extracted from psfilter.csh and updated to
#				support the 3.1 and 3.3 LW drivers.
#	BHP	May, 1987	Updated to support the 4.0 LW driver.
#
#   This filter prepends the necessary LaserPrep file to the Mac file, then
# sends it through a filter to escape 8-bit characters (otherwise lost by the
# UNIX printer driver.)  This version supports the Macintosh LaserWriter driver
# versions 1.1 (LaserPrep version 12), 3.1 (LaserPrep version 40), 3.3
# (LaserPrep version 49), and 4.0 (LaserPrep version 65).
#   The difference between the four versions is deduced by looking at the
# first line of the input to this script.  The first line from the 1.1 driver
# consists entirely of "md begin".  The first line from the 3.1 driver consists
# of "%!PS-Adobe-1.0".  The first line from the 3.3 driver consists of
# "%!PS-Adobe-1.2".  The first line from the 4.0 driver consists of
# "%!PS-Adobe-2.0".  This script fgreps for those strings in the first line of
# the input file and prepends the corresponding LaserPrep version.
#   NOTE:  Other versions of the LaserWriter driver (most notably version 3.0)
# produce output that is not easily distinguishable from output from Laser-
# Writer driver 3.1.  For this reason, this filter cannot provide warnings
# about incorrect input.  In general, the different versions are incompatible,
# and correct output from anything other than PostScript from the Macintosh
# LaserWriter drivers 1.1, 3.1, 3.3, and 4.0 cannot be expected.


set prepdir=/usr/local/lib/allfonts/postscript	# dir where the prep files live

cat > /tmp/mac$$			# save stdin so we can look at it

# search the first line of stdin for "Adobe-1.0", "Adobe-1.2" and "Adobe-2.0".
# The variable stat1 is true if "Adobe-1.0" isn't found, the variable stat2
# is true if "Adobe-1.2" isn't found, and stat3 is true if "Adobe-2.0" isn't
# found.

head -1 /tmp/mac$$ | fgrep -s Adobe-1.0 >& /dev/null
set stat1=$status
head -1 /tmp/mac$$ | fgrep -s Adobe-1.2 >& /dev/null
set stat2=$status
head -1 /tmp/mac$$ | fgrep -s Adobe-2.0 >& /dev/null
set stat3=$status

if ( ! $stat1 ) then
	set prep=laser-prep-40.pro	# found "Adobe-1.0"; use version 40
else if ( ! $stat2 ) then
	set prep=laser-prep-49.pro	# found "Adobe-1.2"; use version 49
else if ( ! $stat3 ) then
	set prep=laser-prep-65.pro	# found "Adobe-2.0"; use version 65
else
	set prep=laser-prep-12.pro	# not found; assume version 12
endif

# Concatenate the prep and the Mac job.  The combined Postscript is then
# piped thru a filter to escape any chars with the high bit set.  (The more
# recent drivers are better about not generating those sorts of characters,
# but we might as well go ahead and do it.)  The final result goes down our
# stdout which is usually being piped into the printer driver (psif or pscomm)

cat $prepdir/$prep /tmp/mac$$ | /usr/local/lib/ps8

set result=$status		# save the result for our exit code

rm -f /tmp/mac$$		# make sure rm runs silently

exit $result			# that's all
