#!/usr/bin/env sh
# Revund CLI installer.
#
# Usage:
#   curl -fsSL https://revund.dev/install.sh | sh
#
# Detects platform + arch, downloads the matching tarball from
# the latest GitHub release, and installs the `revund` binary
# to ~/.local/bin (or to a directory the user picks via the
# REVUND_INSTALL_DIR env var). Idempotent: re-running upgrades
# in place.
#
# Requirements: curl, tar, uname. POSIX sh — no bashisms.

set -eu

REPO="revund-dev/revund-cli"
INSTALL_DIR="${REVUND_INSTALL_DIR:-$HOME/.local/bin}"

# ── colors (only when stderr is a TTY) ─────────────────────────
if [ -t 2 ]; then
  c_blue=$(printf '\033[34m'); c_green=$(printf '\033[32m')
  c_red=$(printf '\033[31m');  c_dim=$(printf '\033[2m');  c_off=$(printf '\033[0m')
else
  c_blue=; c_green=; c_red=; c_dim=; c_off=
fi
say()  { printf "%s%s%s\n" "$c_blue" "→ $*" "$c_off" >&2; }
ok()   { printf "%s%s%s\n" "$c_green" "✓ $*" "$c_off" >&2; }
warn() { printf "%s%s%s\n" "$c_dim"  "  $*" "$c_off" >&2; }
die()  { printf "%s%s%s\n" "$c_red"  "✗ $*" "$c_off" >&2; exit 1; }

# ── platform detection ────────────────────────────────────────
detect_os() {
  case "$(uname -s)" in
    Linux*)   echo linux ;;
    Darwin*)  echo darwin ;;
    MINGW*|MSYS*|CYGWIN*) echo windows ;;
    *) die "unsupported OS: $(uname -s). Open an issue at https://github.com/$REPO/issues." ;;
  esac
}
detect_arch() {
  case "$(uname -m)" in
    x86_64|amd64) echo amd64 ;;
    arm64|aarch64) echo arm64 ;;
    *) die "unsupported architecture: $(uname -m). Open an issue at https://github.com/$REPO/issues." ;;
  esac
}

OS=$(detect_os)
ARCH=$(detect_arch)
say "detected $OS/$ARCH"

# ── resolve latest release ────────────────────────────────────
say "resolving latest release"
LATEST_URL="https://api.github.com/repos/$REPO/releases/latest"
TAG=$(curl -fsSL "$LATEST_URL" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | head -n1)
if [ -z "$TAG" ]; then
  die "couldn't resolve latest release. Check $LATEST_URL"
fi
warn "latest is $TAG"

# ── download + extract ────────────────────────────────────────
VERSION="${TAG#v}"
EXT=tar.gz
[ "$OS" = "windows" ] && EXT=zip
ASSET="revund_${VERSION}_${OS}_${ARCH}.${EXT}"
URL="https://github.com/$REPO/releases/download/$TAG/$ASSET"

TMP=$(mktemp -d 2>/dev/null || mktemp -d -t revund)
trap 'rm -rf "$TMP"' EXIT

say "downloading $ASSET"
if ! curl -fsSL -o "$TMP/$ASSET" "$URL"; then
  die "download failed: $URL"
fi

say "verifying checksum"
CHECKSUMS_URL="https://github.com/$REPO/releases/download/$TAG/checksums.txt"
if curl -fsSL -o "$TMP/checksums.txt" "$CHECKSUMS_URL"; then
  EXPECTED=$(grep " $ASSET\$" "$TMP/checksums.txt" | awk '{print $1}')
  if [ -n "$EXPECTED" ]; then
    if command -v sha256sum >/dev/null 2>&1; then
      ACTUAL=$(sha256sum "$TMP/$ASSET" | awk '{print $1}')
    elif command -v shasum >/dev/null 2>&1; then
      ACTUAL=$(shasum -a 256 "$TMP/$ASSET" | awk '{print $1}')
    else
      ACTUAL=
      warn "no sha256sum/shasum binary; skipping checksum verification"
    fi
    if [ -n "$ACTUAL" ] && [ "$ACTUAL" != "$EXPECTED" ]; then
      die "checksum mismatch: expected $EXPECTED, got $ACTUAL"
    fi
  fi
else
  warn "couldn't fetch checksums.txt; skipping verification"
fi

say "extracting"
cd "$TMP"
if [ "$EXT" = "zip" ]; then
  command -v unzip >/dev/null 2>&1 || die "unzip is required on Windows"
  unzip -q "$ASSET"
else
  tar -xzf "$ASSET"
fi

# ── install ───────────────────────────────────────────────────
mkdir -p "$INSTALL_DIR"
mv revund "$INSTALL_DIR/revund"
chmod +x "$INSTALL_DIR/revund"
ok "installed revund $TAG to $INSTALL_DIR/revund"

# ── PATH check ────────────────────────────────────────────────
case ":$PATH:" in
  *":$INSTALL_DIR:"*)
    ok "$INSTALL_DIR is on \$PATH"
    ;;
  *)
    warn "$INSTALL_DIR is NOT on \$PATH"
    warn "add this to your shell profile (~/.zshrc, ~/.bashrc, etc.):"
    printf "    %sexport PATH=\"%s:\$PATH\"%s\n" "$c_dim" "$INSTALL_DIR" "$c_off" >&2
    ;;
esac

ok "run \`revund --help\` to get started"
