#!/bin/bash
# TeleClaude terminal CLI wrapper.
# Resolvable from any location: follows symlinks back to this file, computes
# the repo root, and invokes the telec module via `uv run` so uv manages the
# project environment from pyproject.toml. No .venv/bin/telec dependency.
set -euo pipefail

SOURCE="$0"
while [ -L "$SOURCE" ]; do
  DIR="$(cd "$(dirname "$SOURCE")" && pwd)"
  TARGET="$(readlink "$SOURCE")"
  if [[ "$TARGET" == /* ]]; then
    SOURCE="$TARGET"
  else
    SOURCE="$DIR/$TARGET"
  fi
done

INSTALL_DIR="$(cd "$(dirname "$SOURCE")/.." && pwd)"
# Preserve the user's invocation directory. `uv run --directory` below sets the
# Python process cwd to INSTALL_DIR, which breaks worktree-scope detection in
# telec code lint/test (`Path.cwd()` would point at the main repo, not the worktree
# the user invoked from). Handlers read this env var to resolve the real cwd.
export TELEC_INVOCATION_CWD="$PWD"
# Block `uv run`'s implicit venv sync. Ambient sync while TeleClaude's daemon
# holds modules loaded causes stale-module ImportError crashes. Explicit
# `uv sync` (install.sh, dependency_upgrade job) still works.
export UV_NO_SYNC=1
exec uv run --directory "$INSTALL_DIR" --quiet python -m teleclaude.cli.telec "$@"
