#!/bin/sh
# Check a Mac for the pieces the DeepSeek Harness Buzz plugin needs.
# Usage: sh scripts/onboard-check.sh [--open]
set -u

OPEN=0
[ "${1:-}" = "--open" ] && OPEN=1

BUZZ="/Applications/Buzz.app"
DSH="/Applications/DeepSeek Harness.app"
DSH_BIN="$DSH/Contents/Resources/runtime/cli/bin/dsh"
REGISTRY="$HOME/Library/Application Support/xyz.block.buzz.desktop/profiles/default/registry.json"
BUZZ_URL="https://github.com/block/buzz-app/releases"
DSH_URL="https://www.deepseek.com/en/download/"

missing=0
next=""
open_url=""

say() { printf '%s\n' "$*"; }
ok() { printf '  [ok]      %s\n' "$*"; }
no() { printf '  [missing] %s\n' "$*"; }
note() { printf '  -         %s\n' "$*"; }
fail() {
  missing=$((missing + 1))
  no "$1"
  [ -n "${2:-}" ] && next="$2"
  [ -n "${3:-}" ] && open_url="$3"
}

say "DeepSeek Harness for Buzz — onboarding check"
say ""

say "System"
os="$(sw_vers -productVersion 2>/dev/null || echo unknown)"
arch="$(uname -m)"
note "macOS $os ($arch)"
if [ "$arch" = "arm64" ]; then
  ok "Apple silicon"
else
  note "The current DeepSeek Harness download is Apple silicon; check the page for Intel"
fi
say ""

say "Buzz"
if [ -d "$BUZZ" ]; then
  version="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' \
    "$BUZZ/Contents/Info.plist" 2>/dev/null || echo unknown)"
  ok "Buzz $version"
  case "$version" in
    0.7.* | 1.*) ;;
    *) note "The plugin needs Buzz 0.7.0-preview.262.1 or newer" ;;
  esac
else
  fail "Buzz is not in /Applications" "Install Buzz: $BUZZ_URL" "$BUZZ_URL"
fi
say ""

say "DeepSeek Harness"
if [ -x "$DSH_BIN" ]; then
  dsh_version="$("$DSH_BIN" --version 2>/dev/null || echo unknown)"
  ok "Desktop app, bundled dsh $dsh_version"
elif command -v dsh >/dev/null 2>&1; then
  ok "dsh on PATH: $(command -v dsh)"
else
  fail "No DeepSeek Harness app or dsh found" \
    "Install DeepSeek Harness: $DSH_URL" "$DSH_URL"
fi
say ""

say "Plugin"
if [ -f "$REGISTRY" ] && grep -q '"dsh.harness"' "$REGISTRY" 2>/dev/null; then
  ok "dsh.harness is installed in Buzz"
else
  fail "dsh.harness is not installed" \
    "Install the plugin with the team's pinned link (or the zip), then rerun this check"
fi
say ""

if [ "$missing" -eq 0 ]; then
  say "Everything the plugin needs is installed."
  say "Next: Agents -> New agent -> DeepSeek Harness, then Settings -> Test connection."
  exit 0
fi

say "Next: $next"
if [ "$OPEN" -eq 1 ] && [ -n "$open_url" ]; then
  open "$open_url" 2>/dev/null || true
fi
exit 1
