#!/usr/bin/env bash
# Orbit CLI installer.  Usage:
#   curl -fsSL https://cli.orbitapps.io/install | bash
#
# Downloads the right native binary for your OS/arch from the latest release,
# verifies its SHA-256, and installs it as `orbit`.
#
# Env overrides:
#   ORBIT_REPO         GitHub repo hosting releases (default: vadelabs/homebrew-tap)
#   ORBIT_VERSION      tag to install (default: latest, e.g. cli-v2026.06.14-3)
#   ORBIT_INSTALL_DIR  install dir (default: /usr/local/bin or ~/.local/bin)
set -euo pipefail

REPO="${ORBIT_REPO:-vadelabs/homebrew-tap}"
BASE="https://github.com/${REPO}/releases"
API="https://api.github.com/repos/${REPO}/releases"

err() { printf '\033[31merror:\033[0m %s\n' "$1" >&2; exit 1; }
info() { printf '\033[38;2;232;145;58m›\033[0m %s\n' "$1" >&2; }

# --- detect platform ---------------------------------------------------------
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
  darwin) os=darwin ;;
  linux)  os=linux ;;
  *) err "unsupported OS: $os (need darwin or linux)" ;;
esac

arch=$(uname -m)
case "$arch" in
  arm64|aarch64) arch=arm64 ;;
  x86_64|amd64)  arch=amd64 ;;
  *) err "unsupported architecture: $arch (need arm64 or amd64)" ;;
esac

if [ "$os" = darwin ] && [ "$arch" = amd64 ]; then
  err "Intel Macs are not supported — orbit ships for Apple Silicon only. On an Intel Mac, run orbit on a Linux host or under Rosetta-emulated tooling."
fi

target="orbit-${os}-${arch}"

# --- resolve version ---------------------------------------------------------
tag="${ORBIT_VERSION:-}"
if [ -z "$tag" ]; then
  info "Resolving latest release…"
  tag=$(curl -fsSL "${API}/latest" \
        | grep '"tag_name"' | head -1 \
        | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')
  [ -n "$tag" ] || err "could not resolve the latest release tag"
fi
info "Installing orbit ${tag#cli-v} (${os}/${arch})"

# --- download + verify -------------------------------------------------------
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
url="${BASE}/download/${tag}/${target}"

info "Downloading ${target}…"
curl -fsSL "$url" -o "${tmp}/orbit" || err "download failed: $url"

if curl -fsSL "${BASE}/download/${tag}/SHA256SUMS" -o "${tmp}/SHA256SUMS" 2>/dev/null; then
  want=$(grep " ${target}\$" "${tmp}/SHA256SUMS" | awk '{print $1}')
  if [ -n "$want" ]; then
    if command -v sha256sum >/dev/null 2>&1; then
      got=$(sha256sum "${tmp}/orbit" | awk '{print $1}')
    else
      got=$(shasum -a 256 "${tmp}/orbit" | awk '{print $1}')
    fi
    [ "$want" = "$got" ] || err "checksum mismatch (expected $want, got $got)"
    info "Checksum verified."
  fi
fi

chmod +x "${tmp}/orbit"

# --- install -----------------------------------------------------------------
dir="${ORBIT_INSTALL_DIR:-}"
if [ -z "$dir" ]; then
  if [ -w /usr/local/bin ] 2>/dev/null; then dir=/usr/local/bin; else dir="${HOME}/.local/bin"; fi
fi
mkdir -p "$dir"
mv "${tmp}/orbit" "${dir}/orbit"

info "Installed to ${dir}/orbit"
case ":${PATH}:" in
  *":${dir}:"*) ;;
  *) info "Add ${dir} to your PATH:  export PATH=\"${dir}:\$PATH\"" ;;
esac
"${dir}/orbit" version || true
