#!/bin/bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# mp2eps version 1.0 : produce postscript files from metapost sources #
# Copyright (C) 2004 Glad Deschrijver, Wed Jun 16 2004, Wed Oct 06 2004 #
# Glad.Deschrijver@UGent.be #
# #
# 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. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, #
# USA. #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
export TEX=latex
delete=yes
preview=no
previewcmd=gv
rename=yes
standalone=yes
typeset=latex
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`
version=1.0
copyright="Copyright (C) 2004 Glad Deschrijver"
bq='`'
eq="'"
file=""
extension=1
mp2epstmp=_mp2eps_
usage()
{
cat << EOF
Usage: $progname [OPTIONS] file[.mp]
Options: [defaults in brackets after descriptions]
Configuration:
--typeset=CMD use CMD for typesetting [$typeset]
--standalone=yes/no produce standalone EPS figures [$standalone]
-s short for --standalone=yes
--delete=yes/no delete auxiliary files produced by mpost
[$delete]
--rename=yes/no rename outputfiles to name mentioned in
file.mp as e.g.
%
--preview=yes/no preview using program defined
by --previewcommand [$preview]
-p short for --preview=yes
--previewcommand=CMD use CMD to preview the output file [$previewcmd]
-h, --help display this help and exit
-v, --version output version information and exit
EOF
}
parsefilename()
{
if `test -z "$file"`
then
echo "$progname: At least one file name is required as an argument."
exit 1
else
F=`echo $file | sed -e "s/\.[^\.]*$//"`
ext=`echo $file | sed -e "s/.*\.//g"`
if `test "$ext" = "mp"`
then
file=$F
fi
if `test ! -r "$file.mp"`
then
echo "$progname: $file.mp not found or not readable."
exit 1
fi
fi
path=`echo $file | sed -e "s/[^\/]*$//g"`
file=`echo $file | sed -e "s/.*\///g"`
if `test ! "$path" = ""`
then
cd "$path"
fi
}
createtexfile()
{
tmpfile2="${tmpfile}.tex"
echo "\documentclass{report}" > $tmpfile2
echo "\usepackage{amssymb}" >> $tmpfile2
echo "\usepackage{amsmath}" >> $tmpfile2
echo "\usepackage{times,mathptmx}" >> $tmpfile2
echo "\usepackage{graphicx}" >> $tmpfile2
echo "\usepackage{color}" >> $tmpfile2
echo "\begin{document}" >> $tmpfile2
echo "\pagestyle{empty}" >> $tmpfile2
echo "\includegraphics{$file.$extension}" >> $tmpfile2
echo "\end{document}" >> $tmpfile2
}
typesetfile()
{
tmpfile="$file$mp2epstmp"
mpto -tex "$file.mp" > "${tmpfile}.tex"
latex "${tmpfile}.tex"
dvitomp "${tmpfile}.dvi" "$file.mpx"
rm $tmpfile*
mpost "$file.mp"
nums=`cat "$file.mp" | grep "^beginfig" | sed -e "s/.*(//" -e "s/).*//"`
for i in $nums
do
extension=$i
file2="${file}_$i"
if `test "$standalone" = "yes"`
then
createtexfile
latex $tmpfile
dvips -E $tmpfile.dvi -o $tmpfile.eps
sed -e "s/$tmpfile\.eps/\"$file2.eps\"/g" \
-e "s/$tmpfile\.dvi/\"$file2\.dvi\"/g" $tmpfile.eps > "$file2.eps"
rm $tmpfile*
else
sed -e "s/$tmpfile\.eps/\"$file2.eps\"/g" \
-e "s/$tmpfile\.dvi/\"$file2\.dvi\"/g" \
${file}.${extension} > "$file2.eps"
fi
if `test "$preview" = "yes"`
then
gv "$file2.eps" &
fi
done
}
deleteauxfiles()
{
rm "$file.mpx" 2>/dev/null
rm "$file.log" 2>/dev/null
nums=`cat "$file.mp" | grep "^beginfig" | sed -e "s/.*(//" -e "s/).*//"`
for i in $nums
do
rm "${file}.$i"
done;
if `test "$rename" = "yes"`
then
export TEMPIFS=$IFS
export IFS=$'\n'
for a in `grep "\%.*" $file.mp`
do
i=`echo $a | sed -r "s/.*number=\"([^\"]*)\".*/\1/"`
rm "${file}_$i.eps"
done;
export IFS=$TEMPIFS
fi
rm mptextmp.mp
rm mptextmp.mpx
}
renameoutputfiles()
{
export TEMPIFS=$IFS
export IFS=$'\n'
# lines should contain e.g. %
for a in `grep "\%.*" $file.mp`
do
i=`echo $a | sed -r "s/.*number=\"([^\"]*)\".*/\1/"`
fileouttmp=`echo $a | sed -r "s/.*name=\"([^\"]*)\".*/\1/"`
fileout=`echo $fileouttmp | sed -r "s/(.eps|.ps)$//"`
tmpfile="${file}_$i"
sed -e "s/$tmpfile\.eps/$fileout\.eps/g" \
-e "s/$tmpfile\.dvi/$fileout\.dvi/g" "$tmpfile.eps" > "$fileout.eps"
done
export IFS=$TEMPIFS
}
while test x"$1" != x
do
case $1 in
-h|--help) usage; exit 0;;
--preview=*)
preview=`echo $1 | sed -e 's/--preview=//'`
shift
;;
-p) preview=yes; shift;;
--delete=*)
delete=`echo $1 | sed -e 's/--delete=//'`
shift
;;
--standalone=*)
standalone=`echo $1 | sed -e 's/--standalone=//'`
shift
;;
-s) standalone=yes; shift;;
--rename=*)
rename=`echo $1 | sed -e 's/--rename=//'`
shift
;;
--typeset=*)
typeset=`echo $1 | sed -e 's/--typeset=//'`
shift
;;
-v|--version)
echo "$progname $version"
echo "$copyright"
echo "This program comes with ABSOLUTELY NO WARRANTY. This is free"
echo "software, and you are welcome to redistribute it under"
echo "the terms of the GNU General Public License."
exit 0
;;
-*) usage; exit 1;;
*) file=$1; shift;;
esac
done
export TEX=$typeset
parsefilename
typesetfile
if `test "$rename" = "yes"`
then
renameoutputfiles
fi
if `test "$delete" = "yes"`
then
deleteauxfiles
fi
exit 0