#!/bin/sh
# This version uses latex and dvips
#              with convert (Part of ImageMagick)
#
help()
{
    cat <<HELP
latex2png -- convert latex file to PNG image

USAGE: latex2png [-d density] [-h] [-k] [-H home dir] latex_file

OPTIONS: -d density (where density is in pixels per inch)
         -h         	this help text
         -k         	keep intermediate files (for debugging)
         -H /home/dir	directory to be included in search path

EXAMPLE: latex2png -d 144 -H /usr/home /tmp/eqn  (to create /tmp/eqn.png)

HELP
    exit 0
}

opt_d=144
opt_k=0
home_dir="."
while [ -n "$1" ]; do
case $1 in
    -h) help;shift 1;;
    -k) opt_k=1;shift 1;; # variable opt_f is set
    -d) opt_d=$2;shift 2;; # -d takes an argument -> shift by 2
    -H) home_dir=$2;shift 2;;
    --) break;;
    -*) echo "error: no such option $1. -h for help";exit 1;;
    *)  break;;
esac
done

# input check:
if [ -z "$1" ] ; then
 error "ERROR: you must specify a file, use -h for help"
 exit 1
fi

#process 'latex2png file.tex' and 'latex2png file' equivalently
name=`basename "$1" ".tex"`
dir=`dirname "$1"`
start_dir=`pwd`

#need to search the current working directory for \input or \include
cd $home_dir
TEXINPUTS=`pwd`":"
export TEXINPUTS
cd $start_dir
cd $dir
rm -f $name.png
rm -f $name.dvi
latex  --interaction batchmode $1 > /dev/null

if [ ! -e "$name.dvi" ] ; then
 	error "ERROR: latex failed to create $name.dvi from $name.tex"
	exit 1
fi

dvips -E -o $name.eps $name.dvi >& /dev/null

if [ ! -e "$name.eps" ] ; then
 	error "ERROR: dvips failed to create $name.eps from $name.dvi"
	exit 1
fi

convert -crop 0x0 -density ${opt_d}x${opt_d} $name.eps $name.png

if [ ! -e "$name.png" ] ; then
 	error "ERROR: ImageMagick convert failed to create $name.png from $name.eps"
	exit 1
fi

if [ $opt_k -eq 0 ] ; then
	rm -f $name.dvi
	rm -f $name.aux
	rm -f $name.log
	rm -f $name.eps
fi

exit 0
