#!/usr/bin/env sh
set -eu

BASE_URL="${PAGEPORT_SKILL_BASE_URL:-https://share.pindo.page/skills/publish-html-artifact}"
TARGET_DIR="${PAGEPORT_SKILL_DIR:-$HOME/.agents/skills/publish-html-artifact}"

download() {
  url="$1"
  dest="$2"
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$url" -o "$dest"
  elif command -v wget >/dev/null 2>&1; then
    wget -qO "$dest" "$url"
  else
    echo "Install failed: curl or wget is required." >&2
    exit 1
  fi
}

mkdir -p "$TARGET_DIR/scripts" "$TARGET_DIR/agents"
download "$BASE_URL/SKILL.md" "$TARGET_DIR/SKILL.md"
download "$BASE_URL/scripts/publish-pageport.mjs" "$TARGET_DIR/scripts/publish-pageport.mjs"
download "$BASE_URL/agents/openai.yaml" "$TARGET_DIR/agents/openai.yaml"
chmod +x "$TARGET_DIR/scripts/publish-pageport.mjs"
echo "Installed PagePort skill to $TARGET_DIR"
echo "Set PAGEPORT_ENDPOINT and PAGEPORT_AGENT_TOKEN before publishing."
