#!/bin/bash
# issu de tex2im 1.8, mais utilisant dvipng 1.9 au lieu de dvips

export PATH=/usr/local/bin:/usr/local/teTeX/bin/powerpc-apple-darwin-current:${PATH}
#
# Default values
#

resolution="72"
format="png"
color1="white"
color2="black"
trans=1
noformula=0
extra_header="$HOME/.tex2png_header"

if [ -f ~/.tex2pngrc ]; then
	source ~/.tex2pngrc
fi

OPTERR=0

if [ $# -lt 1 ]; then
	echo "Usage: `basename $0` [options] file.tex, for help give option -h" 1>&2
	exit 1
fi

while getopts hanzb:t:f:o:r:vx: Optionen; do
	case $Optionen in
		h) echo "tex2png [options] inputfile

The content of input file should be _plain_ latex mathmode code!
Alternatively, a string containing the latex code can be specified.

Options:
-v         show version
-h         show help
-o file    specifies output filename, 
           default is inputfile with new extension
-f expr    specifies output format, 
           possible examples: gif, jpg, tif......
           all formates supported by "convert" should work, 
           default: $format
-r expr    specifies desired resolution in dpi, 
           possible examples: 100, 300, 200, 
           default is $resolution
-b expr    specifies the background color
           default: $color1
-t expr    specifies the text color
           default: $color2
-n         no-formula mode (do not wrap in eqnarray* environment)
           default: off
-z         transparent background
           default: on
-x file    file containing extra header lines.
           default: ~/.tex2png_header"
			exit 0 ;;
		v) echo "TEX2PNG Version 1.0"
			exit 0 ;;
		r) resolution=$OPTARG;;
		o) outfile=$OPTARG;;
		z) trans=1;;
		n) noformula=1;;
		b) color1=$OPTARG;;
		t) color2=$OPTARG;;
		f) format=$OPTARG;;
      x) extra_header=$OPTARG;; 		
	esac
done

#
# Generate temporary directory
#

if test -n "`type -p mktemp`" ; then
	tmpdir="`mktemp /tmp/tex2pngXXXXXX`"
	rm $tmpdir
	mkdir $tmpdir
else
	tmpdir=/tmp/tex2png$$
	if [ -e $tmpdir ] ; then
		echo "$0: Temporary directory $tmpdir already exists." 1>&2
		exit 1
	fi
	mkdir $tmpdir
fi
homedir="`pwd`" || exit 1  

#
# Names for input and output files
#

while [ $OPTIND -le $# ]
do

eval infile=\$${OPTIND}   

if [ -z $outfile ]; then	
	if [ -e "$infile" ]; then
		base=`basename ${infile} .tex` ;
		outfile=${base}.$format
	else
		outfile=out.$format
	fi
fi

#
# Here we go
#

(
cat << ENDHEADER1
\documentclass[12pt]{article}
\usepackage{color}
\usepackage[dvips]{graphicx}
\pagestyle{empty}
ENDHEADER1
) > $tmpdir/out.tex

#
# Do we have a file containing extra files to include into the header?
#

if [ -f $extra_header ]; then
	(
	cat $extra_header
	) >> $tmpdir/out.tex
fi

if [ $noformula -eq 1 ]; then
(
cat << ENDHEADER2
\pagecolor{$color1}
\begin{document}
{\color{$color2}
ENDHEADER2
) >> $tmpdir/out.tex
else
(
cat << ENDHEADER2
\pagecolor{$color1}
\begin{document}
{\color{$color2}
\begin{eqnarray*}
ENDHEADER2
) >> $tmpdir/out.tex
fi

if [ -e "$infile" ]; then
	cat $infile >> $tmpdir/out.tex
else
	echo $infile >> $tmpdir/out.tex
fi	

if [ $noformula -eq 1 ]; then
(
cat << ENDFOOTER
}\end{document}
ENDFOOTER
) >> $tmpdir/out.tex
else
(
cat << ENDFOOTER
\end{eqnarray*}}
\end{document}
ENDFOOTER
) >> $tmpdir/out.tex
fi

cd $tmpdir
for f in $homedir/*.eps; do
    test -f ${f##*/} || ln -s $f . # multi-processing!
done
latex -interaction=batchmode out.tex > /dev/null
cd "$homedir"
if [ $trans -eq 1 ]; then
	bgcolor="Transparent"
else	
	bgcolor="$color1"
fi

dvipng -T tight -D $resolution -bg $bgcolor --gamma 1.5 $tmpdir/out.dvi -o $tmpdir/out1.png > /dev/null

if [[ $color1 == "white" && $color2 == "black" ]]; then
	convert -type GrayscaleMatte -background $color1 $tmpdir/out1.png $tmpdir/out.$format
else
	convert -background $color1 $tmpdir/out1.png $tmpdir/out.$format
fi

if [ -e $tmpdir/out.$format ]; then
	mv $tmpdir/out.$format $outfile
else
	mv $tmpdir/out.$format.0 $outfile
fi

let OPTIND=$OPTIND+1
outfile=""
done

#
# Cleanup
#

rm -rf $tmpdir 
exit 0
