#!/bin/zsh
# Loader for this project. Points at the local uml-viewer checkout so the
# Python scanner is on the classpath.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT"

DEPS='{:deps {uml-viewer/uml-viewer {:local/root "/Users/unclebob/projects/uml-viewer"} quil/quil {:mvn/version "4.3.1563"}}}'

edn_path() {
  local name base f
  name="$(basename "$ROOT")"
  if [[ -f "examples/${name}.edn" ]]; then
    echo "examples/${name}.edn"
    return
  fi
  if [[ -d examples ]]; then
    for f in examples/*.edn(N); do
      [[ -f "$f" ]] || continue
      base="${f:t}"
      [[ "$base" == *.policy.edn ]] && continue
      echo "$f"
      return
    done
  fi
}

LOG="uml-viewer-log.txt"

launch_viewer() {
  {
    printf '----- %s starting uml-viewer' "$(date '+%Y-%m-%d %H:%M:%S')"
    [[ $# -gt 0 ]] && printf ' %s' "$@"
    printf '\n'
  } >> "$LOG"
  # `clj` is rlwrap on Homebrew; a background rlwrap stops (SIGTTOU) and
  # never starts the JVM. `clojure` is the same CLI without the tty wrapper.
  nohup clojure -Sdeps "$DEPS" -M -m uml-viewer.main.uml-viewer "$@" >> "$LOG" 2>&1 </dev/null &
  disown
  echo "UML viewer started (pid $!). Log: $LOG"
}

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
  echo "usage: ./uml [--restart]" >&2
  echo "  (no args)   fresh window; companion writes the diagram" >&2
  echo "  --restart   new JVM, same companion; restores last view" >&2
  echo "  The viewer detaches; exceptions go to $LOG." >&2
  exit 0
fi

if [[ "${1:-}" == "--restart" ]]; then
  edn="$(edn_path)"
  if [[ -n "$edn" ]]; then
    launch_viewer --restart "$edn"
  else
    launch_viewer --restart
  fi
  exit 0
fi

launch_viewer "$@"
