#!/usr/bin/env bash
# Installs the glide-video-from-reference skill for Claude Code.
#   curl -fsSL https://screenagent.app/skills/install.sh | bash                    # for your user: ~/.claude/skills
#   curl -fsSL https://screenagent.app/skills/install.sh | bash -s -- --project    # for this project: ./.claude/skills
#   curl -fsSL https://screenagent.app/skills/install.sh | bash -s -- --dir <path> # any skills folder
# Running it again updates the skill in place.
set -euo pipefail

SKILL="glide-video-from-reference"
ZIP_URL="https://screenagent.app/skills/glide-video-from-reference.zip"
SHA256="5412d2db48ee449bdae7aed387362b68768a9d34d421491d6be28296117f326a"
DEST="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills"

while [ $# -gt 0 ]; do
  case "$1" in
    --project) DEST="$PWD/.claude/skills" ;;
    --dir) shift; DEST="${1:?--dir needs a path}" ;;
    -h|--help) echo "Options: --project (install into ./.claude/skills), --dir <path> (any skills folder)"; exit 0 ;;
    *) echo "Unknown option: $1 (use --project or --dir <path>)" >&2; exit 1 ;;
  esac
  shift
done

for tool in curl unzip shasum; do
  command -v "$tool" >/dev/null || { echo "$tool is required." >&2; exit 1; }
done

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
echo "Downloading $ZIP_URL"
curl -fsSL "$ZIP_URL" -o "$TMP/skill.zip"
echo "$SHA256  $TMP/skill.zip" | shasum -a 256 -c --status || { echo "Checksum mismatch: the download is incomplete or changed. Try again." >&2; exit 1; }
unzip -q "$TMP/skill.zip" -d "$TMP/unpacked"
[ -f "$TMP/unpacked/$SKILL/SKILL.md" ] || { echo "The archive has no $SKILL/SKILL.md." >&2; exit 1; }

mkdir -p "$DEST"
rm -rf "${DEST:?}/$SKILL"
mv "$TMP/unpacked/$SKILL" "$DEST/$SKILL"
chmod +x "$DEST/$SKILL"/scripts/*.sh "$DEST/$SKILL"/scripts/*.py 2>/dev/null || true
echo "Installed $SKILL in $DEST/$SKILL"

echo
[ -d /Applications/Glide.app ] || echo "- Screen Agent isn't in /Applications yet: get it at https://screenagent.app"
command -v ffmpeg >/dev/null || echo "- Install ffmpeg for video analysis: brew install ffmpeg"
if command -v claude >/dev/null && ! claude mcp get screen-agent >/dev/null 2>&1; then
  echo "- Connect Screen Agent to Claude Code: claude mcp add -s user screen-agent -- '/Applications/Screen Agent.app/Contents/MacOS/ScreenAgent' --mcp"
fi
echo "Then open Screen Agent, start Claude Code and ask: \"Make a Screen Agent video like ~/Downloads/reference.mp4\""
