#!/usr/bin/env bash
#
# $Id$
# Author: Markus Wenzel, TU Muenchen
#
# Poly/ML startup script.
export -n INFILE OUTFILE COPYDB COMPRESS MLTEXT TERMINATE NOWRITE
## diagnostics
function fail_out()
{
echo "Unable to create output heap file: \"$OUTFILE\"" >&2
exit 2
}
function check_file()
{
if [ ! -f "$1" ]; then
echo "Unable to locate $1" >&2
echo "Please check your ML system settings!" >&2
exit 2
fi
}
## Poly/ML executable and database
ML_DBASE_PREFIX=""
POLY="$ML_HOME/poly"
check_file "$POLY"
if [ -z "$ML_DBASE" ]; then
if [ ! -e "$ML_HOME/ML_dbase" -a "$(basename "$ML_HOME")" = bin ]; then
ML_DBASE_HOME="$(cd "$ML_HOME"; cd "$(pwd -P)"; cd ../lib/poly; pwd)"
else
ML_DBASE_HOME="$ML_HOME"
fi
if [ -z "$COPYDB" ]; then
ML_DBASE_PREFIX="$ML_DBASE_HOME/"
ML_DBASE="ML_dbase"
else
ML_DBASE="$ML_DBASE_HOME/ML_dbase"
fi
export POLYPATH="$ML_DBASE_HOME"
else
export POLYPATH="$(dirname "$ML_DBASE")"
fi
DISCGARB_OPTIONS="-d -c"
EXIT="fun exit 0 = (OS.Process.exit OS.Process.success): unit | exit _ = OS.Process.exit OS.Process.failure;"
## prepare databases
if [ -z "$INFILE" ]; then
check_file "$ML_DBASE_PREFIX$ML_DBASE"
INFILE="$ML_DBASE"
MLTEXT="val use = PolyML.use; $EXIT $MLTEXT"
case "$ML_SYSTEM" in
polyml-4.1.[12])
DISCGARB_OPTIONS="$DISCGARB_OPTIONS -S 180"
;;
polyml-4.1.[34] | polyml-4.2.*)
DISCGARB_OPTIONS="$DISCGARB_OPTIONS -S max"
;;
esac
else
COPYDB=true
fi
if [ -z "$OUTFILE" ]; then
DB="$INFILE"
ML_OPTIONS="-r $ML_OPTIONS"
elif [ "$INFILE" -ef "$OUTFILE" ]; then
DB="$INFILE"
elif [ -n "$COPYDB" ]; then
[ -f "$OUTFILE" ] && { rm -f "$OUTFILE" || fail_out; }
cp "$INFILE" "$OUTFILE" || fail_out
chmod +w "$OUTFILE" || fail_out
DB="$OUTFILE"
else
[ -f "$OUTFILE" ] && { rm -f "$OUTFILE" || fail_out; }
echo "PolyML.make_database \"$OUTFILE\"; PolyML.quit();" | "$POLY" -r "$INFILE"
[ -f "$OUTFILE" ] || fail_out
DB="$OUTFILE"
fi
## run it!
case "$ML_SYSTEM" in
polyml-4.1.[12])
;;
polyml-4.1.[34] | polyml-4.2.*)
MLTEXT="PolyML.Compiler.printInAlphabeticalOrder := false; $MLTEXT"
;;
esac
if [ -z "$TERMINATE" ]; then
FEEDER_OPTS=""
else
FEEDER_OPTS="-q"
fi
DB_INFO="$(ls -l "$DB" 2>/dev/null)"
"$ISABELLE_HOME/lib/scripts/feeder" -p -h "$MLTEXT" $FEEDER_OPTS | {
read FPID; "$POLY" $ML_OPTIONS "$DB";
RC="$?"; kill -HUP "$FPID"; exit "$RC"; }
RC="$?"
NEW_DB_INFO="$(ls -l "$DB" 2>/dev/null)"
[ -n "$OUTFILE" -a -n "$COMPRESS" -a "$DB_INFO" != "$NEW_DB_INFO" ] && \
"$POLY" $DISCGARB_OPTIONS "$OUTFILE"
[ -n "$OUTFILE" -a -f "$OUTFILE" -a -n "$NOWRITE" ] && chmod -w "$OUTFILE"
exit "$RC"