#!/bin/sh
# Halton Meter public installer — "bootstrap now, binaries later".
#
#   curl -fsSL https://get.haltonmeter.com/install.sh | sh
#   curl -fsSL https://get.haltonmeter.com/install.sh | sh -s -- --version 0.5.0
#
# This is the PUBLIC install path. It does NOT download a prebuilt binary and
# does NOT verify a SHA-256 (that is the enterprise installer, packaging/
# enterprise/install.sh). Instead it:
#   1. installs uv (https://astral.sh/uv) if it is missing,
#   2. `uv tool install halton-meter` from PyPI (isolated, brings its own
#      Python interpreter — no system Python required),
#   3. runs `halton-meter init` (unless --skip-init).
#
# Zero workflow disruption: this only installs + configures the meter. The
# meter's edge fails open, so traffic always flows through to the upstream
# provider even if the daemon is off. Nothing here breaks your API calls.
#
# POSIX sh (not bash) — piped installers run under whatever /bin/sh is.
# macOS + Linux. Windows: use the PowerShell one-liner (see usage()).
set -eu

# The user's ORIGINAL PATH, captured before this script prepends uv's bin
# dirs. This is what their NEXT shell resolves against — the PATH-shadow
# check at the end compares resolution under it vs the binary we installed.
ORIG_PATH="$PATH"

# --- defaults / env overrides ----------------------------------------------
# Env overrides let CI / dev drive the script without editing argv (a piped
# `curl ... | sh` cannot take positional flags cleanly).
SKIP_UV="${HALTON_METER_SKIP_UV:-0}"
SKIP_INIT="${HALTON_METER_SKIP_INIT:-0}"
VERSION="${HALTON_METER_VERSION:-}"
PACKAGE_SPEC="${HALTON_METER_PACKAGE_SPEC:-halton-meter}"
INDEX_URL="${HALTON_METER_INDEX_URL:-}"
INIT_MODE=""

usage() {
    echo "usage: install.sh [--version X.Y.Z] [--skip-init] [--apps|--full]"
    echo ""
    echo "flags:"
    echo "  --version X.Y.Z  pin the halton-meter release to install"
    echo "  --skip-init      install the CLI but do not run 'halton-meter init'"
    echo "  --apps           pass --apps to 'halton-meter init' (metered apps only)"
    echo "  --full           pass --full to 'halton-meter init' (adds browser HTTPS)"
    echo "  -h, --help       show this help and exit"
    echo ""
    echo "env overrides (equivalents for piped 'curl ... | sh' use):"
    echo "  HALTON_METER_VERSION=X.Y.Z     same as --version"
    echo "  HALTON_METER_SKIP_INIT=1       same as --skip-init"
    echo "  HALTON_METER_SKIP_UV=1         assume uv is present; never install it"
    echo "  HALTON_METER_PACKAGE_SPEC=...  override the uv install target"
    echo "                                 (e.g. a local .whl path or 'halton-meter==X.Y.Z')"
    echo "  HALTON_METER_INDEX_URL=...     install from this index instead of PyPI"
    echo "  HALTON_METER_KEEP_PROXY=1      keep proxy env vars (default drops loopback-only)"
}

# --- argument parsing -------------------------------------------------------
while [ $# -gt 0 ]; do
    case "$1" in
        --version)   VERSION="${2:?--version needs a value}"; shift 2 ;;
        --skip-init) SKIP_INIT=1; shift ;;
        --apps)      INIT_MODE=apps; shift ;;
        --full)      INIT_MODE=full; shift ;;
        -h|--help)   usage; exit 0 ;;
        *) echo "error: unknown argument: $1" >&2; usage >&2; exit 2 ;;
    esac
done

# A bare version pin with the default package name becomes a versioned spec.
if [ -n "$VERSION" ] && [ "$PACKAGE_SPEC" = "halton-meter" ]; then
    PACKAGE_SPEC="halton-meter==${VERSION}"
fi

# --- OS / arch detect -------------------------------------------------------
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
    Darwin) OS=macos ;;
    Linux)  OS=linux ;;
    *)
        echo "error: unsupported platform: $OS" >&2
        echo "Windows: use the PowerShell one-liner:" >&2
        echo "    irm https://get.haltonmeter.com/install.ps1 | iex" >&2
        echo "Anything else: contact support@haltonmeter.com" >&2
        exit 1
        ;;
esac
# uv provisions its own interpreter, so we do not hard-reject on arch — just
# echo it for diagnostics if an install ever misbehaves.
echo "==> halton-meter public installer (${OS}, arch ${ARCH})"

