#!/usr/bin/env bash
# grove-seat — the desk's command-line seat: the one script at a terminal.
# b17: GRSET · ΔΣ=42
#
#   scripts/grove-seat        # check-in, scope, served, chain, proposals, check-out
#   scripts/grove-seat --check    # probe only (bot root, python, ratatosk, Ollama); writes nothing
#   scripts/grove-seat --help | --version
#   scripts/grove-seat --no-say | --model NAME   # code only | this run's reply model
#
# The third front end beside web/ and the phone APK; all three call willow-bot's
# one-script api. Portless: a terminal process, no listener. See grove/seat_cli.py.
#
#   WILLOW_BOT_ROOT   willow-bot checkout (default: ../willow-bot beside this repo)
#   GROVE_VENV        python venv dir; wins over every default below. Else
#                     $WILLOW_HOME/willow-bot/venvs/willow-bot (seal D2: the
#                     bot's venvs live in the bot; ships willow_mcp, onescript
#                     and ratatosk), else the legacy $WILLOW_HOME/venvs/willow-bot
#                     (used with a stderr warning until it is recreated in the
#                     bot), else ./.venv, else python3
#   WILLOW_HOME       finds the venvs and the operator's public keyring
#   WILLOW_NESTOR_DB  Nestor's store, read-only: where a seal is looked up
#   GROVE_SEAT_BOX    point the seat at another box, a table's (SPEC R13): its
#                     NEEDS YOU then carries the wagers, the verdict sessions and
#                     the agm_head check, read through api.needs / api.checkout
#   GROVE_SEAT_KEYS   that box's keys file (default: the seat's own)
#   GROVE_SEAT_TABLE_BOX  a SECOND box the seat only watches (the seat's own box
#                     stays its own): read with api.peek, which writes nothing,
#                     so the tip you are about to sign never moves; keys from
#                     GROVE_SEAT_TABLE_KEYS (default: the seat's own)
#   GROVE_SEAT_DRAND  set to read drand rounds (the one script's --drand); unset,
#                     the seat says "beacon reader off: table outcomes unreachable"
#   GROVE_SEAT_HUMAN_KEY  the human's key, a 0600 file outside the box: a sealed
#                     tip is checked and scores show; never printed
#   GROVE_SEAT_LEDGER the ai-game-master campaign.db, for the agm_head check
#                     (THE_TABLE_ROOT finds the_table.agm_head)
#   GROVE_SEAT_GRAPH  a cbm project: a qualified file.py:name cite in a model
#                     sentence is read against its code graph (stale or
#                     unreachable is "unchecked", said; never re-indexes)
#   GROVE_SAY_MODEL   the reply model (default qwen3:4b); --model NAME wins over it
#   OLLAMA_HOST       the local model's Ollama, loopback only (default 127.0.0.1:11434);
#                     it says each reply's sentence (qwen3:4b) and code checks it
#
# A flowering turn runs `ratatosk --onescript --class flowering`; Rat's ladder
# picks the provider and reads its keys from this environment.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"

PY=""
NEW_VENV="${WILLOW_HOME:-}/willow-bot/venvs/willow-bot"
OLD_VENV="${WILLOW_HOME:-}/venvs/willow-bot"
if [[ -n "${GROVE_VENV:-}" && -x "${GROVE_VENV}/bin/python3" ]]; then
  PY="${GROVE_VENV}/bin/python3"
elif [[ -n "${WILLOW_HOME:-}" && -x "${NEW_VENV}/bin/python3" ]]; then
  PY="${NEW_VENV}/bin/python3"
elif [[ -n "${WILLOW_HOME:-}" && -x "${OLD_VENV}/bin/python3" ]]; then
  PY="${OLD_VENV}/bin/python3"
  echo "grove-seat: warning: using the old venv ${OLD_VENV}, outside the bot (D2); recreate it at ${NEW_VENV}." >&2
elif [[ -x "${ROOT}/.venv/bin/python3" ]]; then
  PY="${ROOT}/.venv/bin/python3"
else
  PY="$(command -v python3)"
fi

export PYTHONPATH="${ROOT}${PYTHONPATH:+:$PYTHONPATH}"
exec "$PY" -m grove.seat_cli "$@"
