#!/bin/sh
# CircularChips probe: download ccprobe, check it, run it, clean up.
#
#   curl -fsSL https://chip.bytebazaar.si/circuit/probe.sh | sudo sh -s -- --token XXXX-XXXX-XXXX
#
# 1. Picks the build for this CPU (x86_64 or arm64) from https://chip.bytebazaar.si/circuit/downloads.
# 2. Checks its SHA-256 against https://chip.bytebazaar.si/circuit/downloads/SHA256SUMS. On a mismatch nothing runs.
# 3. Runs: ccprobe run --server https://chip.bytebazaar.si/circuit --token …  (any other ccprobe flags pass through:
#    --load 30m, --include host-b.json, -o report.json, --dry-run)
# 4. Deletes the download. Set CCPROBE_KEEP=1 to keep it in /opt/ccprobe (or CCPROBE_DIR).
#
# The probe reads hardware counters and kernel logs, runs a 10–30 minute load test at full power
# and uploads one signed report. It never reads files, models, datasets or network traffic.
# Read this script first if you like: curl -fsSL https://chip.bytebazaar.si/circuit/probe.sh | less

set -eu

main() {
  ORIGIN="https://chip.bytebazaar.si/circuit"

  say() { printf ' %s\n' "$*"; }
  die() { printf '\n ccprobe: %s\n' "$*" >&2; exit 1; }

  [ "$(uname -s)" = Linux ] || die "ccprobe runs on Linux GPU servers (this is $(uname -s))."
  case "$(uname -m)" in
    x86_64 | amd64) ARCH=amd64 ;;
    aarch64 | arm64) ARCH=arm64 ;;
    *) die "no ccprobe build for this CPU ($(uname -m))." ;;
  esac

  if command -v curl >/dev/null 2>&1; then
    fetch() { curl -fsSL --retry 3 -o "$2" "$1"; }
  elif command -v wget >/dev/null 2>&1; then
    fetch() { wget -q -O "$2" "$1"; }
  else
    die "needs curl or wget."
  fi
  if command -v sha256sum >/dev/null 2>&1; then
    sha() { sha256sum "$1" | cut -d ' ' -f 1; }
  elif command -v shasum >/dev/null 2>&1; then
    sha() { shasum -a 256 "$1" | cut -d ' ' -f 1; }
  else
    die "needs sha256sum to check the download."
  fi

  if [ "$(id -u)" != 0 ]; then
    say "Note: not running as root, so the kernel log (Xid history) may be incomplete."
    say "      Run it as: curl -fsSL $ORIGIN/probe.sh | sudo sh -s -- --token XXXX-XXXX-XXXX"
  fi

  if [ "${CCPROBE_KEEP:-0}" = 1 ]; then
    DIR="${CCPROBE_DIR:-/opt/ccprobe}"
    mkdir -p "$DIR"
  else
    DIR=$(mktemp -d "${TMPDIR:-/tmp}/ccprobe.XXXXXX")
    trap 'rm -rf "$DIR"' EXIT
    trap 'rm -rf "$DIR"; exit 130' INT TERM
  fi

  fetch "$ORIGIN/downloads/SHA256SUMS" "$DIR/SHA256SUMS" || die "could not reach $ORIGIN."
  NAME=$(awk -v a="linux-$ARCH.tar.gz" 'substr($2, length($2) - length(a) + 1) == a && $2 ~ /^ccprobe-/ { print $2; exit }' "$DIR/SHA256SUMS")
  [ -n "$NAME" ] || die "$ORIGIN has no ccprobe build for linux-$ARCH yet."
  WANT=$(awk -v n="$NAME" '$2 == n { print $1; exit }' "$DIR/SHA256SUMS")

  say "Downloading $NAME"
  fetch "$ORIGIN/downloads/$NAME" "$DIR/$NAME" || die "download failed."
  GOT=$(sha "$DIR/$NAME")
  [ "$GOT" = "$WANT" ] || die "SHA-256 mismatch for $NAME (got $GOT, expected $WANT). Nothing was run."
  say "SHA-256 $GOT ok"
  tar -xzf "$DIR/$NAME" -C "$DIR"
  BIN="$DIR/${NAME%.tar.gz}/ccprobe"
  [ -x "$BIN" ] || die "the bundle has no ccprobe binary."
  printf '\n'

  # 'run' is the default; other ccprobe commands (collect, perf, bench, version) pass through.
  CMD=run
  case "${1:-}" in run | collect | perf | bench | version) CMD=$1; shift ;; esac
  if [ "$CMD" = run ]; then
    "$BIN" run --server "$ORIGIN" "$@"
  else
    "$BIN" "$CMD" "$@"
  fi
  if [ "${CCPROBE_KEEP:-0}" = 1 ]; then
    printf '\n'
    say "ccprobe kept in $DIR/${NAME%.tar.gz}"
  fi
}

main "$@"
