#!/usr/bin/env bash
set -euo pipefail

PLUGIN_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
PRIMARY_OUTPUT_INPUT="${1:?usage: build-packages.sh <output-dir> [mirror-dir ...]}"
mkdir -p "$PRIMARY_OUTPUT_INPUT"
PRIMARY_OUTPUT="$(cd "$PRIMARY_OUTPUT_INPUT" && pwd)"
shift
MIRRORS=()
for mirror_input in "$@"; do
  mkdir -p "$mirror_input"
  MIRRORS+=("$(cd "$mirror_input" && pwd)")
done
STAGE="$(mktemp -d)"

cleanup() {
  rm -rf "$STAGE"
}
trap cleanup EXIT

archive_excludes=(
  --exclude '.git'
  --exclude '.carbon'
  --exclude '.jank'
  --exclude '.playwright-cli'
  --exclude '.ruff_cache'
  --exclude 'cloud-hidden'
  --exclude 'output'
  --exclude '__pycache__'
  --exclude '*.pyc'
  --exclude '.DS_Store'
)

copy_plugin() {
  local destination="$1"
  mkdir -p "$destination"
  rsync -a "${archive_excludes[@]}" "$PLUGIN_ROOT/" "$destination/"
}

copy_openai_plugin() {
  local destination="$1"
  mkdir -p "$destination"
  rsync -a "${archive_excludes[@]}" \
    --exclude '/.claude-plugin' \
    --exclude '/.cursor-plugin' \
    --exclude '/.mcp.claude.json' \
    --exclude '/plugin.json' \
    --exclude '/mcp.json' \
    --exclude '/packaging' \
    "$PLUGIN_ROOT/" "$destination/"
}

copy_claude_plugin() {
  local destination="$1"
  mkdir -p "$destination"
  rsync -a "${archive_excludes[@]}" \
    --exclude '/.codex-plugin' \
    --exclude '/.cursor-plugin' \
    --exclude '/.mcp.claude.json' \
    --exclude '/plugin.json' \
    --exclude '/mcp.json' \
    --exclude '/packaging' \
    "$PLUGIN_ROOT/" "$destination/"
}

copy_agent_plugin() {
  local destination="$1"
  mkdir -p "$destination"
  rsync -a "${archive_excludes[@]}" \
    --exclude '/.claude-plugin' \
    --exclude '/.codex-plugin' \
    --exclude '/.cursor-plugin' \
    --exclude '/.mcp.json' \
    --exclude '/.mcp.claude.json' \
    --exclude '/packaging' \
    "$PLUGIN_ROOT/" "$destination/"
}

copy_cursor_plugin() {
  local destination="$1"
  mkdir -p "$destination"
  rsync -a "${archive_excludes[@]}" \
    --exclude '/.claude-plugin' \
    --exclude '/.codex-plugin' \
    --exclude '/.mcp.json' \
    --exclude '/.mcp.claude.json' \
    --exclude '/plugin.json' \
    --exclude '/packaging' \
    "$PLUGIN_ROOT/" "$destination/"
}

copy_standalone_mcp() {
  local destination="$1"
  mkdir -p "$destination"
  rsync -a "${archive_excludes[@]}" \
    --exclude '/.claude-plugin' \
    --exclude '/.codex-plugin' \
    --exclude '/.cursor-plugin' \
    --exclude '/.mcp.json' \
    --exclude '/.mcp.claude.json' \
    --exclude '/plugin.json' \
    --exclude '/mcp.json' \
    --exclude '/packaging' \
    "$PLUGIN_ROOT/" "$destination/"
}

prepare_claude_plugin() {
  local destination="$1"
  copy_claude_plugin "$destination"
  python3 "$PLUGIN_ROOT/scripts/configure-claude-plugin-skills.py" "$destination" --apply >/dev/null
}

mkdir -p "$STAGE/openai"
copy_openai_plugin "$STAGE/openai/carbon"
cd "$STAGE/openai"
zip -qr "$STAGE/carbon-openai.zip" carbon -x '*/.DS_Store'

mkdir -p "$STAGE/claude"
prepare_claude_plugin "$STAGE/claude/carbon"
cd "$STAGE/claude"
zip -qr "$STAGE/carbon-claude.zip" carbon -x '*/.DS_Store'

mkdir -p "$STAGE/claude-marketplace/.claude-plugin" "$STAGE/claude-marketplace/plugins"
cp "$PLUGIN_ROOT/packaging/claude-marketplace.json" "$STAGE/claude-marketplace/.claude-plugin/marketplace.json"
prepare_claude_plugin "$STAGE/claude-marketplace/plugins/carbon"
cd "$STAGE/claude-marketplace"
zip -qr "$STAGE/carbon-claude-marketplace.zip" . -x '*/.DS_Store'

mkdir -p "$STAGE/agent"
copy_agent_plugin "$STAGE/agent/carbon"
cd "$STAGE/agent"
zip -qr "$STAGE/carbon-agent-plugin.zip" carbon -x '*/.DS_Store'
cd "$STAGE/agent/carbon"
zip -qr "$STAGE/carbon.plugin" . -x '*/.DS_Store'
cp "$STAGE/carbon.plugin" "$STAGE/carbon-antigravity.plugin"

mkdir -p "$STAGE/cursor"
copy_cursor_plugin "$STAGE/cursor/carbon"
cd "$STAGE/cursor"
zip -qr "$STAGE/carbon-cursor.zip" carbon -x '*/.DS_Store'

mkdir -p "$STAGE/codex/.agents/plugins" "$STAGE/codex/plugins"
cp "$PLUGIN_ROOT/packaging/marketplace.json" "$STAGE/codex/.agents/plugins/marketplace.json"
copy_openai_plugin "$STAGE/codex/plugins/carbon"
cd "$STAGE/codex"
zip -qr "$STAGE/carbon-codex.zip" . -x '*/.DS_Store'

copy_standalone_mcp "$STAGE/carbon-mcp"
cd "$STAGE"
zip -qr "$STAGE/carbon-mcp.zip" carbon-mcp -x '*/.DS_Store'

artifacts=(
  carbon-openai.zip
  carbon-agent-plugin.zip
  carbon-cursor.zip
  carbon-claude.zip
  carbon-claude-marketplace.zip
  carbon-codex.zip
  carbon.plugin
  carbon-antigravity.plugin
  carbon-mcp.zip
)

for artifact in "${artifacts[@]}"; do
  install -m 0644 "$STAGE/$artifact" "$PRIMARY_OUTPUT/$artifact"
done

for mirror in "${MIRRORS[@]:-}"; do
  [[ -n "$mirror" ]] || continue
  for artifact in "${artifacts[@]}"; do
    install -m 0644 "$STAGE/$artifact" "$mirror/$artifact"
  done
done

echo "Built CARBON packages in $PRIMARY_OUTPUT"
for mirror in "${MIRRORS[@]:-}"; do
  [[ -n "$mirror" ]] || continue
  echo "Mirrored CARBON packages to $mirror"
done
