#!/usr/bin/env bash
#
# Author: Makarius
#
# mk - build graph browser
#
# Requires proper Isabelle settings environment.


## diagnostics

function fail()
{
  echo "$1" >&2
  exit 2
}

[ -n "$ISABELLE_HOME" ] || fail "Missing Isabelle settings environment"


## dependencies

declare -a SOURCES=(
  GraphBrowser/AWTFontMetrics.java
  GraphBrowser/AbstractFontMetrics.java
  GraphBrowser/Box.java
  GraphBrowser/Console.java
  GraphBrowser/DefaultFontMetrics.java
  GraphBrowser/Directory.java
  GraphBrowser/DummyVertex.java
  GraphBrowser/Graph.java
  GraphBrowser/GraphBrowser.java
  GraphBrowser/GraphBrowserFrame.java
  GraphBrowser/GraphView.java
  GraphBrowser/NormalVertex.java
  GraphBrowser/ParseError.java
  GraphBrowser/Region.java
  GraphBrowser/Spline.java
  GraphBrowser/TreeBrowser.java
  GraphBrowser/TreeNode.java
  GraphBrowser/Vertex.java
  awtUtilities/Border.java
  awtUtilities/MessageDialog.java
  awtUtilities/TextFrame.java
)

TARGET="$ISABELLE_HOME/lib/browser/GraphBrowser.jar"


## main

OUTDATED=false

for SOURCE in "${SOURCES[@]}"
do
  [ ! -e "$SOURCE" ] && fail "Missing source file: $SOURCE"
  [ ! -e "$TARGET" -o "$SOURCE" -nt "$TARGET" ] && OUTDATED=true
done

if [ "$OUTDATED" = true ]
then
  echo "### Building graph browser ..."

  rm -rf classes && mkdir classes

  isabelle_jdk javac -d classes -source 1.4 "${SOURCES[@]}" || \
    fail "Failed to compile sources"
  isabelle_jdk jar cf "$(jvmpath "$TARGET")" -C classes . ||
    fail "Failed to produce $TARGET"

  rm -rf classes
fi
