#!/bin/sh
#
# This is mathfig, Copyright 1992 Silvio Levy
# $Header: /u/levy/texts/geombook/RCS/mathfig.orig,v 1.3 1994/11/19 05:07:24 levy Exp $
#
umask 002

# The following lines give the locations of the auxiliary scripts
# geomfix and mathlabels.
geomfix=geomfix
mathlabels=mathlabels

init=""
verbose="False"

if test $# -eq 0
then
        echo "Usage: $0 [-v] [-d directory] [-i initfile] filenames"; exit;
fi

while test $# -gt 0
do
        case "$1" in
        "-d") 
        if test $# -lt 3
        then
          echo "Usage: $0 [-v] [-d directory] [-i initfile] filenames"; exit;
        fi;
        if test -d "$2" && test -w "$2" && test -r "$2"
              then	
        	cd "$2"; shift; shift ;
              else
        	echo "Directory $2 not found or permissions wrong"; exit;

              fi;;
        "-i") 
        if test $# -lt 3
        then
          echo "Usage: $0 [-v] [-d directory] [-i initfile] filenames"; exit;
        fi;
        init="$2"; shift; shift;;
        "-v") verbose="True"; shift;;
        "-"*) 
          echo "Usage: $0 [-v] [-d directory] [-i initfile] filenames"; exit;;
        *) break;
        esac
done

for i in "$@"
do
        pref=`echo $i | sed 's/\.m$//'`
        /bin/rm -f $pref.ps $pref.lab

        math -batchoutput -run 'Off[General::spell1]' << EOF
        (\$Def[x_?NumberQ,_] := ToString[x]; 
        \$Def[__,default_] := default;
        If["$init"!="", If[$verbose, Print["Reading $init..."]]; Get["$init"]];
        If[$verbose,Print["Reading $pref.m..."]]; picture=Get["$pref.m"];
        Display[StringJoin[
            "! $geomfix -width ",
               \$Def[\$width,"5"], " -height ", \$Def[\$height,"2"], 
               " -lmarg 0 -rmarg 0 -tmarg 0 -bmarg 0 ",
            "| $mathlabels $pref.lab ", \$Def[\$xlo,"OK"], " ", 
               \$Def[\$xhi,"OK"], " ", \$Def[\$ylo,"OK"], " ", 
               \$Def[\$yhi,"OK"], "> $pref.ps"],
          picture];)
EOF
        if test `ls -l $pref.lab | awk '{print $4;}'` -eq 0
        then
                /bin/rm $pref.lab
        fi
done