#!/bin/sh
# vibeCodeCLI installer · https://vibecodecli.com
#
#   curl -fsSL vibecodecli.com/install.sh | sh
#
# Downloads a prebuilt binary for your OS/arch and installs it. No Rust, no
# compiler, no dependencies required · it's a single static binary.
#
# Env overrides:
#   VIBECODECLI_VERSION       version to install (default: latest)
#   VIBECODECLI_INSTALL_DIR   where to install (default: $HOME/.local/bin)
#   VIBECODECLI_BASE_URL      download origin (default: https://vibecodecli.com/bin)
set -eu

BIN="vibecodecli"
BASE_URL="${VIBECODECLI_BASE_URL:-https://vibecodecli.com/bin}"
VERSION="${VIBECODECLI_VERSION:-latest}"
INSTALL_DIR="${VIBECODECLI_INSTALL_DIR:-$HOME/.local/bin}"
DOCS="https://vibecodecli.com/docs.html"

# ---- pretty output (only colorize a real terminal) -------------------------
if [ -t 1 ]; then
  BOLD=$(printf '\033[1m'); DIM=$(printf '\033[2m'); RED=$(printf '\033[31m')
  GRN=$(printf '\033[32m'); ACC=$(printf '\033[38;5;202m'); RST=$(printf '\033[0m')
else
  BOLD=''; DIM=''; RED=''; GRN=''; ACC=''; RST=''
fi
say(){ printf '%s\n' "$*"; }
die(){ printf '%s\n' "  ${RED}error:${RST} $*" >&2; exit 1; }

# ---- a downloader: curl or wget --------------------------------------------
if command -v curl >/dev/null 2>&1; then
  dl(){ curl -fsSL -o "$1" "$2"; }
elif command -v wget >/dev/null 2>&1; then
  dl(){ wget -qO "$1" "$2"; }
else
  die "need curl or wget on PATH"
fi
command -v tar >/dev/null 2>&1 || die "need tar on PATH"

# ---- detect platform -> rust target triple ---------------------------------
os=$(uname -s); arch=$(uname -m)
case "$os" in
  Linux)  os_t="unknown-linux-musl" ;;   # musl = static, runs on any distro
  Darwin) os_t="apple-darwin" ;;
  *) die "unsupported OS '$os'. Build from source · see $DOCS" ;;
esac
case "$arch" in
  x86_64|amd64)   arch_t="x86_64" ;;
  aarch64|arm64)  arch_t="aarch64" ;;
  *) die "unsupported arch '$arch'. Build from source · see $DOCS" ;;
esac
TARGET="${arch_t}-${os_t}"
ASSET="${BIN}-${TARGET}.tar.gz"
URL="${BASE_URL}/${VERSION}/${ASSET}"
SUMS_URL="${BASE_URL}/${VERSION}/SHA256SUMS"

say ""
say "  ${ACC}vibeCodeCLI${RST} ${DIM}installer${RST}"
say "  ${DIM}target${RST} ${TARGET}   ${DIM}version${RST} ${VERSION}"
say ""

TMP=$(mktemp -d 2>/dev/null || mktemp -d -t vcli) || die "cannot create temp dir"
trap 'rm -rf "$TMP"' EXIT INT TERM

# ---- download --------------------------------------------------------------
say "  ${DIM}↓${RST} downloading ${ASSET} …"
dl "$TMP/$ASSET" "$URL" || die "download failed: $URL
  No prebuilt binary for ${TARGET} yet? Build from source · see $DOCS"

# ---- verify checksum (if SHA256SUMS is published) --------------------------
if dl "$TMP/SHA256SUMS" "$SUMS_URL" 2>/dev/null; then
  if command -v sha256sum >/dev/null 2>&1; then
    got=$(sha256sum "$TMP/$ASSET" | awk '{print $1}')
  elif command -v shasum >/dev/null 2>&1; then
    got=$(shasum -a 256 "$TMP/$ASSET" | awk '{print $1}')
  else
    got=''
  fi
  if [ -n "$got" ]; then
    want=$(grep "$ASSET" "$TMP/SHA256SUMS" | awk '{print $1}' | head -n1)
    [ -n "$want" ] && [ "$got" != "$want" ] && die "checksum mismatch for $ASSET · aborting"
    [ -n "$want" ] && say "  ${GRN}✓${RST} checksum verified"
  fi
else
  say "  ${DIM}· no checksum file published; skipping verification${RST}"
fi

# ---- extract ---------------------------------------------------------------
tar -xzf "$TMP/$ASSET" -C "$TMP" || die "failed to extract $ASSET"
src="$TMP/$BIN"
[ -f "$src" ] || src=$(find "$TMP" -type f -name "$BIN" 2>/dev/null | head -n1)
[ -n "$src" ] && [ -f "$src" ] || die "'$BIN' not found inside the archive"
chmod +x "$src"

# ---- install ---------------------------------------------------------------
mkdir -p "$INSTALL_DIR" 2>/dev/null || die "cannot create $INSTALL_DIR"
if ! mv "$src" "$INSTALL_DIR/$BIN" 2>/dev/null; then
  if command -v sudo >/dev/null 2>&1; then
    sudo mv "$src" "$INSTALL_DIR/$BIN" || die "could not install to $INSTALL_DIR"
  else
    die "cannot write to $INSTALL_DIR · set VIBECODECLI_INSTALL_DIR to a writable dir"
  fi
fi

say ""
say "  ${GRN}✓ installed${RST} ${BOLD}${BIN}${RST} ${DIM}→${RST} ${INSTALL_DIR}/${BIN}"

# ---- PATH guidance + version banner ----------------------------------------
case ":${PATH}:" in
  *":${INSTALL_DIR}:"*) on_path=1 ;;
  *) on_path=0 ;;
esac
if [ "$on_path" -eq 1 ]; then
  ver=$("$INSTALL_DIR/$BIN" --version 2>/dev/null || true)
  [ -n "$ver" ] && say "  ${DIM}${ver}${RST}"
else
  say ""
  say "  ${BOLD}Add it to your PATH${RST} ${DIM}(then restart your shell):${RST}"
  say "    ${ACC}export PATH=\"${INSTALL_DIR}:\$PATH\"${RST}   ${DIM}# add to ~/.bashrc or ~/.zshrc${RST}"
fi

say ""
# Anonymous, best-effort install ping · powers the download counter on the site.
# No personal data; fire-and-forget, never affects the install.
PING_URL="${BASE_URL%/bin}/install-ping?v=${VERSION}&t=${TARGET}"
if command -v curl >/dev/null 2>&1; then
  ( curl -fsS -m 3 "$PING_URL" >/dev/null 2>&1 || true ) &
elif command -v wget >/dev/null 2>&1; then
  ( wget -qO /dev/null -T 3 "$PING_URL" >/dev/null 2>&1 || true ) &
fi

say "  ${BOLD}Get started${RST}"
say "    ${ACC}${BIN}${RST}                              ${DIM}# drop into an interactive session${RST}"
say "    ${ACC}${BIN} run --agents 4 \"your task\"${RST}   ${DIM}# fan out a fleet${RST}"
say "  ${DIM}Docs:${RST} ${DOCS}"
say ""
