#!/bin/bash
#
# $Id$
#
# SML/NJ startup script (for 109.27 or later).
#
# Global vars: INFILE OUTFILE MLTEXT TERMINATE NOWRITE ISABELLE_TMP,
# and from settings
## diagnostics
function fail_out()
{
echo "Unable to create output heap file: \"$OUTFILE\"" >&2
exit 2
}
## basic setup
EXIT="fun exit 0 : unit = OS.Process.exit OS.Process.success | exit _ = OS.Process.exit OS.Process.failure;"
COMMIT="fun commit () = not (SMLofNJ.exportML\"$OUTFILE\");"
COMMIT_RO='fun commit () = (TextIO.output (TextIO.stdErr, "Error - Database is not opened for writing.\\n"); false);'
eval $($ML_HOME/.arch-n-opsys)
SUFFIX=".$ARCH-$OPSYS"
## prepare databases
DB="$INFILE"
if [ -n "$DB" ]; then
DB="@SMLload=$INFILE"
EXIT=""
fi
[ -z "$OUTFILE" ] && COMMIT="$COMMIT_RO"
[ -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
sh -c "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
#fix heap file name
[ -n "$OUTFILE" -a -n "$SUFFIX" -a -f "$OUTFILE$SUFFIX" ] \
&& mv "$OUTFILE$SUFFIX" "$OUTFILE"
[ -n "$OUTFILE" -a -n "$NOWRITE" ] && chmod -w "$OUTFILE"
exit $RC