#!/usr/local/bin/bash
#############################################################
# TEX2IM:   Converts LaTeX formulas to pixel graphics which #
#           can be easily included in Text-Processors like  #
#           M$ or Staroffice.                               #
#                                                           #
# Required software: latex, convert (image magic)           #
# to get color, the latex color package is required         #
#############################################################
# Version 1.3  (http://www.nought.de/tex2im.html)           #
# published under the GNU public licence (GPL)              #
# (c) 11. Oktober 2001 by Andreas Reigber                   #
# Email: anderl@nought.de                                   #
#############################################################

#
# Default values
#
resolution="150x150"
format="png"
color1="white"
color2="black"

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 hb:t:f:o:r:v Optionen; do
	case $Optionen in
		h) echo "tex2im [options] inputfile

The content of input file should be _plain_ latex mathmode code!

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: png
-r expr    specifies desired resolution in dpi, 
           possible examples: 100x100, 300x300, 200x150, 
           default is 150x150
-b expr    specifies the background color
           default: white
-t expr    specifies the text color
           default: black"
			exit 0 ;;
		v) echo "TEX2IM Version 1.3"
			exit 0 ;;
		r) resolution=$OPTARG;;
		o) outfile=$OPTARG;;
		b) color1=$OPTARG;;
		t) color2=$OPTARG;;
		f) format=$OPTARG;;
	esac
done

#
# Generate temporary directory
#

if test -n "`type -p mktemp`" ; then
	tmpdir="`mktemp /tmp/tex2imXXXXXX`"
	rm $tmpdir
	mkdir $tmpdir
else
	tmpdir=/tmp/tex2im$$
	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
#

eval infile=\$${OPTIND}   

if [ -z $infile ]; then
	echo $0: no input file specified
	exit 1
else	
	if [ -z $outfile ]; then	
		base=`basename ${infile} .tex` ;
		outfile=${base}.$format
	fi
fi

#
# Here we go
#

(
cat << ENDHEADER
\documentclass[12pt]{article}
\usepackage{color}
\pagestyle{empty}
\pagecolor{$color1}
\begin{document}
\begin{eqnarray*}
{\color{$color2}
ENDHEADER
) > $tmpdir/out.tex

cat $infile >> $tmpdir/out.tex

(
cat << ENDFOOTER
}\end{eqnarray*}
\end{document}
ENDFOOTER
) >> $tmpdir/out.tex

cd $tmpdir
latex -interaction=batchmode out.tex > /dev/null
cd $homedir
dvips -o $tmpdir/out.eps -E $tmpdir/out.dvi 2> /dev/null
convert +adjoin -density $resolution $tmpdir/out.eps $tmpdir/out.$format

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

#
# Cleanup
#

rm -rf $tmpdir 
exit 0
