#! /bin/sh
TMPBASE=/tmp
TMP=${TMPBASE}/imagen$$
LATEX=latex
DVIPS=dvips
GS=gs
PNMCROP=pnmcrop
PNMEXTRA=""
DVIPSMAG=1414
EXT=gif
PPMTOGIF=ppmtogif
PPMTOGIFOPTS="-transparent  #ffffff"
PPMQUANT=cat
PPMQUANTOPTS=""
PNMMARGIN=pnmmargin
RM="/bin/rm -f"

doconvert()
{
  pnmtopng -transparent '#000000'
}

donothing ()
{
  cat
}

while true
do
    case $1 in
     -gif)
       ;;
     -png)
        PPMTOGIF=doconvert
        PPMTOGIFOPT=""
        EXT=png
        ;;
     -pnm)
        PPMTOGIF=donothing
        PPMTOGIFOPT=""
        EXT=pnm
        ;;  
     -quant)
        shift
        PPMQUANT=ppmquant
        PPMQUANTOPTS=$1
        ;;
    -extra)
        shift
        PNMEXTRA="$1"
        ;;
    -mag)
        shift
        DVIPSMAG="$1"
        ;;
    *)
        break
        ;;
    esac
    shift
done

#always quantize for gifs (ppmtogif ouputs non valid files when colors >= 256)
if [ "$EXT" = "gif" -a  "$PPMQUANT" = "cat" ]
then
  PPMQUANT=ppmquant
  PPMQUANTOPTS=255
fi


trap "/bin/rm -rf ${TMP} ; exit 2" 1 2 3 8 10 13 15
case $0 in
  '')
   BASE=image
   ;;
  *)
   BASE=$1
  ;;
esac

NAME=${BASE}.image
DVIPSOPTS="-t a3 -x${DVIPSMAG}"
GSOPTS="-q -sPAPERSIZE=a3 -dNOPAUSE -DNOPROMPT -sDEVICE=ppmraw -dTextAlphaBits=2"
PNMMARGINOPTS="-white 1"

NEWDVIPS=true
if ${DVIPS} -i 2>&1 | grep -i 'bad option' 2> /dev/null 1> /dev/null
then
  NEWDVIPS=false
fi


test -f  ${NAME}.tex ||\
{ echo "Failure: no ${NAME}.tex file!" 2>&1 ; exit 2 ; }
${LATEX} ${NAME}.tex

mkdir ${TMP} || { echo "Failure: cannot create tmp dir $TMP" 2>&1 ; exit 2 ; }

if ${NEWDVIPS}
then
  DVIPSOPTS="${DVIPSOPTS} -S 1 -i"
  ${DVIPS} ${DVIPSOPTS} -o${TMP}/${BASE} ${NAME}.dvi
  ${RM} ${NAME}.log ${NAME}.aux ${NAME}.dvi

  test -f ${TMP}/${BASE}001 || {  echo "Failure: no ps files!" 2>&1 ; exit 2 ; }
  for i in ${TMP}/${BASE}[0-9][0-9][0-9]
  do
    echo "Translating Postscript file: $i"
    ${GS} ${GSOPTS} -sOutputFile=- $i -c quit |\
    ${PNMCROP} |\
    case ${PNMEXTRA} in
      "")
       cat
       ;;
      *)
       sh -c "${PNMEXTRA}"
       ;;
    esac |\
    ${PNMMARGIN} ${PNMMARGINOPTS} |\
    ${PPMQUANT} ${PPMQUANTOPTS} |\
    ${PPMTOGIF} ${PPMTOGIFOPTS} > `basename $i`.${EXT}
    ${RM} $i
  done
else
  ${DVIPS} ${DVIPSOPTS} -o${TMP}/${BASE}.ps ${NAME}.dvi
  ${RM} ${NAME}.log ${NAME}.aux ${NAME}.dvi

  test -f ${TMP}/${BASE}.ps || { echo "No ps file!" 2>&1 ; exit 2 ; }
  echo "Translating (and cutting) Postscript file ${TMP}/${BASE}.ps into ppm"
  (echo "(${TMP}/${BASE}.ps) run" ; echo "quit") |\
  ${GS} ${GSOPTS} -sOutputFile=${TMP}/${BASE}%03d
  ${RM} ${TMP}/${BASE}.ps

  test -f ${TMP}/${BASE}001 || { echo "No ppm files!"  2>&1 ; exit 2 ; }
  for i in ${TMP}/${BASE}[0-9][0-9][0-9]
  do
    echo "Translating ppm file: $i"
    ${PNMCROP}  < $i |\
    case ${PNMEXTRA} in
      "")
       cat
       ;;
      *)
       sh -c "${PNMEXTRA}"
       ;;
    esac |\
    ${PNMMARGIN} ${PNMMARGINOPTS} |\
    ${PPMQUANT} ${PPMQUANTOPTS} |\
    ${PPMTOGIF} ${PPMTOGIFOPTS}  > `basename $i`.${EXT}
    ${RM} $i
  done
fi

rmdir ${TMP}