#!/bin/sh
# Synced from testrelic-ai/testrelic-cli (install.sh) - edit there, not here.
# TestRelic CLI installer (Linux / macOS).
#   curl -fsSL https://testrelic.ai/install.sh | sh
#
# Environment:
#   TESTRELIC_VERSION      pin a release tag, e.g. v0.3.0 (default: latest)
#   TESTRELIC_INSTALL_DIR  install location (default: $HOME/.testrelic/bin)
#   TESTRELIC_DL_BASE      download host (default: the TestRelic releases bucket)
#   TESTRELIC_DRY_RUN=1    print what would happen and exit
#
# Flags: --dry-run
set -eu

REPO="testrelic-ai/testrelic-cli"
DL_BASE="${TESTRELIC_DL_BASE:-https://testrelic-downloads.s3.us-east-1.amazonaws.com/cli}"
INSTALL_DIR="${TESTRELIC_INSTALL_DIR:-$HOME/.testrelic/bin}"
DRY_RUN=0
[ "${TESTRELIC_DRY_RUN:-0}" = "1" ] && DRY_RUN=1
for a in "$@"; do
  case "$a" in
  --dry-run) DRY_RUN=1 ;;
  *) ;;
  esac
done

say() { printf '%s\n' "$*"; }
err() {
  printf 'error: %s\n' "$1" >&2
  exit 1
}
have() { command -v "$1" >/dev/null 2>&1; }

fetch() { # fetch <url> -> stdout
  if have curl; then
    curl -fsSL -H 'User-Agent: testrelic-installer' "$1"
  elif have wget; then
    wget -qO- "$1"
  else
    err "need 'curl' or 'wget' to download"
  fi
}
download() { # download <url> <out>
  if have curl; then
    curl -fsSL -H 'User-Agent: testrelic-installer' -o "$2" "$1"
  elif have wget; then
    wget -qO "$2" "$1"
  else
    err "need 'curl' or 'wget' to download"
  fi
}

# --- detect platform ---
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux) plat_os="linux" ;;
Darwin) plat_os="macos" ;;
MINGW* | MSYS* | CYGWIN* | Windows_NT)
  err "Windows detected — use the PowerShell installer instead:
  irm https://testrelic.ai/install.ps1 | iex"
  ;;
*) err "unsupported OS '$os'. Build from source:
  cargo install --git https://github.com/$REPO trc-cli" ;;
esac
case "$arch" in
x86_64 | amd64) plat_arch="x86_64" ;;
arm64 | aarch64) plat_arch="arm64" ;;
*) err "unsupported architecture '$arch'. Build from source:
  cargo install --git https://github.com/$REPO trc-cli" ;;
esac
artifact="${plat_os}-${plat_arch}"

# --- resolve version ---
version="${TESTRELIC_VERSION:-}"
if [ -z "$version" ]; then
  version="$(fetch "$DL_BASE/stable" | tr -d '[:space:]')"
  [ -n "$version" ] || err "could not fetch the latest version from $DL_BASE/stable; set TESTRELIC_VERSION"
fi
case "$version" in
v*) ;;
*) version="v$version" ;;
esac

asset="testrelic-cli-${version}-${artifact}.tar.gz"
url="$DL_BASE/${version}/${asset}"

if [ "$DRY_RUN" = "1" ]; then
  say "platform : $os/$arch -> $artifact"
  say "version  : $version"
  say "asset    : $asset"
  say "url      : $url"
  say "install  : $INSTALL_DIR"
  exit 0
fi

# --- download + install ---
say ""
say "  Installing TestRelic CLI $version ($artifact)"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT INT TERM
download "$url" "$tmp/pkg.tar.gz" || err "download failed: $url"
tar -C "$tmp" -xzf "$tmp/pkg.tar.gz" || err "could not extract the archive"
mkdir -p "$INSTALL_DIR"
for bin in testrelic tr ask-ai; do
  [ -f "$tmp/$bin" ] || err "archive is missing '$bin'"
  cp "$tmp/$bin" "$INSTALL_DIR/$bin"
  chmod 0755 "$INSTALL_DIR/$bin"
done

# --- PATH ---
added_path=0
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*)
  shell_name="$(basename "${SHELL:-sh}")"
  case "$shell_name" in
  zsh) rc="$HOME/.zshrc" ;;
  bash) rc="$HOME/.bashrc" ;;
  *) rc="$HOME/.profile" ;;
  esac
  if ! { [ -f "$rc" ] && grep -qF "$INSTALL_DIR" "$rc"; }; then
    printf '\n# Added by TestRelic CLI installer\nexport PATH="%s:$PATH"\n' "$INSTALL_DIR" >>"$rc"
    say "  Added $INSTALL_DIR to PATH in $rc"
  fi
  added_path=1
  ;;
esac

ver_out="$("$INSTALL_DIR/testrelic" --version 2>/dev/null || true)"
say ""
say "  Installed ${ver_out:-testrelic} -> $INSTALL_DIR"
if [ "$added_path" = "1" ]; then
  say "  Restart your shell, or run: export PATH=\"$INSTALL_DIR:\$PATH\""
fi
say "  Note: the 'tr' shortcut shadows coreutils 'tr' when $INSTALL_DIR is first on PATH — use 'testrelic' if that matters."
say ""
say "  Get started:  testrelic"
