#!/bin/sh

if [ $# -ne 1 ]
then
  echo "usage: latex2pdf.sh [file(.tex)]"
else
  # split $1 on / to get the path and filename
  path=`echo ${1%/*}`
  file=`echo ${1##*/}`
  if [ $path = $file ]
  then
    path=`pwd`
  fi

  # check if the file already has the .tex ext
  suffix=`echo $file | grep ".tex$" | wc -l`
  if [ $suffix -eq 0 ]
  then
    f=`echo "$file.tex"`
  else
    f=`echo "$file"`
  fi

  # define the filename base string w/o the .tex ext
  # (what the .aux, .dvi., .ps, .log files will be named)
  s=`echo "$f" | sed -e 's/\.tex$//'`

  # compile the .tex file and convert to pdf
  latex "$path/$f"
  dvips "$s.dvi"
  ps2pdf "$s.ps"
  rm -f "$s.aux"
  rm -f "$s.dvi"
  rm -f "$s.log"
  rm -f "$s.ps"
fi

