#!/usr/bin/env bash
# ag-sync — register/refresh the agentile plugin from this repo as a
# snapshot install. This is the distribution path: what it produces is exactly
# what someone else gets when they install. Run it after a batch of edits to
# re-snapshot, or once to register the marketplace + install the first time.
#
# For a live edit loop on your own machine, run bin/ag-dev-link instead.
set -euo pipefail

REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MARKET="agentile"
PLUGIN="agentile"

echo "Repo:        $REPO"
echo "Marketplace: $MARKET"
echo "Plugin:      $PLUGIN"
echo

# Validate before touching anything installed.
claude plugin validate "$REPO"

# Register the marketplace if missing, else update it from source.
if claude plugin marketplace list 2>/dev/null | grep -q "$MARKET"; then
  echo "Updating marketplace..."
  claude plugin marketplace update "$MARKET"
else
  echo "Adding marketplace from ${REPO}..."
  claude plugin marketplace add "$REPO"
fi

# Install if missing, else update.
if claude plugin list 2>/dev/null | grep -q "$PLUGIN"; then
  echo "Updating plugin..."
  claude plugin update "$PLUGIN"
else
  echo "Installing plugin..."
  claude plugin install "$PLUGIN@$MARKET"
fi

echo
echo "Done. Restart your Claude Code session to apply."
