#!/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_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 "$@"
