#!/usr/bin/env bash
# Exec a vc-frame pane Python script with the Runtime Pack interpreter.
# Host `env python3` (Homebrew) does not import vibecrafted_core.
set -euo pipefail

launcher="${1:-}"
if [[ $# -gt 0 ]]; then
  shift
fi

if [[ -z "$launcher" || ! -x "$launcher" ]]; then
  printf '\n  Pane launcher missing: %s\n  Repair: run make install from the Vibecrafted checkout with your verified Runtime Pack\n\n' "${launcher:-<none>}" >&2
  exec "${SHELL:-/bin/zsh}" -l
fi

# The pane runs the runtime's pinned interpreter by absolute path: the pin the
# Frame server inherited from its entry, else the selected generation's own
# bin/python3. Never the uv-tool venv, the active-generation pointer or a PATH
# python: a pane that quietly ran another interpreter would hide the broken
# pin until the first import that needs 3.11+.
candidates=(
  "${VIBECRAFTED_PYTHON:-}"
  "${VIBECRAFTED_RUNTIME_ROOT:+${VIBECRAFTED_RUNTIME_ROOT}/bin/python3}"
  "${VIBECRAFTED_ROOT:+${VIBECRAFTED_ROOT}/bin/python3}"
)
for py in "${candidates[@]}"; do
  [[ "$py" == /* && -x "$py" && ! -d "$py" ]] || continue
  # Product config is republished on upgrade while an existing Frame retains
  # its interpreter pin. Pair managed Python panes with that interpreter's
  # own helper, not the newer shared script. Explicit/private and in-tree
  # launchers keep their requested path.
  # Classify the requested path, not a symlink target: an explicit checkout
  # helper stays explicit even if the shared config directory points there.
  launcher_dir="$(cd -L "$(dirname "$launcher")" && pwd -L)"
  managed=false
  for config_home in "$HOME/.config" "${XDG_CONFIG_HOME:-$HOME/.config}"; do
    shared="$config_home/vibecrafted/vc-frame"
    [[ -d "$shared" ]] || continue
    if [[ "$launcher_dir" == "$(cd -L "$shared" && pwd -L)" ]]; then
      # Only shipped Python pane entries are ours. Other files in this
      # directory remain Founder-owned, as in the installer's managed leaves.
      case "${launcher##*/}" in
        vc-agent-workshop.py|vc-start-here.py)
          managed=true
          break
          ;;
      esac
    fi
  done
  if [[ "$managed" == true ]]; then
    # Resolve the bin directory, not the executable: generation venvs may
    # symlink CPython outside the generation. No active/PATH/env-root fallback.
    bin_dir="$(cd "$(dirname "$py")" && pwd -P)"
    generation="$(cd "$bin_dir/.." && pwd -P)"
    helpers="$generation/vibecrafted-core/vibecrafted_core/config/vc-frame"
    counterpart="$helpers/$(basename "$launcher")"
    if [[ "$bin_dir" != "$generation/bin" || ! -d "$helpers" ||
          "$(cd "$helpers" && pwd -P)" != "$helpers" ||
          ! -f "$counterpart" || -L "$counterpart" || ! -r "$counterpart" ]]; then
      printf '\n  Pane helper unavailable in pinned generation: %s\n  Repair the pinned Runtime Pack; this Frame keeps its current runtime.\n\n' "$counterpart" >&2
      exit 78
    fi
    launcher="$counterpart"
  fi
  # This wrapper intentionally becomes the selected interpreter.
  # shellcheck disable=SC2093
  exec "$py" "$launcher" "$@"
done
printf '\n  No runtime interpreter pinned for this pane (VIBECRAFTED_PYTHON / VIBECRAFTED_RUNTIME_ROOT unset or not executable).\n  Repair: reopen the workspace from Vibecrafted (vc-start) so the Frame server carries its runtime pin.\n\n' >&2
exec "${SHELL:-/bin/zsh}" -l
