#!/usr/bin/env bash
# Wrapper script for launching the Navi Code terminal client from anywhere.
#
# Usage:
#   1. Clone the navi repository.
#   2. Create a venv and install the package:
#        python -m venv .venv
#        .venv/bin/pip install -e ".[dev]"
#   3. Symlink this script onto your PATH:
#        ln -s /path/to/navi/bin/navi-code ~/.local/bin/navi-code
#   4. cd into any project and run:
#        navi-code
#
# The wrapper locates the project root from the script's own location (resolving
# symlinks so it works when called via a PATH symlink), runs the client with the
# repo's venv python, and puts the repo root on PYTHONPATH so the `clients`
# package is importable from any cwd WITHOUT cd-ing (cd would change the cwd the
# client captures and sends to the backend, breaking per-project path resolution).

# Resolve symlinks so the wrapper finds PROJECT_ROOT correctly even when
# invoked via a symlink on PATH (e.g. ~/.local/bin/navi-code -> .../bin/navi-code).
SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
VENV_DIR="${NAVI_CODE_VENV:-${PROJECT_ROOT}/.venv}"

if [[ -f "${VENV_DIR}/bin/python" ]]; then
    export PYTHONPATH="${PROJECT_ROOT}${PYTHONPATH:+:${PYTHONPATH}}"
    exec "${VENV_DIR}/bin/python" -m clients.terminal.cli "$@"
fi

# Fallback: run the entry point directly if the package is installed elsewhere.
exec python -m clients.terminal.cli "$@"