#!/bin/sh
# adlibrary-research — universal Agent Skill installer
#
#   curl -fsSL https://adlibrary.com/skill/install.sh | sh
#
# Installs the `adlibrary-research` skill (open SKILL.md standard,
# agentskills.io) so any compatible coding agent can do ad-intelligence
# research against the adlibrary.com API — Claude Code, OpenAI Codex,
# Gemini CLI, GitHub Copilot / VS Code, Cursor, and other tools that read
# the Agent Skills format.
#
# Layout:
#   .agents/skills/adlibrary-research   <- canonical copy (Codex and other
#                                          spec-compliant agents scan this)
#   .claude/skills/adlibrary-research   <- symlink (Claude Code)
#
# Env overrides:
#   ADL_SKILL_DIR=/path/to/skills   # install ONLY here, no symlinks
#   ADL_SKILL_URL=https://adlibrary.com/skill/adlibrary-research.tar.gz
#
# This script is POSIX sh, makes no permanent changes outside the skills
# dirs, and prints exactly what it does. Read it before piping to sh if
# you prefer.

set -eu

SKILL_NAME="adlibrary-research"
TARBALL_URL="${ADL_SKILL_URL:-https://adlibrary.com/skill/adlibrary-research.tar.gz}"

# ---- pick the install root --------------------------------------------------
# Project-local wins if you're inside a project that already has an agent
# dir (.agents/.claude); otherwise install user-global under $HOME.
CUSTOM_DIR=""
if [ -n "${ADL_SKILL_DIR:-}" ]; then
  CUSTOM_DIR="$ADL_SKILL_DIR"
  ROOT=""
elif [ -d ".agents" ] || [ -d ".claude" ]; then
  ROOT="$(pwd)"
else
  ROOT="$HOME"
fi

if [ -n "$CUSTOM_DIR" ]; then
  CANONICAL_DIR="$CUSTOM_DIR"
else
  CANONICAL_DIR="$ROOT/.agents/skills"
fi
DEST="$CANONICAL_DIR/$SKILL_NAME"

# ---- need a downloader -----------------------------------------------------
if command -v curl >/dev/null 2>&1; then
  DL="curl -fsSL"
elif command -v wget >/dev/null 2>&1; then
  DL="wget -qO-"
else
  echo "error: need curl or wget to download the skill." >&2
  exit 1
fi

echo "adlibrary-research skill installer"
echo "  source: $TARBALL_URL"
echo "  target: $DEST"

# ---- download + extract into a temp dir, then swap atomically --------------
TMP="$(mktemp -d "${TMPDIR:-/tmp}/adl-skill.XXXXXX")"
trap 'rm -rf "$TMP"' EXIT INT TERM

echo "  downloading..."
$DL "$TARBALL_URL" > "$TMP/skill.tar.gz"

# sanity: a real gzip tarball, not an HTML error page
if ! tar -tzf "$TMP/skill.tar.gz" >/dev/null 2>&1; then
  echo "error: downloaded file is not a valid tar.gz (got an error page?)." >&2
  echo "       check the URL: $TARBALL_URL" >&2
  exit 1
fi

echo "  extracting..."
tar -xzf "$TMP/skill.tar.gz" -C "$TMP"

# the tarball contains a top-level "adlibrary-research/" dir
if [ ! -f "$TMP/$SKILL_NAME/SKILL.md" ]; then
  echo "error: tarball did not contain $SKILL_NAME/SKILL.md as expected." >&2
  exit 1
fi

mkdir -p "$CANONICAL_DIR"
rm -rf "$DEST"
mv "$TMP/$SKILL_NAME" "$DEST"
echo "  installed -> $DEST"

# ---- link into agent-specific skill dirs ------------------------------------
# Canonical copy lives in .agents/skills (scanned natively by Codex and other
# agents that follow the Agent Skills spec). Agents with their own skills dir
# get a symlink to the canonical copy. Relative link: .claude/skills and
# .agents/skills are siblings under the same root.
link_skill() {
  agent_dir="$1"   # e.g. $ROOT/.claude/skills
  label="$2"
  link="$agent_dir/$SKILL_NAME"
  mkdir -p "$agent_dir"
  rm -rf "$link"
  if ln -s "../../.agents/skills/$SKILL_NAME" "$link" 2>/dev/null; then
    echo "  linked    -> $link ($label)"
  else
    # filesystem without symlink support: fall back to a real copy
    cp -R "$DEST" "$link"
    echo "  copied    -> $link ($label)"
  fi
}

if [ -z "$CUSTOM_DIR" ]; then
  link_skill "$ROOT/.claude/skills" "Claude Code"
fi

echo ""
echo "Agent compatibility:"
echo "  - OpenAI Codex:  picks up .agents/skills automatically — done."
echo "  - Claude Code:   linked into .claude/skills — done."
echo "  - Other agents (Gemini CLI, Copilot/VS Code, Cursor, ...): point your"
echo "    agent's skills directory at, or symlink/copy:"
echo "      $DEST"
echo ""
echo "Next steps:"
echo "  1. With an active Business subscription, create an API key at:"
echo "       https://adlibrary.com/api-access"
echo "  2. Export it so the skill can use it:"
echo "       export ADLIBRARY_API_KEY=adl_your_key_here"
echo "     (add that line to your shell profile to make it permanent)."
echo "  3. Ask your agent — e.g. \"what ads is Gymshark running?\""
echo "     or \"find winning video ads for protein powder in the US\"."
echo ""
echo "Docs: the skill's reference/ folder has the full API reference, the"
echo "AdCreative schema, and the credit-cost guide."
