#!/bin/sh

# This Bourne or Korn Shell script runs MuPAD in batchmode.
# Use option -o to specifiy the output file.

# These are the directories in which you installed MuPAD

# MuPAD_ROOT_PATH is the root directory of the MuPAD-System
# MuPAD_BIN       is the directory of the MuPAD programms
# MuPAD_LIB       is the root directory of the MuPAD-Library
# MuPAD_HELP      is the directory of the Help dependent stuff
# MuPAD_DYNMOD    is the directory of MuPAD moduls

SYSINFO=`$MuPAD_ROOT_PATH/share/bin/sysinfo`

MuPAD_BIN=$MuPAD_ROOT_PATH/$SYSINFO/bin
MuPAD_LIB=$MuPAD_ROOT_PATH/share/lib
MuPAD_HELP=$MuPAD_ROOT_PATH/share/doc/ascii
MuPAD_DYNMOD=$MuPAD_ROOT_PATH/$SYSINFO/modules

error() {
   echo ""
   echo Error: $1
   echo ""
   exit 1
}

DEFAULT_ARGS="-l $MuPAD_LIB -d $MuPAD_DYNMOD -h $MuPAD_HELP"
USER_ARGS=''
IN=''
OUT=""

while test $# -gt 0
do
    case $1 in

    -l | \
    -u | \
    -s | \
    -m | \
    -h | \
    -d | \
    -p | \
    -G )

	USER_ARGS=${USER_ARGS}' '$1
	shift
        if test $# -eq 0 ; then
           $MuPAD_BIN/mupad -help
           exit 1
        else
	   USER_ARGS=${USER_ARGS}' '$1
        fi ;;
    
    -c | \
    -g | \
    -r | \
    -t | \
    -v | \
    -i | \
    -S )

	USER_ARGS=${USER_ARGS}' '$1 ;;

    -o) 

	shift
        if test $# -eq 0 ; then
          error "You must specify an output filename"
        else 
          OUT=$1
        fi;;
    
    *)

	if test -s $1 ; then
	   IN=${IN}' '$1
        else
           error "File $1 doesn't exist"
        fi;;
	
    esac
    shift
done

if test "$OUT" = "" ; then
   error "Use option -o to specify an output file"
fi

if test "$IN" = "" ; then
   error "You must specify an input file"
fi

# Concat all input files, and insert quit to exit mupad automatically

cat $IN > /tmp/$$
echo quit >> /tmp/$$

# This ensures that MuPAD will find all of its modules.

$MuPAD_BIN/mupad $DEFAULT_ARGS $USER_ARGS < /tmp/$$ > $OUT 2>&1 &

# Remove temporary file

rm -f /tmp/$$
