#! /bin/bash
jog="/usr/local/jog07/src/jog"
cdr=$(pwd)
name=${1%.jog}
#echo "ge open $name"
rep=${1%/*}
if [ -d ${rep} ]
then
  cd ${rep}
fi
if $jog <${name}.jog >${name}.ps
then
# Fit BB to drawing
  echo "gs -q -dNOPAUSE -sDEVICE=bbox -- ${name}.ps 2> _BB"
  gs -q -dNOPAUSE -sDEVICE=bbox -- ${name}.ps 2> _BB
# retrieves the bounding box from file _BB
# add a 10 pt white margin
  BB=$(awk '!/Hi/{print($1 " " $2-10 " " $3-10 " " $4+10 " " $5+10)}' _BB)
# insert bounding box comment where it belongs and remove the showpage
  awk -vBB="$BB"  '{
    if ( /%%Bou/ ){
        printf("%s\n",BB)
    } else if ( /showpage/ ) {
        print "% showpage"
    } else {
        print($0)
    }
  }' ${name}.ps > ${name}.eps
  rm _BB
  #rm ${name}.ps
  #gv ${name}.eps
else
  echo "jog error ?"
fi