#!/bin/bash -norc
#
# $Id$
#
# SML/NJ startup script (for 1.06 or later).
#
# Global vars: INFILE OUTFILE COPYDB MLTEXT TERMINATE,
# and from settings
## diagnostics
function fail_out()
{
echo "Unable to create output heap file: \"$OUTFILE\"" >&2
exit 2
}
## prepare databases
EXIT=""
if [ -z "$INFILE" ]; then
case "$ML_SYSTEM" in
smlnj-1.0[678])
EXIT="val exit = System.Unix.exit;"
;;
smlnj-1.09*)
EXIT="fun exit 0 = OS.Process.exit OS.Process.success | exit _ = OS.Process.exit OS.Process.failure;"
;;
esac
fi
DB="$INFILE"
[ -n "$DB" ] && DB="@SMLload=$INFILE"
COMMIT="fun commit () = not (exportML\"$OUTFILE\");"
if [ -z "$OUTFILE" ]; then
COMMIT='fun commit () = (output (std_err, "Error - Database is not opened for writing.\n"); false);'
elif [ -n "$INFILE" -a ! "$INFILE" -ef "$OUTFILE" ]; then
[ -f "$OUTFILE" ] && { rm -f "$OUTFILE" || fail_out }
cp "$INFILE" "$OUTFILE" || fail_out
fi
[ -n "$OUTFILE" -a -f "$OUTFILE" ] && { chmod +w "$OUTFILE" || fail_out }
MLTEXT="$EXIT $COMMIT $MLTEXT"
MLEXIT="commit();"
## run it!
START_SML="$ML_HOME/sml $ML_OPTIONS $DB"
if [ -n "$TERMINATE" ]; then
{ echo "$MLTEXT" "$MLEXIT" } | $START_SML
RC=$?
elif [ -z "$MLTEXT" ]; then
sh -c "{ $ISABELLE_HOME/lib/scripts/ucat; echo '$MLEXIT'; } | $START_SML"
RC=$?
else
sh -c "{ echo '$MLTEXT'; $ISABELLE_HOME/lib/scripts/ucat; echo '$MLEXIT'; } | $START_SML"
RC=$?
fi
exit $RC