#!/usr/bin/env bash
# Bootstrap a fresh provider-managed worktree from inside that worktree.
#
# Only gitignored state needs handling here; tracked files come with the checkout.
# swe-mux keeps its runtime state in ~/.mux (not in the repo), so there is no .env
# or config to copy; the cost here is dependency installation.
set -euo pipefail

note() { printf 'worktree-setup: %s\n' "$*" >&2; }

# Share the caches so per-worktree installs are near-instant rather than full downloads.
export UV_CACHE_DIR="${UV_CACHE_DIR:-$HOME/.cache/uv}"
export npm_config_cache="${npm_config_cache:-$HOME/.npm}"

# `--extra voice-local` because the gate is not the same gate without it: the
# real misaki G2P tests in `test_kokoro_tts.py` are `importorskip`-guarded, so
# a bare sync turns them into silent skips. The extra is optional at install
# time for a user who is happy with the browser's speech stack; a worktree that
# runs the verification gate is not that user. `--extra desktop` is deliberately
# NOT here: a worktree never builds or runs the app (CLAUDE.md § Worktrees).
#
# The spaCy model those G2P tests also need is not in that extra and must not be
# (`pyproject.toml`, `voice-local`): it lives in the `g2p-model` dependency
# group, which `dev` includes, and `dev` is uv's default group - so this command
# still installs it and no flag is needed here. That is why it is in `dev` rather
# than only in `package`.
note "installing python deps (uv sync --extra voice-local)"
uv sync --extra voice-local

if [ -f frontend/package-lock.json ]; then
    note "installing frontend deps (npm ci)"
    ( cd frontend && npm ci )
elif [ -f frontend/package.json ]; then
    note "installing frontend deps (npm install)"
    ( cd frontend && npm install )
fi

note "ready"
