#!/usr/bin/env sh
# Install the NapkinLab portable skill pack into the current project.
# curl -fsSL https://napkinlab.pages.dev/skill/install.sh | bash

set -eu

BASE="${NAPKINLAB_URL:-https://napkinlab.pages.dev}"
BASE="${BASE%/}"

TMP="/tmp/napkinlab-install-$$.mjs"
trap 'rm -f "$TMP"' EXIT

curl -fsSL "${BASE}/skill/install.mjs" >"$TMP"

# SPA fallback returns 200 HTML for missing /skill/* — refuse to eval it.
case "$(head -c 64 "$TMP" | tr '[:upper:]' '[:lower:]')" in
  *'<!doctype html'*|*'<html'*)
    echo "napkinlab install: ${BASE}/skill/install.mjs returned HTML (pack not deployed?)" >&2
    exit 1
    ;;
esac

node "$TMP" --base "$BASE" --dir "$(pwd)"
