#!/bin/sh

if [ "$1" = "" ]; then
  echo -e 'Usage: mptoepsgif FILE [DPI [-noantialias]]\n'
  exit 1
fi

if [ "$2" = "" ]; then
  DPI=300
else
  DPI=$2
fi

DIR=$PWD
FILE=$DIR/`echo $1 | awk 'BEGIN{FS=".";} {print $1}'`
MP=$FILE.mp
EPS=$FILE.eps
GIF=$FILE.gif

#TEXMFCNF=$DIR; export TEXMFCNF
TEX=latex ; export TEX
HSIZE=18.85cm
VSIZE=15.48cm

#cd /tmp

cp $MP mptogif.$$.mp
echo "\\hsize=$HSIZE\\vsize=$VSIZE\\input epsf\\nopagenumbers\\leavevmode\\noindent\\epsfbox{mptogif.$$.1}\\vfill\\end" > mptogif.$$.tex
#ln -sf $DIR/mbmac.mp
#ln -sf $DIR/axonometry.mp
#ln -sf $DIR/mbmac.tex
#ln -sf $DIR/data
#ln -sf $DIR/psfixbb

mpost mptogif.$$.mp
cp mptogif.$$.1 $FILE.1
tex mptogif.$$.tex
dvips -E -V -T $HSIZE,$VSIZE -o mptogif.$$.ps mptogif.$$.dvi
#./psfixbb -o $EPS mptogif.$$.ps
cp mptogif.$$.ps $EPS
gs -q -dSAFER -sPAPERSIZE=a4 -sDEVICE=ppmraw -sOutputFile=- -dNOPAUSE -r"$DPI"x"$DPI" $EPS quit.ps | pnmcrop > mptogif.$$.pnm
if [ "$3" != "-noantialias" ]; then
  pnmscale 0.5 < mptogif.$$.pnm | ppmtogif > $GIF
else
  ppmtogif < mptogif.$$.pnm > $GIF
fi
rm mptogif.$$.*
#rm mbmac.mp axonometry.mp mbmac.tex data
#cd $DIR

