#!/usr/bin/env bash
set -euo pipefail

BASE_URL="${CAMP_DEPLOY_INSTALL_BASE:-https://camp-deploy.superbrain-ai.com}"
WORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/camp-deploy-install.XXXXXX")"

cleanup() {
  rm -rf "$WORK_DIR"
}
trap cleanup EXIT

need_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    echo "Missing required command: $1" >&2
    exit 1
  fi
}

need_cmd curl
need_cmd unzip

echo "Downloading camp-deploy skill package..."
curl -fsSL "$BASE_URL/camp-deploy-toolkit.zip" -o "$WORK_DIR/camp-deploy-toolkit.zip"

if curl -fsSL "$BASE_URL/camp-deploy-toolkit.zip.sha256" -o "$WORK_DIR/camp-deploy-toolkit.zip.sha256"; then
  expected="$(awk '{print $1}' "$WORK_DIR/camp-deploy-toolkit.zip.sha256")"
  if command -v shasum >/dev/null 2>&1; then
    actual="$(shasum -a 256 "$WORK_DIR/camp-deploy-toolkit.zip" | awk '{print $1}')"
  elif command -v sha256sum >/dev/null 2>&1; then
    actual="$(sha256sum "$WORK_DIR/camp-deploy-toolkit.zip" | awk '{print $1}')"
  else
    actual=""
  fi
  if [[ -n "$actual" && "$actual" != "$expected" ]]; then
    echo "Checksum mismatch. Download may be incomplete or unsafe." >&2
    exit 1
  fi
fi

unzip -q "$WORK_DIR/camp-deploy-toolkit.zip" -d "$WORK_DIR/package"
bash "$WORK_DIR/package/camp-deploy/scripts/install.sh" --all

cat <<'MSG'

camp-deploy skill installed.

Next:
1. Put the restricted deploy key at ~/.ssh/camp-deploy-key
2. Run: chmod 600 ~/.ssh/camp-deploy-key
3. Ask your AI agent to use the camp-deploy skill to publish your static website.

The private deploy key is not included in this public installer.
MSG
