#!/usr/bin/env bash
# Empacota o tema WordPress em dist/ufpe-ds-theme.zip, pronto para upload
# em Aparência → Temas → Adicionar nova → Enviar tema.
#
# Passos: rebuilda @ufpe/tokens, copia o tokens.css para assets/, monta um
# staging com a pasta-raiz "ufpe-ds-theme/" (exigida pelo WordPress) e zipa.
set -euo pipefail

THEME_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$THEME_DIR/../.." && pwd)"
SLUG="ufpe-ds-theme"
DIST="$THEME_DIR/dist"
STAGE="$DIST/$SLUG"

echo "→ build dos tokens (@ufpe/tokens)…"
pnpm --filter @ufpe/tokens build

echo "→ copia tokens.css para o tema…"
mkdir -p "$THEME_DIR/assets"
cp "$ROOT/packages/tokens/src/tokens.css" "$THEME_DIR/assets/tokens.css"

echo "→ monta staging…"
rm -rf "$STAGE" "$DIST/$SLUG.zip"
mkdir -p "$STAGE"
for item in style.css theme.json functions.php readme.txt screenshot.png templates parts patterns styles assets; do
  [ -e "$THEME_DIR/$item" ] && cp -R "$THEME_DIR/$item" "$STAGE/"
done

echo "→ zip…"
( cd "$DIST" && zip -rq "$SLUG.zip" "$SLUG" -x '*.DS_Store' )
rm -rf "$STAGE"

SIZE=$(du -h "$DIST/$SLUG.zip" | cut -f1)
echo "✓ $DIST/$SLUG.zip ($SIZE)"
