#! /bin/sh
# convert ps file to Gif
#
resolution=95
quantcmd="cat"
transcmd="-transparent #ffffff "
antialiasing=""
#
usage() {
  echo "Usage:" 1>&2
  echo "  ps2gif [ -res RESOLUTION ] [ -notrans ] psfile.ps" 1>&2
  echo " " 1>&2
  exit 1
}

[ $# -lt 1 ] && usage

while [ $# -gt 0 ]; do
  case $1 in
    -quant)
      quantcmd="ppmquant 50"
      ;;
    -notrans)
      transcmd=""
      ;;
    -antialiasing)
      antialiasing="-dTextAlphaBits=4 -dGraphicsAlphaBits=4"
      ;;
    -res)
      shift
      if [ $# -eq 0 ]; then
        echo "ps2gif: no resolution specified" 1>&2
        exit 1
      fi
      resolution=$1
      ;;
    -*)
      usage;;
    *)
      fig=$1
  esac
  shift
done

gif=`echo $fig | sed -e 's/\.[^\.]*$/.gif/'`

awk -f $HYPERLATEX_DIR/normalize-eps.awk $fig \
| gs -q -dNOPAUSE -r$resolution $antialiasing -sDEVICE=ppm -sOutputFile=- - \
| pnmcrop | $quantcmd | ppmtogif -interlace $transcmd > $gif

echo "Done"