# --- helpers ----------------------------------------------------------------
# Prepend a directory to PATH for THIS script's scope if it exists and is not
# already present. Used to make a freshly-installed uv (and the uv tool bin
# dir) visible to the rest of the script without a new shell.
ensure_on_path() {
    _dir="$1"
    [ -n "$_dir" ] || return 0
    [ -d "$_dir" ] || return 0
    case ":${PATH}:" in
        *":${_dir}:"*) : ;;
        *) PATH="${_dir}:${PATH}"; export PATH ;;
    esac
}

# Physical path of a file: resolve its DIRECTORY with `pwd -P` (POSIX —
# `readlink -f` is not portable to macOS's BSD readlink) and keep the
# basename. Normalises symlinked directories so the PATH-shadow check
# compares real locations, not spellings. Falls back to the input on any
# resolution failure.
phys() {
    _pp="$1"
    _pd=$(dirname "$_pp")
    _pb=$(basename "$_pp")
    if _pr=$(cd "$_pd" 2>/dev/null && pwd -P); then
        printf '%s/%s\n' "$_pr" "$_pb"
    else
        printf '%s\n' "$_pp"
    fi
}

# --- stale-CA / stale-proxy pre-emption -------------------------------------
# A CA env var left over from a previous certifi bundle (since deleted) makes
# uv/curl abort with "Could not find a suitable TLS CA certificate bundle".
# A half-configured meter edge that is currently down would break our own
# curl/uv if we honoured its proxy vars. Neutralise both FOR THIS SCRIPT ONLY;
# we never touch the user's shell. See INSTALL.md "Install / upgrade gotchas".
UNSET_LIST=""
_val=""
_pval=""
_dropped_proxy=""
for _name in CURL_CA_BUNDLE SSL_CERT_FILE REQUESTS_CA_BUNDLE \
             NODE_EXTRA_CA_CERTS GIT_SSL_CAINFO AWS_CA_BUNDLE; do
    eval "_val=\${$_name:-}"
    if [ -n "$_val" ] && [ ! -e "$_val" ]; then
        unset "$_name"
        UNSET_LIST="${UNSET_LIST} ${_name}"
    fi
done
# Only drop proxy vars that point at a LOOPBACK host (the meter's own edge,
# which may be half-configured / down). A remote/corporate proxy is preserved —
# we may need it to reach PyPI. Override entirely with HALTON_METER_KEEP_PROXY=1.
if [ "${HALTON_METER_KEEP_PROXY:-0}" != 1 ]; then
    _dropped_proxy=""
    for _pv in HTTPS_PROXY HTTP_PROXY ALL_PROXY https_proxy http_proxy all_proxy; do
        eval "_pval=\${$_pv:-}"
        case "$_pval" in
            *localhost*|*127.0.0.1*|*"[::1]"*)
                unset "$_pv" 2>/dev/null || true
                _dropped_proxy="${_dropped_proxy} ${_pv}"
                ;;
        esac
    done
    if [ -n "$_dropped_proxy" ]; then
        echo "==> dropped loopback proxy var(s) for this install:${_dropped_proxy}"
        echo "    (they point at the meter's own edge, which may be down mid-install;"
        echo "     your API traffic is unaffected). Keep them with HALTON_METER_KEEP_PROXY=1."
    fi
fi

if [ -n "$UNSET_LIST" ]; then
    echo "==> cleared stale CA env var(s) for this install:${UNSET_LIST}"
    echo "    (they pointed at files that no longer exist)"
    echo "    If a later MANUAL command fails with a TLS CA error, run this in YOUR shell:"
    echo "        unset CURL_CA_BUNDLE SSL_CERT_FILE REQUESTS_CA_BUNDLE NODE_EXTRA_CA_CERTS \\"
    echo "              GIT_SSL_CAINFO AWS_CA_BUNDLE"
fi

# --- ensure uv --------------------------------------------------------------
if [ "$SKIP_UV" = 1 ]; then
    echo "==> HALTON_METER_SKIP_UV=1 set; assuming uv is already present"
elif command -v uv >/dev/null 2>&1; then
    echo "==> uv already installed ($(uv --version 2>/dev/null || echo unknown))"
else
    echo "==> installing uv (astral.sh)"
    curl -LsSf https://astral.sh/uv/install.sh | sh
fi

