#!/bin/sh
# install.sh: install the rippit CLI binary.
#
# This script ONLY installs the binary. It does not modify your project.
# To also instrument your project after install, use:
#
#   curl -fsSL https://cli.rippit.com/instrument | sh
#
# Usage:
#   curl -fsSL https://cli.rippit.com | sh
#   curl -fsSL https://cli.rippit.com | sh -s -- --version v0.2.0
#   curl -fsSL https://cli.rippit.com | sh -s -- --prefix /usr/local
#
# Environment variables:
#   RIPPIT_INSTALL_BASE   override the download host (default: https://cli.rippit.com)
#   RIPPIT_INSTALL_PREFIX override the install prefix (default: $HOME/.local)
#   RIPPIT_VERSION        install a specific tagged version (default: latest)

set -eu

BIN_NAME="rippit"
DEFAULT_BASE="https://cli.rippit.com"
DEFAULT_PREFIX="${HOME}/.local"

BASE="${RIPPIT_INSTALL_BASE:-$DEFAULT_BASE}"
PREFIX="${RIPPIT_INSTALL_PREFIX:-$DEFAULT_PREFIX}"
VERSION="${RIPPIT_VERSION:-latest}"

show_help() {
  # Inlined rather than read from $0 because the canonical invocation
  # is `curl ... | sh -s -- --help`, where $0 is the shell binary
  # (e.g. /bin/sh), not the script.
  cat <<'HELP'
install.sh: install the rippit CLI binary.

This script ONLY installs the binary. It does not modify your project.
To also instrument your project after install, use:

  curl -fsSL https://cli.rippit.com/instrument | sh

Usage:
  curl -fsSL https://cli.rippit.com | sh
  curl -fsSL https://cli.rippit.com | sh -s -- --version v0.2.0
  curl -fsSL https://cli.rippit.com | sh -s -- --prefix /usr/local

Environment variables:
  RIPPIT_INSTALL_BASE   override the download host (default: https://cli.rippit.com)
  RIPPIT_INSTALL_PREFIX override the install prefix (default: $HOME/.local)
  RIPPIT_VERSION        install a specific tagged version (default: latest)
HELP
}

while [ $# -gt 0 ]; do
  case "$1" in
    --version) VERSION="$2"; shift 2 ;;
    --prefix)  PREFIX="$2"; shift 2 ;;
    --base)    BASE="$2"; shift 2 ;;
    -h|--help) show_help; exit 0 ;;
    *) echo "unknown argument: $1" >&2; exit 2 ;;
  esac
done

log()  { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m==>\033[0m %s\n' "$*" >&2; }
die()  { printf '\033[1;31m==>\033[0m %s\n' "$*" >&2; exit 1; }

need() { command -v "$1" >/dev/null 2>&1 || die "missing required tool: $1"; }
need uname
need tar
need mkdir
if command -v curl >/dev/null 2>&1; then
  fetch() { curl -fsSL "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
  fetch() { wget -qO "$2" "$1"; }
else
  die "need curl or wget"
fi

detect_os() {
  case "$(uname -s)" in
    Darwin) echo darwin ;;
    Linux)  echo linux ;;
    *) die "unsupported OS: $(uname -s)" ;;
  esac
}

detect_arch() {
  case "$(uname -m)" in
    x86_64|amd64) echo amd64 ;;
    arm64|aarch64) echo arm64 ;;
    *) die "unsupported architecture: $(uname -m)" ;;
  esac
}

resolve_version() {
  if [ "$VERSION" = "latest" ]; then
    tmp="$(mktemp)"
    fetch "${BASE}/cli/latest" "$tmp" || die "failed to resolve latest version from ${BASE}/cli/latest"
    VERSION="$(tr -d '[:space:]' < "$tmp")"
    rm -f "$tmp"
    [ -n "$VERSION" ] || die "latest version response was empty"
  fi
}

install_binary() {
  os="$(detect_os)"
  arch="$(detect_arch)"
  resolve_version

  tarball="${BIN_NAME}_${VERSION}_${os}_${arch}.tar.gz"
  url="${BASE}/cli/${VERSION}/${tarball}"

  tmpdir="$(mktemp -d)"
  trap 'rm -rf "$tmpdir"' EXIT

  log "downloading ${url}"
  fetch "$url" "${tmpdir}/${tarball}" || die "download failed: ${url}"

  log "extracting"
  tar -xzf "${tmpdir}/${tarball}" -C "$tmpdir"

  bin_src="${tmpdir}/${BIN_NAME}"
  [ -x "$bin_src" ] || die "extracted archive did not contain executable ${BIN_NAME}"

  bin_dest="${PREFIX}/bin"
  mkdir -p "$bin_dest"

  install -m 0755 "$bin_src" "${bin_dest}/${BIN_NAME}" 2>/dev/null \
    || cp "$bin_src" "${bin_dest}/${BIN_NAME}" \
    || die "failed to install to ${bin_dest}"
  chmod 0755 "${bin_dest}/${BIN_NAME}"

  log "installed ${BIN_NAME} ${VERSION} to ${bin_dest}/${BIN_NAME}"
  check_path "$bin_dest"
}

check_path() {
  case ":${PATH}:" in
    *":$1:"*) ;;
    *)
      warn "${1} is not on your PATH."
      warn "Add this to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
      printf '\n    export PATH="%s:$PATH"\n\n' "$1"
      ;;
  esac
}

install_binary
"${PREFIX}/bin/${BIN_NAME}" --version || true

printf '\n'
log "next: run \`rippit instrument\` to wrap your LLM calls with the Rippit SDK,"
log "      or rerun via \`curl -fsSL https://cli.rippit.com/instrument | sh\` to do both at once."
