#!/bin/sh
# WBSP App installer.
#
#   curl -fsSL https://get.wbsp.ai/install.sh | bash
#
# Downloads the wbsp-app binary for your OS/arch from the public WBSP CDN,
# verifies its checksum, and installs it to a directory on your PATH.
#
# Environment overrides:
#   WBSP_BASE_URL    base download URL      (default: https://get.wbsp.ai)
#   WBSP_VERSION     pin a specific version (default: latest)
#   WBSP_INSTALL_DIR install location       (default: /usr/local/bin, else ~/.local/bin)
set -eu

BASE_URL="${WBSP_BASE_URL:-https://get.wbsp.ai}"

err() { echo "wbsp install: $*" >&2; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || err "required command not found: $1"; }

need uname
need tar
if command -v curl >/dev/null 2>&1; then
  fetch() { curl -fsSL "$1" -o "$2"; }
  fetch_stdout() { curl -fsSL "$1"; }
elif command -v wget >/dev/null 2>&1; then
  fetch() { wget -qO "$2" "$1"; }
  fetch_stdout() { wget -qO - "$1"; }
else
  err "need curl or wget"
fi

# --- detect platform ---------------------------------------------------------
os=$(uname -s)
case "$os" in
  Darwin) OS=darwin ;;
  Linux)  OS=linux ;;
  *) err "unsupported OS: $os (macOS and Linux only)" ;;
esac

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

# --- resolve version ---------------------------------------------------------
VERSION="${WBSP_VERSION:-}"
if [ -z "$VERSION" ]; then
  VERSION=$(fetch_stdout "${BASE_URL}/latest.txt" | tr -d ' \t\r\n') \
    || err "could not resolve latest version from ${BASE_URL}/latest.txt"
fi
[ -n "$VERSION" ] || err "empty version"

ARCHIVE="wbsp-app_${VERSION}_${OS}_${ARCH}.tar.gz"
ARCHIVE_URL="${BASE_URL}/${VERSION}/${ARCHIVE}"
CHECKSUMS_URL="${BASE_URL}/${VERSION}/checksums.txt"

echo "Installing wbsp-app ${VERSION} (${OS}/${ARCH})"

# --- download ----------------------------------------------------------------
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

fetch "$ARCHIVE_URL" "$tmp/$ARCHIVE" || err "download failed: $ARCHIVE_URL"

# --- verify checksum ---------------------------------------------------------
if fetch "$CHECKSUMS_URL" "$tmp/checksums.txt" 2>/dev/null; then
  expected=$(grep " $ARCHIVE\$" "$tmp/checksums.txt" | awk '{print $1}')
  if [ -n "$expected" ]; then
    if command -v sha256sum >/dev/null 2>&1; then
      actual=$(sha256sum "$tmp/$ARCHIVE" | awk '{print $1}')
    else
      actual=$(shasum -a 256 "$tmp/$ARCHIVE" | awk '{print $1}')
    fi
    [ "$expected" = "$actual" ] || err "checksum mismatch for $ARCHIVE"
    echo "Checksum verified."
  else
    echo "WARNING: $ARCHIVE not listed in checksums.txt; skipping verification" >&2
  fi
else
  echo "WARNING: checksums.txt unavailable; skipping verification" >&2
fi

# --- extract -----------------------------------------------------------------
tar -xzf "$tmp/$ARCHIVE" -C "$tmp" wbsp-app || err "failed to extract wbsp-app"
chmod +x "$tmp/wbsp-app"

# --- choose install dir ------------------------------------------------------
DIR="${WBSP_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"

if mv "$tmp/wbsp-app" "$DIR/wbsp-app" 2>/dev/null; then
  :
elif command -v sudo >/dev/null 2>&1; then
  echo "Elevated permission needed to write $DIR"
  sudo mv "$tmp/wbsp-app" "$DIR/wbsp-app"
else
  err "cannot write to $DIR (set WBSP_INSTALL_DIR to a writable directory)"
fi

echo "Installed wbsp-app to $DIR/wbsp-app"

case ":$PATH:" in
  *":$DIR:"*) ;;
  *) echo "NOTE: $DIR is not on your PATH. Add it, e.g.: export PATH=\"$DIR:\$PATH\"" ;;
esac

"$DIR/wbsp-app" --version || true
