#!/bin/bash 
#
# $Id: runpltx,v 1.3 2007/01/04 21:59:25 frank Exp $
#
#set -x

MPFILE="FOOBAR"
PARAM=
crop=false
#
## Is string documentclass present in ltx file?
grep documentclass $1 >& /dev/null
if test $? = 1
then 
 # no "documentclass" present --> not LaTeX --> exit
 echo "$1 not runnable LaTeX "
 exit 0
fi

## If eps file already exists --> do nothing

F=`basename $1 .pltx`
if test -r $F.eps
then
  echo $F.eps already made
  exit
fi

## No EPS file yet --> run LaTeX

LATEX=latex
export TEXINPUTS=inputs:pstricks:
export HOMETEXMF=../texmf:
export TEXMFHOME=../texmf:
## in TL2004 the .map and .enc files are under texmf/fonts/[map,enc] only
## therefore I have to add the subdirectories where our such stuff resides.
#export TEXFONTMAPS=../texmf/dvips//:
#export ENCFONTS=../texmf/dvips//:

    echo "*************************** latex $F.pltx FIRST RUN "

$LATEX $F.pltx
if test $? -gt 0
  then
    echo "*************************** latex $F.pltx has ERRORS"
    exit 1
fi
                                                                         
# we need two runs for some PSTricks examples
    echo "*************************** latex $F.pltx SECOND RUN "

$LATEX $F.pltx
if test $? -gt 0
  then
    echo "*************************** latex $F.pltx has ERRORS"
    exit 1
fi
R=`grep ' Process ' $F.log` >& /dev/null
if test $? = 0
then
  # We have to deal with Metapost

  MFILE=`grep ' Process ' $F.log | tail -1 | awk '{print $3}'`

  MPFILE=`basename $MFILE .mp`
    echo "*************************** Metapost run $MFILE.mp "
  mpost $MPFILE
  if test $? -gt 0
  then
    echo "********************** Metapost $MPFILE.mp has ERRORS"
    exit 1
  fi
  $LATEX $F.pltx
  if test $? -gt 0
  then
    echo "*************************** latex $F.pltx has ERRORS"
    exit 1
  fi
else
  R=`grep "t forget to process" $F.log` >& /dev/null
  if test $? = 0
  then
    # We have to process Metafont file
    MFILE=`grep "t forget to process" $F.log | tail -1 | \
             awk '{print $6}' | sed 's/\!//'`
    MFFILE=`basename $MFILE .mf`
    mf "\\mode=ljfour; input $MFFILE"
    if test $? -gt 0
    then
      echo "********************** Metafont $MFFILE.mf has ERRORS"
      exit 1
    fi
    $LATEX $F.pltx
    if test $? -gt 0
    then
      echo "*************************** latex $F.pltx has ERRORS"
      exit 1
    fi
    gftopk $MFFILE.600gf
    PARAM="-mode ljfour -D600"
  else
    R=`grep -i "musixtex" $F.log` >& /dev/null
    if test $? = 0
    then
      # We have to process musxitex file with musixflex
      PARAM="-mode ljfour -D600"
      musixflx $F
      $LATEX $F.pltx
      if test $? -gt 0
      then
        echo "*************************** latex $F.pltx has ERRORS"
        exit 1
      fi
    fi
  fi
fi 
#export KPATHSEA_DEBUG=32
dvips  -o "$F-pics.ps" "$F.dvi"
ps2pdf -dAutoRotatePages=/None "$F-pics.ps" "$F-pics.pdf"
pdflatex "$F.pltx"
if $crop; then
pdfcrop "$F.pdf"
mv "$F-crop.pdf" "$F.pdf"
fi
pdftops -f 1 -l 1 -eps "$F.pdf" 
#
# 

./cleaneps $F.eps



if test ! -s $F.eps
then
  rm $F.eps
  echo "*************************** eps file $F.eps has zero size"
  exit 1
fi
rm -f $F.dvi $F.aux $F.log $F.idx $MPFILE.* ftmp.mp  mfpicex.* $F.mx1 $F.mx2 $F-pics.*