#! /bin/sh
#
#                        Mathematica source file
#
#        Copyright 1986 through 1993 by Wolfram Research Inc.
#
# $Id: psfix,v 1.10.2.1 1993/03/26 22:29:28 rory Exp $
#
# This shell file reads stdin (or any list of files) and adds the
# PostScript macros that Mathematica output assumes.  It uses a
# default type size of 10 points, an 8.5 by 11 inch page and 1/2 inch
# margins.  The fonts used are specified from Mathematica.  The 
# default font is Courier 10.  Other fonts can be introduced with
# a change to $DefaultFont, assigning the DefaultFont option for
# or by using the FontForm directive.
#
# Usage is:
#	psfix [-lmarg l] [-rmarg r] [-bmarg b] [-tmarg r]
#                 [-land] [-stretch] [-width w] -height h]
#                     [-epsf] [-stringfix] [-dump] [file ...]
# where the -land option causes the plot to be done in landscape mode
# (i.e., the x-axis runs along the 11 inch side of the page).
# Note, the sed is simply to strip off comments to save time and
# space.


# Defaults:
lmarg=1.0		# default left margin: 1.0 inches
rmarg=1.0		# default right margin: 1.0 inches
bmarg=1.0		# default bottom margin: 1.0 inches
tmarg=1.0		# default top margin: 1.0 inches
transform=''		# default transformation: identity
nodistort=true		# default no-stretching: no
fixwid=true		# default fixwid compensation
fixdash=false		# default fixdash compensation
landg=false		# landscape flag
epsfg=true		# default for epsf generation
news=true
while :; do
	case $# in
	0)
		break 1
		;;
	esac
	case "$1" in
	-lmarg)
		shift
		lmarg=$1
		shift
		;;
	-rmarg)
		shift
		rmarg=$1
		shift
		;;
	-bmarg)
		shift
		bmarg=$1
		shift
		;;
	-tmarg)
		shift
		tmarg=$1
		shift
		;;
	-width)
		shift
		width=$1
		shift
		;;
	-height)
		shift
		height=$1
		shift
		;;
	-land)
		landg=true
		transform="90 rotate 0 Mheight neg translate"
		shift
		;;
	-stretch)
		nodistort=false
		shift
		;;
	-stringfix)
		fixwid=false
		shift
		;;
	-dashfix)
		fixdash=false
		shift
		;;
	-epsf)
		epsfg=true
		shift
		;;
	-news)
		news=true
		shift
		;;
	-dump)
		dump=true
		shift
		;;
	-*)
		echo \
'Usage: psfix [-lmarg inches] [-rmarg inches] [-bmarg inches] [-tmarg inches]
                          [-land] [-stretch] [-width inches] [-height inches] 
                          [-epsf] [-dump] [-stringfix] [-dashfix] [file...]' \
			>&2
		exit 1
		;;
	*)
		break 1
		;;
	esac
done

# Default values for height and width
if [ ".$epsfg" = ".true" ] ; then
	lmarg=0
	rmarg=0
	bmarg=0
	tmarg=0
	if [ ".$height" = "." ] ; then
		height=4
	fi
	if [ ".$width" = "." ] ; then
		width=4
	fi
else
	if [ ".$height" = "." ] ; then
		if [ ".$landg" = ".false" ] ; then
			height=11
		else
			height=8.5
		fi
	fi
	if [ ".$width" = "." ] ; then
		if [ ".$landg" = ".false" ] ; then
			width=8.5
		else
			width=11
		fi
	fi
fi

# Compute bounding box
#(
#bc -l <<!
#$lmarg * 72
#$bmarg * 72
#($width - $rmarg) * 72
#($height - $tmarg) * 72
#!
#) | (
#	read left
#	read bottom
#	read right
#	read top
#)

left=`echo "$lmarg * 72" | bc -l | sed 's/\..*$//'`
bottom=`echo "$bmarg * 72" | bc -l | sed 's/\..*$//'`
right=`echo "($width - $rmarg) * 72" | bc -l | sed 's/\..*$//'`
top=`echo "($height - $tmarg) * 72" | bc -l | sed 's/\..*$//'`


date=`date`
preview=''
body=''

# Redefine variables to their names if dumping

if [ ".$dump" = ".true" ] ; then
	textsize="\$textsize"
	width="\$width"
	height="\$height"
	left="\$left"
	right="\$right"
	top="\$top"
	bottom="\$bottom"
	lmarg="\$lmarg"
	rmarg="\$rmarg"
	bmarg="\$bmarg"
	tmarg="\$tmarg"
	nodistort="\$nodistort"
	transform="\$transform"
	time="\$time"
	date="\$date"
	preview="\$preview"
	body="\$body"
fi

# Output header
echo '%!PS-Adobe-2.0 EPSF-2.0'
echo "%%BoundingBox: $left $bottom $right $top"
cat << !
%%Creator: Mathematica
%%CreationDate: $date
%%EndComments
$preview
!

if [ ".$news" != ".true" ] ; then
cat << !
/Mathdict 150 dict def
Mathdict begin
!
fi

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

/Mlmarg		$lmarg 72 mul def	% left margin (points)
/Mrmarg		$rmarg 72 mul def	% right margin (points)
/Mbmarg		$bmarg 72 mul def	% top margin (points)
/Mtmarg		$tmarg 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
/g { setgray} bind def
/k { setcmykcolor} bind def
/m { moveto} bind def
/p { gsave} bind def
/r { setrgbcolor} bind def
/w { setlinewidth} bind def
/C { curveto} bind def
/F { fill} bind def
/L { lineto} bind def
/P { grestore} bind def
/s { stroke} bind def
Mswitches
!


cat << !
$body
!

if [ ".$dump" != ".true" ] ; then
	sed '/^%!$/d
	/^%%Creator:/d' $@
fi

if [ ".$news" != ".true" ] ; then
cat <<'!'
end
!
fi

if [ ".$epsfg" != ".true" ] ; then
cat <<'!'
showpage
!
fi