# Make a just-installed uv visible without a new shell. uv 0.8 installs to
# $HOME/.local/bin by default, honouring XDG_BIN_HOME / CARGO_HOME.
ensure_on_path "${XDG_BIN_HOME:-}"
if [ -n "${CARGO_HOME:-}" ]; then
    ensure_on_path "${CARGO_HOME}/bin"
fi
ensure_on_path "${HOME}/.local/bin"

if ! command -v uv >/dev/null 2>&1; then
    echo "error: uv is not on PATH after install." >&2
    echo "Open a new terminal (so uv's env is loaded) and re-run this installer," >&2
    echo "or install uv manually: https://docs.astral.sh/uv/getting-started/installation/" >&2
    exit 1
fi

# --- install / upgrade halton-meter ----------------------------------------
# uv tool install brings its own Python interpreter, so no system Python is
# required. Always `install --force`: `uv tool upgrade` silently no-ops on a
# pinned receipt (a previous `--version X.Y.Z` install or a local-wheel spec)
# and leaves the machine on stale code while this script reports success
# (2026-07-14 incident). `install --force <spec>` acts on the requested spec
# unconditionally and is an idempotent no-op-shaped reinstall when current.
set -- tool install --force "$PACKAGE_SPEC"
# --index is the current uv 0.8 flag (-i/--index-url is deprecated).
if [ -n "$INDEX_URL" ]; then
    set -- "$@" --index "$INDEX_URL"
fi
echo "==> uv $*"
if ! uv "$@"; then
    echo "error: '${*}' failed. halton-meter was NOT installed." >&2
    echo "Your existing API traffic is unaffected (this only installs software)." >&2
    exit 1
fi

# --- resolve + verify the installed executable ------------------------------
# Verify THE VENV WE JUST ACTED ON — never whatever `command -v` happens to
# find first (a stale pipx shim earlier on PATH must not vouch for this
# install; that's exactly how the 2026-07-14 run reported success against
# old code).
UV_TOOL_BIN="$(uv tool dir --bin 2>/dev/null || true)"
if [ -z "$UV_TOOL_BIN" ]; then
    UV_TOOL_BIN="${HOME}/.local/bin"
fi
ensure_on_path "$UV_TOOL_BIN"

HM_BIN="${UV_TOOL_BIN}/halton-meter"
if [ ! -x "$HM_BIN" ]; then
    echo "error: uv reported success but ${HM_BIN} does not exist." >&2
    echo "Inspect the install with: uv tool list" >&2
    exit 1
fi

# Load-bearing version check: a broken venv (bad wheel, import-time error)
# must fail the install HERE, not on the user's first real command.
if ! VERSION_OUTPUT="$("$HM_BIN" --version 2>&1)"; then
    echo "error: '${HM_BIN} --version' failed — the installed CLI is broken:" >&2
    echo "${VERSION_OUTPUT}" >&2
    exit 1
fi
# `halton-meter, version X.Y.Z` -> `X.Y.Z`.
INSTALLED_VERSION="$(printf '%s\n' "$VERSION_OUTPUT" \
    | sed -n 's/^.*version \([0-9][0-9A-Za-z.+!-]*\).*$/\1/p' | head -n 1)"
echo "==> installed: halton-meter ${INSTALLED_VERSION:-unknown} (${HM_BIN})"

# --- PyPI-latest comparison (fail-open) --------------------------------------
# Only when installing the default spec from the default index — a pinned
# --version or a custom index/spec legitimately diverges from PyPI latest.
# No jq on purpose: piped installers can't assume it. Any curl/parse failure
# skips the comparison silently; this is a warning aid, never a gate.
if [ -z "$VERSION" ] && [ -z "$INDEX_URL" ] && \
   [ "$PACKAGE_SPEC" = "halton-meter" ] && [ -n "$INSTALLED_VERSION" ]; then
    PYPI_LATEST="$(curl -fsSL --max-time 5 https://pypi.org/pypi/halton-meter/json 2>/dev/null \
        | tr ',' '\n' \
        | sed -n 's/^.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*$/\1/p' \
        | head -n 1 || true)"
    if [ -n "$PYPI_LATEST" ] && [ "$PYPI_LATEST" != "$INSTALLED_VERSION" ]; then
        echo ""
        echo "WARNING: installed halton-meter ${INSTALLED_VERSION}, but PyPI latest is ${PYPI_LATEST}."
        echo "         Force a clean reinstall with:"
        echo "             uv tool install --force --reinstall halton-meter"
    fi
fi

