#!/bin/sh
# I hate this shell! I hope the day is close I can do those things
# from the rc shell. /jfw.

# usage: bbset file
#  The computed value is then written instead of the old
#  %%BoundingBox comment or inserted right before the %%EndComments.
#  The result is printed to stdout.

TMP="bbset$$"
TMP2="";

# Refer to the doc for bb4gs what the following command is good for.

echo /show /print load def > $TMP

if [ x$1 != x ]
then
  INFILE=$1;
else
  TMP2=bbset2$$;
  cat > $TMP2 
  INFILE=$TMP2;
fi

GS_LIB=/usr/lib/sdc/bin
export GS_LIB

BB="`(gs -dSAFER -dNODISPLAY $TMP bb4gs.ps $INFILE showpage.ps quit.ps ; \
      echo ' ') | sed -n -e /%%BoundingBox/p`"

# Write a short sed script to exchange/insert the new %%BoundingBox

cat >$TMP <<EOF
:head
/%%BoundingBox/ {
d
i\\
$BB
b rest
}

/%%EndComments/ {
i\\
$BB
b rest
}

n
b head

:rest
s,^/TimesItalic ,/Times-Italic ,
s,^/Times ,/Times-Roman ,
n
b rest
EOF

sed -f $TMP $INFILE

rm $TMP $TMP2
