#! /bin/sh
#
# This is geomfix, Copyright 1992 Silvio Levy
# $Header: /a/tampa/h/tampa_a/fac/levy/texts/geombook/RCS/geomfix,v 1.1 92/07/17 13:27:45 levy Exp $
#
# This shell file reads stdin (or any list of files) and adds a header
# similar to the one added by psfix, but without any of the definitons.
# It also abbreviates certain PostScript commands.

# Defaults:
lmargin=1.0		# default left margin: .5 inches
rmargin=1.0		# default right margin: .5 inches
bmargin=1.0		# default bottom margin: .5 inches
tmargin=1.0		# default top margin: .5 inches
width=8.5		# default page width: 8.5 inches
height=11		# default page height: 11 inches
transform=''		# default transformation: identity
nodistort=true		# default no-stretching: no
fixwid=false		# default fixwid compensation
fixdash=false		# default fixdash compensation
while :; do
	case $# in
	0)
		break 2
		;;
	esac
	case "$1" in
	-lmarg)
		shift
		lmargin=$1
		shift
		;;
	-rmarg)
		shift
		rmargin=$1
		shift
		;;
	-bmarg)
		shift
		bmargin=$1
		shift
		;;
	-tmarg)
		shift
		tmargin=$1
		shift
		;;
	-width)
		shift
		width=$1
		shift
		;;
	-height)
		shift
		height=$1
		shift
		;;
	-land)
		tmp=$width
		width=$height
		height=$tmp
		transform="90 rotate 0 Mheight neg translate"
		shift
		;;
	-stretch)
		nodistort=false
		shift
		;;
	-stringfix)
		fixwid=false
		shift
		;;
	-dashfix)
		fixdash=false
		shift
		;;
	-*)
		echo \
'Usage: psfix [-lmarg inches] [-rmarg inches] [-bmarg inches] [-tmarg inches]
                          [-land] [-stretch] [-width inches] [-height inches] 
                                  [-stringfix] [-dashfix] [file...]' \
			>&2
		exit 1
		;;
	*)
		break 2
		;;
	esac
done

# Output header
echo '%!PS-Adobe-2.0 EPSF-2.0'
# Compute bounding box
(
bc <<!
$width * 72
$height * 72
!
) | (
	read right
	read top
	echo "%%BoundingBox: 0 0 $right $top"
)
cat <<'!'
%%Creator: Mathematica
%%EndComments
!

# Output default values
sed 's/[ 	]*%.*$//
s/^[ 	]*//
/^[ 	]*$/d' <<!

MyDict begin
/Mlmarg		$lmargin 72 mul def	% left margin (points)
/Mrmarg		$rmargin 72 mul def	% right margin (points)
/Mbmarg		$bmargin 72 mul def	% top margin (points)
/Mtmarg		$tmargin 72 mul def	% bottom margin (points)
/Mwidth		$width 72 mul def	% total page width (points)
/Mheight	$height 72 mul def	% total page height
/Mtransform	{ $transform } bind def	% possible page rotation
/Mnodistort	$nodistort def		% no-stretching flag
/Mfixwid	$fixwid	def		% fixedwidth font only flag
/Mfixdash	$fixdash def		% fixeddash flag
/Mrot		0	def		% angle at which text is rendered
!

# Some of the following abbrevs were used by me 
# prior to version 2.1 to save space.
# In version 2.1 different abbrevs were introduced.  
# Only k causes a conflict.

sed '/^%!$/d
s/^0\.\([0-9]\)/.\1/
s/\([ -]\)0\.\([0-9]\)/\1.\2/g
s/\<k\>$/setcmykcolor/
s/\<Mabswid\>$/Ma/g
s/\<arc\>$/a/g
s/\<curveto\>$/c/g
s/\<setdash\>$/d/g
s/\<fill\>$/f/g
s/\<setgray\>$/g/g
s/\<grestore\>$/gr/g
s/\<gsave\>$/gs/g
s/\<stroke\>$/k/g
s/\<lineto\>$/l/g
s/\<setlinewidth\>$/lw/g
s/\<moveto\>$/m/g
s/\<newpath\>$/n/g
s/\<setrgbcolor\>$/r/g
/^%%Creator:/d' $@

echo end