#!/usr/bin/env bash
#
# $Id$
# Author: Markus Wenzel, TU Muenchen
# License: GPL (GNU GENERAL PUBLIC LICENSE)
#
# MLWorks startup script (for 1.0r2 or later).
export -n INFILE OUTFILE COPYDB COMPRESS MLTEXT TERMINATE NOWRITE
## diagnostics
function fail_out()
{
echo "Unable to create output heap file: \"$OUTFILE\"" >&2
exit 2
}
## prepare databases
if [ -z "$INFILE" ]; then
EXIT="fun exit 0 = (OS.Process.exit OS.Process.success): unit | exit _ = OS.Process.exit OS.Process.failure;"
MLWORKS="mlworks-basis -tty"
else
EXIT=""
MLWORKS="mlimage -load $INFILE -tty"
fi
if [ -z "$OUTFILE" ]; then
COMMIT='fun commit () = (TextIO.output (TextIO.stdErr, "Error - Database is not opened for writing.\n"); false);'
else
COMMIT="fun commit () = (Shell.saveImage (\"$OUTFILE\", false); true);"
[ -f "$OUTFILE" ] && { chmod +w "$OUTFILE" || fail_out; }
fi
## run it!
MLTEXT="$EXIT $COMMIT $MLTEXT"
MLEXIT="commit();"
if [ -z "$TERMINATE" ]; then
FEEDER_OPTS=""
else
FEEDER_OPTS="-q"
fi
"$ISABELLE_HOME/lib/scripts/feeder" -p -h "$MLTEXT" -t "$MLEXIT" $FEEDER_OPTS | \
{ read FPID; "$ML_HOME"/$MLWORKS $ML_OPTIONS 2>&1; RC="$?"; kill -HUP "$FPID"; exit "$RC"; }
RC="$?"
[ -n "$OUTFILE" -a -f "$OUTFILE" -a -n "$NOWRITE" ] && chmod -w "$OUTFILE"
exit "$RC"