# --- PATH-shadow check --------------------------------------------------------
# The user's next shell resolves against ORIG_PATH. If that resolves
# `halton-meter` to a DIFFERENT binary than the one we just installed (e.g.
# an old pipx shim), the shadowing copy keeps winning — and it may be stale.
# Warn loudly; still exit 0 (this install itself succeeded).
SHADOW_BIN="$(env PATH="$ORIG_PATH" sh -c 'command -v halton-meter' 2>/dev/null || true)"
if [ -n "$SHADOW_BIN" ] && [ "$(phys "$SHADOW_BIN")" != "$(phys "$HM_BIN")" ]; then
    echo ""
    echo "WARNING: a different 'halton-meter' shadows this install on your PATH:"
    echo "    your shell resolves:  ${SHADOW_BIN}"
    echo "    this install put:     ${HM_BIN}"
    echo "New shells will keep running the shadowing copy, which may be stale."
    echo "If it is a pipx install, remove it with:"
    echo "    pipx uninstall halton-meter"
    echo "Then open a new terminal and verify: halton-meter --version"
fi

# --- PATH guidance ----------------------------------------------------------
HM_BIN_DIR="$(dirname "$HM_BIN")"
case ":${ORIG_PATH}:" in
    *":${HM_BIN_DIR}:"*) : ;;
    *)
        echo ""
        echo "Add halton-meter to your PATH in new shells:"
        echo "    echo 'export PATH=\"${HM_BIN_DIR}:\$PATH\"' >> ~/.zshrc"
        ;;
esac

# --- init -------------------------------------------------------------------
if [ "$SKIP_INIT" = 1 ]; then
    echo ""
    echo "halton-meter installed (init skipped)."
    if [ -n "$INIT_MODE" ]; then
        echo "Finish setup with: ${HM_BIN} init --${INIT_MODE}"
    else
        echo "Finish setup with: ${HM_BIN} init"
    fi
    exit 0
fi

echo ""
echo "==> running halton-meter init"
echo "    This MAY PROMPT FOR YOUR PASSWORD to trust the inspection certificate."
if [ -n "$INIT_MODE" ]; then
    set -- init "--${INIT_MODE}"
else
    set -- init
fi
# Under `curl | sh` stdin is the pipe, so init's consent prompts (e.g. the
# shell-rc block) would read EOF and silently decline, leaving new terminals
# unmetered. When a controlling terminal exists, let init ask the user there;
# with none (CI, non-interactive), behaviour is unchanged.
if [ ! -t 0 ] && (: </dev/tty) 2>/dev/null; then
    run_init() { "$HM_BIN" "$@" </dev/tty; }
else
    run_init() { "$HM_BIN" "$@"; }
fi
if run_init "$@"; then
    echo ""
    # init printed its own next steps above. Add only what it cannot know:
    # what this install MODE means for capture, and which already-running
    # programs still hold the old environment. Never advise killing the
    # user's tools.
    if [ -n "$INIT_MODE" ]; then
        echo "halton-meter installed and initialised (--${INIT_MODE})."
        echo ""
        echo "Before your next session:"
        echo "  - Quit your terminal app completely and open it again, and do the"
        echo "    same for any Claude Code session or IDE that was already running."
        echo "    They started before this install and still hold the old environment."
        echo "    Until then their traffic goes straight to the provider, unmetered;"
        echo "    it is never blocked."
        echo "  - Then check:  halton-meter status"
        echo "    If it says the meter is stopped:  halton-meter start"
        echo "  - See what was captured:  halton-meter report"
    else
        echo "halton-meter installed and initialised in env-only mode."
        echo ""
        echo "In this mode nothing is captured automatically. Choose one:"
        echo "  - Capture your normal Claude Code / SDK usage with no wrapper:"
        echo "        halton-meter init --apps"
        echo "    (sets proxy + CA environment variables for your user account and"
        echo "     offers to add them to your shell rc; undo with 'halton-meter uninstall')"
        echo "  - Or capture one command at a time:"
        echo "        halton-meter run -- <your command>"
        echo "  - See what was captured:  halton-meter report"
    fi
else
    # Fail open: the CLI IS installed. Do not exit non-zero in a way that
    # reads like the install failed — just tell the user how to finish init.
    echo ""
    echo "halton-meter is installed, but 'halton-meter init' did not complete." >&2
    if [ -n "$INIT_MODE" ]; then
        echo "Re-run it manually: ${HM_BIN} init --${INIT_MODE}" >&2
    else
        echo "Re-run it manually: ${HM_BIN} init" >&2
    fi
fi
