#!/usr/bin/env bash
# Construit build/gcms-full-${VERSION}.zip — CMS prêt à déployer (vendor, assets Vite).
# À lancer après composer install et npm run build (faits dans le job CI ou en local).
# Usage : VERSION=1.2.3 bash scripts/build-gcms-full-release.sh
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
VERSION="${VERSION:-}"
if [[ -z "$VERSION" ]]; then
  echo "Erreur : définir VERSION (ex. VERSION=1.2.3)" >&2
  exit 1
fi

if [[ ! -d "${ROOT}/vendor" ]]; then
  echo "Erreur : vendor/ absent — exécutez d'abord : composer install --no-dev --no-interaction" >&2
  exit 1
fi

OUT="${ROOT}/build"
mkdir -p "${OUT}"
FULL_ZIP="${OUT}/gcms-full-${VERSION}.zip"
TMP_ZIP="/tmp/gcms-full-${VERSION}.zip.$$"
cd "${ROOT}"
rm -f "${TMP_ZIP}" "${FULL_ZIP}"

# Sortie dans /tmp d’abord : le ZIP vit sous build/ et serait sinon inclus dans l’archive.
zip -rq "${TMP_ZIP}" . \
  -x "./.git/*" \
  -x "./build/*" \
  -x "./node_modules/*" \
  -x "./.env" \
  -x "./.env.*" \
  -x "./.phpunit.result.cache" \
  -x "./.phpunit.cache/*" \
  -x "./phpunit.xml" \
  -x "./tests/*" \
  -x "./coverage/*" \
  -x "./storage/logs/*" \
  -x "./storage/framework/cache/*" \
  -x "./storage/framework/sessions/*" \
  -x "./storage/framework/views/*" \
  -x "./storage/app/gcms-version.lock" \
  -x "./.idea/*" \
  -x "./.vscode/*" \
  -x "./.fleet/*" \
  -x "./.nova/*" \
  -x "./.zed/*" \
  -x "./.cursor/*" \
  -x "./Homestead.yaml" \
  -x "./Homestead.json" \
  -x "./auth.json" \
  -x "./Thumbs.db" \
  -x "./.DS_Store" \
  -x "*.log"

mv "${TMP_ZIP}" "${FULL_ZIP}"
echo "OK : ${FULL_ZIP}"
