#!/bin/sh
# slapp installer — curl -fsSL https://slapp.dev/install.sh | bash
# Clones slapp onto this Mac, then hands off to the repo's own installer
# (server/install.sh), which wires slappd and the slapp command and starts
# slappd as a LaunchAgent. Your part is left standing at the end.
#
# knobs: SLAPP_DIR (where it lands), SLAPP_REPO_URL (a fork or a local clone),
#        SLAPP_DRY=1 (clones, prints the plan, touches nothing else).

set -u

RED="\033[38;5;167m"
GREEN="\033[38;5;108m"
DIM="\033[2m"
BOLD="\033[1m"
OFF="\033[0m"

step() { printf "%b\n" "${DIM}·${OFF} $1"; }
ok()   { printf "%b\n" "  ${GREEN}ok${OFF}  $1"; }
warn() { printf "%b\n" "  ${RED}!${OFF}  $1"; }
die()  { printf "%b\n" "  ${RED}x${OFF}  $1" >&2; exit 1; }

printf "%b\n" ""
printf "%b\n" "${BOLD}slapp.${OFF} hold. talk. heard."
printf "%b\n" "${DIM}the installer. clones the repo, wires the daemon, starts it.${OFF}"
printf "%b\n" ""

# ── platform ──────────────────────────────────────────────────────────
case "$(uname -s)" in
  Darwin) ;;
  *) die "macOS only for now — the daemon drives Xcode simulators." ;;
esac

# ── preflight ─────────────────────────────────────────────────────────
command -v git >/dev/null 2>&1 || die "git missing — install it first."
command -v node >/dev/null 2>&1 || die "node missing — node 24 or newer, please."
NODE_MAJOR="$(node -p 'parseInt(process.versions.node, 10)' 2>/dev/null || echo 0)"
case "$NODE_MAJOR" in ''|*[!0-9]*) NODE_MAJOR=0 ;; esac
if ! node -e 'process.exit(typeof require("zlib").createZstdDecompress === "function" ? 0 : 1)' 2>/dev/null; then
  die "node cannot unzip what the daemon ships (node 24 or newer, please).
       install it somewhere the LaunchAgent can see it (/opt/homebrew/bin)."
fi
[ "$NODE_MAJOR" -lt 24 ] && warn "node carries zstd but is under 24 — pi itself may want the newer one."
command -v python3 >/dev/null 2>&1 || die "python3 missing — 3.9+ needed."
python3 -c "import qrcode, PIL" 2>/dev/null || warn "python3 has no qrcode/pillow — pip3 install qrcode pillow (the QR draws with them)."

# ── where it lands ────────────────────────────────────────────────────
REPO_DIR="${SLAPP_DIR:-$HOME/Slapp}"
CLONE_URL="${SLAPP_REPO_URL:-https://github.com/Snail3D/Slapp}"

# ── the code ──────────────────────────────────────────────────────────
if [ -d "$REPO_DIR/server" ]; then
  step "repo already cloned — updating"
  git -C "$REPO_DIR" pull --ff-only >/dev/null 2>&1 && ok "updated" || warn "update skipped (offline or diverged)"
else
  step "cloning slapp"
  mkdir -p "$(dirname "$REPO_DIR")"
  if git clone --depth 1 "$CLONE_URL" "$REPO_DIR" >/dev/null 2>&1; then
    ok "cloned to $REPO_DIR"
  elif command -v gh >/dev/null 2>&1 && gh auth status >/dev/null 2>&1 \
       && gh repo clone Snail3D/Slapp "$REPO_DIR" -- --depth 1 >/dev/null 2>&1; then
    ok "cloned with gh (the repo is private, you are authenticated)"
  else
    die "could not clone $CLONE_URL — the repo is private.
       clone it yourself (gh repo clone Snail3D/Slapp), then rerun with SLAPP_DIR=<where it landed>."
  fi
fi

# ── hand off to the repo's installer ──────────────────────────────────
if [ "${SLAPP_DRY:-0}" = "1" ]; then
  printf "%b\n" ""
  printf "%b\n" "  ${RED}dry run${OFF} — would now run:"
  printf "%b\n" "    ${DIM}bash $REPO_DIR/server/install.sh${OFF}"
  printf "%b\n" "  ${DIM}(that wires slappd and the slapp command, and starts slappd)${OFF}"
  printf "%b\n" ""
  exit 0
fi

step "wiring slappd and the slapp command"
bash "$REPO_DIR/server/install.sh" || die "the repo installer stopped — read its last line, fix it, rerun."

# ── done ──────────────────────────────────────────────────────────────
printf "%b\n" ""
printf "%b\n" "  ${RED}your part${OFF}:"
printf "%b\n" "    1. run pi and type ${DIM}/login${OFF} — paste your own model key (BYOK, no relay)"
printf "%b\n" "    2. put this Mac on your tailnet so the phone reaches it anywhere"
printf "%b\n" "    3. the phone needs the bridge (docs/SELF-HOSTING.md) — with one running, open a pairing window and pair from the app ${DIM}(settings → pair)${OFF}"
printf "%b\n" ""
printf "%b\n" "  ${DIM}hold. talk. heard.${OFF}"
printf "%b\n" ""
