#!/usr/bin/env bash
#
# $Id: run-polyml,v 1.38 2005/08/16 11:42:17 wenzelm Exp $
# 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=""
ML_DBASE_SUFFIX=""

case "$ML_PLATFORM" in
  *-cygwin)
    ML_DBASE_SUFFIX=".pmd"
    POLY="$ML_HOME/PolyML.exe"
    function fixpath () { cygpath -m "$@"; }
    ;;
  *)
    POLY="$ML_HOME/poly"
    function fixpath () { echo -n "$@"; }
    ;;
esac

check_file "$POLY"

if [ -z "$ML_DBASE" ]; then
  if [ ! -e "$ML_HOME/ML_dbase${ML_DBASE_SUFFIX}" -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${ML_DBASE_SUFFIX}"
  else
    ML_DBASE="$ML_DBASE_HOME/ML_dbase${ML_DBASE_SUFFIX}"
  fi
  export POLYPATH="$(fixpath "$ML_DBASE_HOME")"
else
  export POLYPATH="$(fixpath "$(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.*)
      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 \"$(fixpath "$OUTFILE")\"; PolyML.quit();" | "$POLY" -r "$(fixpath "$INFILE")"
  [ -f "$OUTFILE" ] || fail_out
  DB="$OUTFILE"
fi


## run it!

case "$ML_SYSTEM" in
  polyml-4.1.[12])
    ;;
  polyml-4.1.*)
    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 "$(fixpath "$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"
