#!/usr/bin/env bash
# Release build (doc 12 §8): composer runs on dev machines / CI — never on the
# server — and vendor/ is bundled into the artifact. The artifact is a clean
# export of HEAD (git archive honours .gitattributes export-ignore, so tests
# and docs stay out) plus production dependencies and a VERSION stamp.
#
#   build/release.sh [version-label]
#
# Output: dist/build/ (deployable tree) and dist/cafe-centre-<version>.tar.gz
set -euo pipefail
cd "$(dirname "$0")/.."

version="${1:-$(git describe --tags --always --dirty 2>/dev/null || echo untagged)}"

rm -rf dist/build
mkdir -p dist/build
git archive HEAD | tar -x -C dist/build

composer install \
    --working-dir=dist/build \
    --no-dev --no-interaction --no-progress \
    --optimize-autoloader --classmap-authoritative

printf '%s\n' "$version" > dist/build/VERSION

tar -czf "dist/cafe-centre-${version}.tar.gz" -C dist/build .

echo "Artifact: dist/cafe-centre-${version}.tar.gz"
sha256sum "dist/cafe-centre-${version}.tar.gz"
