#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# quill — start Gnasher's Quill on macOS or Linux.
#
#   ./quill              open the web studio in your browser
#   ./quill doctor       is this machine ready to record?
#   ./quill probe        what audio hardware did it find?
#   ./quill mine         digest the newest transcript
#   ./quill reports      rebuild LEDGER / CONCORDANCE / MIRROR
#   ./quill chorus       merge a Chorus session (one track per voice)
#   ./quill coldopen     the sheet you read before the players arrive
#   ./quill lexicon      build lexicon.txt from your campaign notes
#
# On macOS you can also double-click `quill.command`.
#
# The web studio IS the cross-platform face of the Quill. The fancy terminal studio
# (Quill-Studio.ps1) is PowerShell and Windows-only; everything it does, the browser
# page and these subcommands do too.

set -u
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$HERE" || exit 1

# Find a Python that actually RUNS.
#
# `command -v python3` is not good enough. Windows ships an "App execution alias"
# stub at that name which resolves fine and then prints "Python was not found; run
# without arguments to install from the Microsoft Store" instead of doing anything —
# so the check passes and every subcommand silently fails. (Transcribe-Session.ps1
# guards against the same stub on the PowerShell side.) Execute a trivial program
# and believe only a working exit code.
works() { [ -n "${1:-}" ] && "$1" -c 'import sys; sys.exit(0)' >/dev/null 2>&1; }

PY=""
for c in "$HERE/.venv/bin/python" "$HERE/.venv/Scripts/python.exe" \
         python3.12 python3.11 python3 python; do
  if works "$c"; then PY="$c"; break; fi
done
if [ -z "$PY" ]; then
  echo "No working Python found. Run ./setup-quill.sh first."
  echo "(If 'python3' exists but does nothing, it is the Microsoft Store stub —"
  echo " install real Python from python.org, or disable the alias in Settings.)"
  exit 1
fi

cmd="${1:-studio}"
shift 2>/dev/null || true

case "$cmd" in
  studio|"")  exec "$PY" quill_server.py ;;
  probe)      exec "$PY" quill_platform.py ;;
  mine)       exec "$PY" Mine-Transcript.py "${@:-transcripts}" ;;
  ledger)     exec "$PY" Quill-Ledger.py "$@" ;;
  concordance|find)
              exec "$PY" Quill-Concordance.py "$@" ;;
  mirror)     exec "$PY" Quill-Mirror.py "${@:-transcripts}" ;;
  lexicon)    exec "$PY" Quill-Lexicon.py "$@" ;;
  chorus)     exec "$PY" Quill-Chorus.py "$@" ;;
  coldopen|open)
              exec "$PY" Quill-ColdOpen.py "$@" ;;
  transcribe) exec "$PY" transcribe.py "$@" ;;
  reports)
      echo "Rebuilding the campaign reports..."
      "$PY" Quill-Ledger.py       || exit 1
      "$PY" Quill-Concordance.py  || exit 1
      "$PY" Quill-Mirror.py transcripts --out MIRROR.md || exit 1
      "$PY" Quill-ColdOpen.py || exit 1
      echo "-> LEDGER.md  CONCORDANCE.md  MIRROR.md  COLD-OPEN.md"
      ;;
  doctor)
      # The full Doctor is PowerShell. If pwsh is here, use it; otherwise the probe
      # covers the parts that actually stop a session from happening.
      if command -v pwsh >/dev/null 2>&1; then exec pwsh -File "$HERE/Quill-Doctor.ps1"
      else
        echo "(PowerShell not installed - running the platform probe instead)"
        exec "$PY" quill_platform.py
      fi ;;
  help|-h|--help)
      sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//' ;;
  *)  echo "unknown command: $cmd"; echo "try: ./quill help"; exit 2 ;;
esac
