#!/usr/bin/env bash
#
# $Id$
# Author: Markus Wenzel, TU Muenchen
# License: GPL (GNU GENERAL PUBLIC LICENSE)
#
# 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
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;"
;;
*)
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
check_file "$ML_DBASE_PREFIX$ML_DBASE"
INFILE="$ML_DBASE"
MLTEXT="val use = PolyML.use; $EXIT $MLTEXT"
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!
if [ -z "$TERMINATE" ]; then
FEEDER_OPTS="-s"
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" ] && \
"$DISCGARB" $DISCGARB_OPTIONS "$OUTFILE"
[ -n "$OUTFILE" -a -f "$OUTFILE" -a -n "$NOWRITE" ] && chmod -w "$OUTFILE"
exit "$RC"