#!/bin/bash
#
# Script to batch convert tex, amstex, and latex into pdf                     
#
# eluhrs@math, 2/27/04
#

##
## Give syntax if called with no args.
##

if [ $# == 0 ]

  then
    echo "Usage: tex2pdf filename(s).tex"
    exit 1

fi

##
## For each input file, determine format, and set the right command.
##

for INFILE in $*

do

  if /usr/local/gnu/bin/grep -q amstex $INFILE
    then 
      COMMAND=/usr/local/bin/amstex

  elif /usr/local/gnu/bin/grep -qi "plain tex" $INFILE
    then 
      COMMAND=/usr/local/bin/tex

  else
      COMMAND=/usr/local/bin/latex

  fi

##
## Set current file.
##

  CURRENT=`basename $INFILE .tex`

##
## Compile the source file.
##

  $COMMAND $INFILE

##
## If source file is LaTex, then compile again for tables, notes, etc.
##

  if [ -e $CURRENT.aux ]
    then
      $COMMAND $INFILE
      rm $CURRENT.aux
  fi

##
## Run dvips, ps2pdf, remove remnants, make PDF world readable.
##

  /usr/local/bin/dvips -o $CURRENT.ps -Ppdf -G0 -t letter $CURRENT.dvi
  /usr/local/bin/ps2pdf -dMaxSubsetPct=100 -dCompatibilityLevel=1.2 -dSubsetFonts=true -dEmbedAllFonts=true $CURRENT.ps $CURRENT.pdf
  rm $CURRENT.dvi $CURRENT.ps $CURRENT.log
  chmod 644 $CURRENT.pdf

done
