#!/usr/bin/env bash
# Pixio Plugin After Effects — macOS installer
#
# Usage:
#   curl -fsSL https://beta.pixio.myapps.ai/downloads/after-effects/install-pixio-ae.sh | bash
# or with a local zip:
#   ./install-pixio-ae.sh ./Pixio-After-Effects-0.1.0.zip
set -euo pipefail

ZIP="${1:-}"
DOWNLOAD_URL="${PIXIO_AE_DOWNLOAD_URL:-https://beta.pixio.myapps.ai/downloads/after-effects/Pixio-After-Effects-latest.zip}"
BUNDLE_ID="com.pixio.after-effects-plugin"
TARGET="$HOME/Library/Application Support/Adobe/CEP/extensions/$BUNDLE_ID"

echo "Pixio Plugin After Effects"

if pgrep -x "After Effects" >/dev/null 2>&1; then
  echo "Please quit After Effects first, then re-run this installer."
  exit 1
fi

if [ -z "$ZIP" ]; then
  ZIP="$(mktemp -t pixio-ae).zip"
  echo "Downloading $DOWNLOAD_URL"
  curl -fsSL -o "$ZIP" "$DOWNLOAD_URL"
fi

rm -rf "$TARGET"
mkdir -p "$TARGET"
unzip -q -o "$ZIP" -d "$TARGET"
echo "Installed to $TARGET"

HAS_CLAUDE=no; command -v claude >/dev/null 2>&1 && HAS_CLAUDE=yes
HAS_CODEX=no;  command -v codex  >/dev/null 2>&1 && HAS_CODEX=yes
echo
echo "Agents found:  Claude Code: $HAS_CLAUDE   Codex: $HAS_CODEX"
if [ "$HAS_CLAUDE" = no ] && [ "$HAS_CODEX" = no ]; then
  echo "Install one to chat inside After Effects:"
  echo "  npm i -g @anthropic-ai/claude-code   (then run: claude  and /login)"
  echo "  npm i -g @openai/codex               (then run: codex login)"
fi

echo
echo "Next:"
echo "  1. Start After Effects → Window › Extensions › Pixio"
echo "  2. After Effects › Settings › Scripting & Expressions → enable \"Allow Scripts to Write Files and Access Network\""
echo "  3. Chat tab → pick Claude Code or Codex → describe what to build"
