#!/bin/sh
# Zuno skills installer — https://zuno.press/mcp
# Installs the Zuno agent skills (SKILL.md folders) for Claude Code, Codex and other agents.
#
#   curl -fsSL https://zuno.press/skills/install.sh | sh                   # ~/.claude/skills + ~/.agents/skills
#   curl -fsSL https://zuno.press/skills/install.sh | sh -s -- --claude    # Claude Code only
#   curl -fsSL https://zuno.press/skills/install.sh | sh -s -- --codex     # ~/.agents/skills only (Codex, Gemini CLI, Cursor, VS Code, Devin)
#   curl -fsSL https://zuno.press/skills/install.sh | sh -s -- --project   # into ./.claude/skills + ./.agents/skills of this project
#
# Generated by scripts/build-skills.mjs — do not edit by hand.
set -eu
# Bash 3.2 (macOS /bin/sh) may read UTF-8 bytes after $VAR as part of the name — every expansion next to
# Cyrillic text below is braced, ${VAR}.

VERSION="1.0.0"
SHA256="4f9c58f91cd9350f63ec3d3be11029f99d4d869f375ea30b22e32c0540d55da0"
SKILLS="zuno-ads zuno-avatar zuno-campaign zuno-generate zuno-marketplace-cards zuno-product-photoshoot zuno-social-content"
BASE_URL="${ZUNO_SKILLS_URL:-https://zuno.press/skills}"
ZIP_FILE="${ZUNO_SKILLS_ZIP:-}"

say() { printf '%s\n' "$*"; }
die() { printf 'zuno-skills: %s\n' "$*" >&2; exit 1; }

usage() {
  cat <<'USAGE'
Zuno skills installer

Usage: install.sh [targets] [options]

Targets (default: --claude --codex):
  --claude      ~/.claude/skills        Claude Code (also read by Cursor and VS Code)
  --codex       ~/.agents/skills        Codex, Gemini CLI, Cursor, VS Code Copilot, Devin (alias: --agents)
  --cursor      ~/.cursor/skills        Cursor-only folder
  --all         all of the above
  --dir PATH    any other skills folder

Options:
  --project     install into the current project (./.claude/skills, ./.agents/skills, ./.cursor/skills)
  --uninstall   remove the Zuno skills from the chosen targets
  --zip FILE    install from a downloaded zuno-skills.zip instead of the network
  -h, --help    show this help

Environment: ZUNO_SKILLS_URL (base URL), ZUNO_SKILLS_ZIP (local zip), ZUNO_SKIP_VERIFY=1 (skip SHA-256 check).
USAGE
}

want_claude=0; want_codex=0; want_cursor=0; custom_dirs=""; project=0; uninstall=0
while [ $# -gt 0 ]; do
  case "$1" in
    --claude) want_claude=1 ;;
    --codex|--agents) want_codex=1 ;;
    --cursor) want_cursor=1 ;;
    --all) want_claude=1; want_codex=1; want_cursor=1 ;;
    --dir) [ $# -ge 2 ] || die "--dir needs a path"; custom_dirs="$custom_dirs
$2"; shift ;;
    --dir=*) custom_dirs="$custom_dirs
${1#--dir=}" ;;
    --project) project=1 ;;
    --uninstall) uninstall=1 ;;
    --zip) [ $# -ge 2 ] || die "--zip needs a file"; ZIP_FILE="$2"; shift ;;
    --zip=*) ZIP_FILE="${1#--zip=}" ;;
    -h|--help) usage; exit 0 ;;
    *) usage >&2; die "unknown option: $1" ;;
  esac
  shift
done
if [ "$want_claude$want_codex$want_cursor" = "000" ] && [ -z "$custom_dirs" ]; then want_claude=1; want_codex=1; fi

if [ "$project" -eq 1 ]; then base="$(pwd)"; else
  [ -n "${HOME:-}" ] || die "HOME is not set"
  base="$HOME"
fi
targets=""
[ "$want_claude" -eq 1 ] && targets="$targets
$base/.claude/skills"
[ "$want_codex" -eq 1 ] && targets="$targets
$base/.agents/skills"
[ "$want_cursor" -eq 1 ] && targets="$targets
$base/.cursor/skills"
targets="$targets$custom_dirs"

