#!/bin/sh

# Generate an Encapsulated Postscript file (eps) from the
# PLplot pstex driver generated files.

if test $# = "0"; then
	echo -e "\n\
	Usage: pstex2eps <filename>\n\
	\n\
	Where <filename> is the name of the output file you specified\n\
	with the -o option in PLplot.\n\
	There must exist two files, the postscript file without text,\n\
	<filename>, and the latex file with the text, <filename_t>.\n\
	The output file will be named <filename.eps>.\n\
	\n\
	The Computer Modern Type 1 fonts will be included in the output\n\
	eps file. If you intent to use the plot figure in LaTeX, just\n\
	use a plain \include{<filename_t>} in your latex file.\n"
	exit 1
fi

if test ! -e $1 -o ! -e $1_t; then
	echo "$1 or $1_t don't exist. Exiting."
	exit 1
fi

ifile=$1
ofile=`mktemp pstex2eps.XXXXXX`

cat > ${ofile}.tex <<EOF
\documentclass{article}
\usepackage{ae,aecompl}
\usepackage{graphicx}
\begin{document}
\pagestyle{empty}
This is a  preview demo of your plot, the 'pstex' driver is intended
to be used with \LaTeX.
Use \LaTeX math capabilities as strings in your plots,
and they will appear as you would expect.
\begin{figure}[!htb]
{\centering{\rotatebox{-90}{\scalebox{0.5}{\input{${ifile}_t}}}}
\caption{This is how your plot will look.}}
\end{figure}
\end{document}
EOF

latex ${ofile} > /dev/null \
&& dvips -Pcmz -o ${ifile}.eps $ofile 2> /dev/null

rm ${ofile}*
