#!/bin/sh
#------------------------------------------------------------------------------
# fixbb: Fix bounding box info in postscript files.
# Written 2/92 by ggh@cs.brown.edu,
# using an idea of Doug Crabill (dgc@cs.purdue.edu).
# Revised 9/30/92: Use GNU utilities, make backup copy of input.
# Revised 3/18/93: Set PATH explicitly.
# Revised 4/23/93: Hack FrameMaker header so that it doesn't set paper size. 
#------------------------------------------------------------------------------

USAGE="Usage: fixbb postscript_file"

if [ $# -ne 1 ]; then echo $USAGE >&2; exit 1; fi

if [ ! -f $1 -o ! -r $1 ]; then echo "fixbb: Cannot open $1" >&2; exit 1; fi
 
TMP1=/tmp/$USER.1.$$				# Temp file name #1.
TMP2=/tmp/$USER.2.$$				# Temp file name #2.
trap 'rm -f $TMP1 $TMP2; exit 1' 0 1 2 3 15	# Remove temp files on exit.

GS=/afs/athena.mit.edu/project/gnu/arch/@sys/bin/gs		# Location of GhostScript.
PNMCROP=/afs/athena.mit.edu/contrib/graphics/arch/@sys/bin/pnmcrop # Location of pnmcrop.
AWK=/afs/athena.mit.edu/project/gnu/arch/@sys/bin/gawk		# Location of awk.
GMV=/afs/athena.mit.edu/project/gnu/arch/@sys/bin/gmv		# Location of gnu mv.
PATH=/usr/ucb:/usr/bin:/bin			# Location of cat, rm, etc.

#------------------------------------------------------------------------------
# Render image using GhostScript.
#------------------------------------------------------------------------------

echo -n "Computing bounding box: ."

cat - $1 << eof | $GS -dNODISPLAY -dQUIET -
  [1 0 0 -1 0 792] 612 792 <ff 00> makeimagedevice setdevice
  /OutFile ($TMP1) (w) file def
  /showpage {OutFile currentdevice writeppmfile erasepage initgraphics} def
eof

if [ ! -s $TMP1 ]; then echo; echo "fixbb: Could not render $1" >&2; exit 1; fi

#------------------------------------------------------------------------------
# Crop image, save messages about number of lines and columns cropped.
#------------------------------------------------------------------------------

echo -n "."

$PNMCROP $TMP1 > /dev/null 2> $TMP2

if [ $? -ne 0 ]; then echo; echo "fixbb: Could not find bbox" >&2; exit 1; fi

#------------------------------------------------------------------------------
# Parse pnmcrop output and compute bounding box.
#------------------------------------------------------------------------------

echo -n "."

$AWK -f - $TMP2 << eof > $TMP1
  BEGIN			{left=0; right=612; top=792; bottom=0;}
  /pnmcrop: .* left/	{left=left+\$3; next}
  /pnmcrop: .* right/	{right=right-\$3; next}
  /pnmcrop: .* top/	{top=top-\$3; next}
  /pnmcrop: .* bottom/	{bottom=bottom+\$3; next}
  END			{print left " " bottom " " right " " top}
eof

if [ $? -ne 0 ]; then echo; echo "fixbb: Could not find bbox" >&2; exit 1; fi

BBOX=`cat $TMP1`
echo "$BBOX"

#------------------------------------------------------------------------------
# Update %%BoundingBox comment in source file, nuke /papersize in Frame output.
#------------------------------------------------------------------------------

echo -n "Editing $1: ."

$AWK -f - $1 << eof > $TMP2
  /^%%BoundingBox/ {if (!done) print "%%BoundingBox: $BBOX"; done=1; next}
  /FrameDict/	{frame=1}
  /%%EndProlog/	{if (frame) print "FrameDict begin /papersize {false} def end"}
  /.*/		{print}
  END		{if (!done) print "%%BoundingBox: $BBOX"}
eof

if [ $? -ne 0 ]; then echo; echo "fixbb: Edit of $1 failed" >&2; exit 1; fi

#------------------------------------------------------------------------------
# Backup original file and replace with updated version.
#------------------------------------------------------------------------------

echo -n "."

$GMV -b $TMP2 $1

if [ $? -ne 0 ]; then echo; echo "fixbb: Could not update $1" >&2; exit 1; fi

echo ".done"
trap 'rm -f $TMP1 $TMP2; exit 0' 0 1 2 3 15