if [ "$uninstall" -eq 1 ]; then
  printf '%s\n' "$targets" | while IFS= read -r dest; do
    [ -n "$dest" ] || continue
    for name in $SKILLS; do
      if [ -d "$dest/$name" ]; then rm -rf "$dest/$name"; say "  − ${dest}/${name}"; fi
    done
  done
  say "Навыки Zuno удалены. / Zuno skills removed."
  exit 0
fi

command -v unzip >/dev/null 2>&1 || die "нужен unzip / unzip is required (macOS: brew install unzip, Debian/Ubuntu: sudo apt install unzip)"

tmp="$(mktemp -d 2>/dev/null || mktemp -d -t zuno-skills)"
trap 'rm -rf "$tmp"' EXIT INT TERM

if [ -n "$ZIP_FILE" ]; then
  [ -f "$ZIP_FILE" ] || die "zip not found: $ZIP_FILE"
  cp "$ZIP_FILE" "$tmp/pack.zip"
else
  command -v curl >/dev/null 2>&1 || die "нужен curl / curl is required"
  say "Скачиваю навыки Zuno ${VERSION}… / Downloading Zuno skills ${VERSION}…"
  curl -fsSL --retry 2 -o "$tmp/pack.zip" "$BASE_URL/zuno-skills.zip" || die "download failed: $BASE_URL/zuno-skills.zip"
fi

sum=""
if command -v shasum >/dev/null 2>&1; then sum="$(shasum -a 256 "$tmp/pack.zip" | awk '{print $1}')"
elif command -v sha256sum >/dev/null 2>&1; then sum="$(sha256sum "$tmp/pack.zip" | awk '{print $1}')"
elif command -v openssl >/dev/null 2>&1; then sum="$(openssl dgst -sha256 "$tmp/pack.zip" | awk '{print $NF}')"
fi
if [ "${ZUNO_SKIP_VERIFY:-0}" != "1" ] && [ -n "$sum" ] && [ -z "$ZIP_FILE" ] && [ "$sum" != "$SHA256" ]; then
  die "checksum mismatch (got $sum). Re-run the command; if it repeats, download the ZIP from https://zuno.press/mcp"
fi

unzip -q "$tmp/pack.zip" -d "$tmp/x" || die "cannot unpack the archive"
src="$tmp/x/zuno-skills"
[ -d "$src" ] || die "unexpected archive layout"

printf '%s\n' "$targets" | while IFS= read -r dest; do
  [ -n "$dest" ] || continue
  mkdir -p "$dest" || die "cannot create $dest"
  for dir in "$src"/zuno-*/; do
    [ -d "$dir" ] || continue
    name="$(basename "$dir")"
    rm -rf "$dest/.$name.new"
    cp -R "$dir" "$dest/.$name.new"
    if [ -d "$dest/$name" ]; then verb="обновлён / updated"; else verb="установлен / installed"; fi
    rm -rf "$dest/$name"
    mv "$dest/.$name.new" "$dest/$name"
    say "  ✓ ${dest}/${name} — ${verb}"
  done
done

cat <<NEXT

Готово. Навыки Zuno ${VERSION} установлены.
Дальше:
  1. Подключите сервер Zuno (если ещё не подключён):
       claude mcp add --transport http --scope user zuno https://zuno.press/api/mcp
     затем в Claude Code: /mcp → zuno → Authenticate.
     Другие агенты: https://zuno.press/mcp
  2. Перезапустите агента и попросите: «Сделай главное фото для Ozon по фото ./mug.jpg».

Done. Zuno skills ${VERSION} are installed.
Next:
  1. Connect the Zuno MCP server (if you haven't): claude mcp add --transport http --scope user zuno https://zuno.press/api/mcp
     then in Claude Code run /mcp → zuno → Authenticate. Other agents: https://zuno.press/mcp
  2. Restart your agent and ask: "Make an Ozon main photo from ./mug.jpg".
NEXT
