#!/bin/bash
#
# $Id$
# Author: Markus Wenzel, TU Muenchen
# License: GPL (GNU GENERAL PUBLIC LICENSE)
#
# SML/NJ startup script (for 0.93).
export -n INFILE OUTFILE COMPRESS MLTEXT TERMINATE NOWRITE
## diagnostics
function fail_out()
{
echo "Unable to create output heap file: \"$OUTFILE\"" >&2
exit 2
}
function check_mlhome_file()
{
if [ ! -f "$1" ]; then
echo "Unable to locate $1" >&2
echo "Please check your ML_HOME setting!" >&2
exit 2
fi
}
## prepare databases
if [ -z "$INFILE" ]; then
INFILE="$ML_HOME/sml"
check_mlhome_file "$INFILE"
EXIT="val exit: int -> unit = System.Unsafe.CInterface.exit;"
else
EXIT=""
fi
MOVE=""
if [ -z "$OUTFILE" ]; then
COMMIT='fun commit () = (output (std_err, "Error - Database is not opened for writing.\n"); false);'
else
if [ "$INFILE" -ef "$OUTFILE" ]; then
OUTDIR=$(dirname "$OUTFILE")/tmp
OUTFILE="$OUTDIR"/$(basename "$OUTFILE")
mkdir -p "$OUTDIR" || fail_out
MOVE=true
fi
[ -f "$OUTFILE" ] && { chmod +w "$OUTFILE" || fail_out; }
COMMIT="fun commit () = not (exportML\"$OUTFILE\");"
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; "$INFILE" $ML_OPTIONS; RC="$?"; kill -HUP "$FPID"; exit "$RC"; }
RC="$?"
## fix heap file
[ -n "$OUTFILE" -a -f "$OUTFILE" -a -n "$NOWRITE" ] && chmod -w "$OUTFILE"
if [ -n "$MOVE" -a -f "$OUTFILE" ]; then
rm -f "$INFILE" || fail_out
mv "$OUTFILE" "$INFILE" || fail_out
fi
exit "$RC"