#!/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 programs
FEEDER=""
ML_DBASE_PREFIX=""
POLY="$ML_HOME/poly"
check_file "$POLY"
case "$ML_SYSTEM" in
polyml-4.*)
if [ -z "$ML_DBASE" ]; then
if [ -z "$COPYDB" ]; then
ML_DBASE_PREFIX="$ML_HOME/"
ML_DBASE="ML_dbase"
else
ML_DBASE="$ML_HOME/ML_dbase"
fi
POLYPATH="$ML_HOME"
else
POLYPATH=$(dirname "$ML_DBASE")
fi
export POLYPATH
DISCGARB="$POLY"
DISCGARB_OPTIONS="-d -c"
EXIT="fun exit 0 = (OS.Process.exit OS.Process.success): unit | exit _ = OS.Process.exit OS.Process.failure;"
;;
*)
FEEDER=true
if [ -z "$ML_DBASE" ]; then
ML_DBASE="$ML_HOME/ML_dbase"
fi
DISCGARB="$ML_HOME/discgarb"
DISCGARB_OPTIONS="-c"
check_file "$DISCGARB"
EXIT="val exit = PolyML.exit;"
;;
esac
## prepare databases
if [ -z "$INFILE" ]; then
FEEDER=true
check_file "$ML_DBASE_PREFIX$ML_DBASE"
INFILE="$ML_DBASE"
MLTEXT="val use = PolyML.use; $EXIT $MLTEXT"
[ "$ML_SYSTEM" = polyml-4.1.3 ] && \
DISCGARB_OPTIONS="$DISCGARB_OPTIONS -S max"
[ "$ML_SYSTEM" = polyml-4.1.1 -o "$ML_SYSTEM" = polyml-4.1.2 ] && \
DISCGARB_OPTIONS="$DISCGARB_OPTIONS -S 180"
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!
DB_INFO=$(ls -l "$DB" 2>/dev/null)
if [ -n "$TERMINATE" ]; then
echo "$MLTEXT" | "$POLY" $ML_OPTIONS "$DB"
elif [ -n "$FEEDER" ]; then
"$ISABELLE_HOME/lib/scripts/feeder" -p -h "$MLTEXT" | \
{ read FPID; "$POLY" $ML_OPTIONS "$DB"; RC="$?"; kill -HUP "$FPID"; exit "$RC"; }
else
"$POLY" $ML_OPTIONS "$DB" -- "$MLTEXT"
fi
RC="$?"
NEW_DB_INFO=$(ls -l "$DB" 2>/dev/null)
[ -n "$OUTFILE" -a -n "$COMPRESS" -a "$DB_INFO" != "$NEW_DB_INFO" ] && \
"$DISCGARB" $DISCGARB_OPTIONS "$OUTFILE"
[ -n "$OUTFILE" -a -f "$OUTFILE" -a -n "$NOWRITE" ] && chmod -w "$OUTFILE"
exit "$RC"