#!/bin/bash
#
# mp2ps: script to create ps files from metapost that include TeX
#
# It runs the metapost through TeX so that the resuling ps files
# will be viewable in xdvi without error or usable as stand alone
# files.
#
# Version 1.2
# Copyleft (C) 2006 Daniel J. Cross
# http://www.physics.drexel.edu/~dcross
#
# This program can be found at
# http://www.physics.drexel.edu/~dcross/files/mp2ps
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Bugs and comments can be reported to Daniel J. Cross
#
#
# Update to 1.2
# This version adds the -halt-on-error command to mpost to prevent
# hanging on metapost errors. Since mpost sends errors to standard out,
# in order to see errors, all mpost output is sent to the terminal.
if [ -z "$1" ]; then
echo ""
echo "Usage: mp2ps file"
echo "Script assumes .mp for metapost files and will mpost them."
echo "All figs will be evaluated if given an mp file."
echo ""
echo "Any other extension is assumed to be a ps with embedded TeX."
echo "If the file is .ps then the output will be .ps.new, otherwise"
echo "it is just .ps"
exit
fi
ISPOST="0"
i=""
NEW=""
infile=$1
MPOST="mpost"
MPOSTOPTS="-halt-on-error"
TEX="latex"
RM="rm -f"
DVIPS="dvips"
# strip off extension
file=`echo $infile | sed s/'\.'[^.]*$//`
# Check for mp file
if [ `echo $infile | grep .mp$` ]; then
ISPOST="1"
$MPOST $MPOSTOPTS $infile
infile=$file.1
POSTEXTS="log mpx 1"
i="1"
fi
# mpost ok - now redirect outoput
exec 1>/dev/null
exec 2>&1
# loop over all figs from the mp file
while [ -e $infile ]; do
# bare TeX to include graphic
TeXFile="\documentclass{article}
\usepackage{graphicx}
\DeclareGraphicsRule{*}{eps}{*}{}
\begin{document}
\thispagestyle{empty}
\includegraphics{$infile}
\end{document}"
# TeX the file - becomes article.dvi
$TEX $TeXFile
# turn the DVI to a PS and remove empty space (sent errors to null here)
if [ `echo $infile | grep .ps$` ]; then
NEW=".new"
fi
$DVIPS -E -o $file$i.ps$NEW article.dvi
if [ $ISPOST -eq "0" ]; then
break
fi
i=`expr $i + 1`
infile=$file.$i
POSTEXTS="$POSTEXTS $i"
done
# clean up
EXTS="log dvi aux"
for i in $EXTS; do
$RM article.$i
done
for i in $POSTEXTS; do
$RM $file.$i
done