#!/usr/bin/env bash
#
# Builds a clean, customer-facing distributable zip of Hostledger — the
# thing you'd actually hand to someone buying the product, upload to a
# cPanel account, or attach to a release.
#
# This script itself is a build-time convenience for the developer/vendor;
# it is NOT required at runtime and is not something a buyer ever needs to
# run (consistent with the project's no-CLI-dependency-at-runtime rule —
# that rule is about the *product*, not about how the vendor builds it).
#
# Usage:
#   bash scripts/build-release.sh
#
# Output: dist/hostledger-<version>.zip
#
# What's deliberately left OUT of the zip (and why):
#   - config.php              generated by the installer per-install; a
#                              customer's real DB credentials must never
#                              ship inside a template zip
#   - install/install.lock    same reasoning — a fresh install must be
#                              able to run the wizard
#   - storage/logs/*.log,     runtime-generated content, not source
#     storage/cache/*
#   - PROJECT_PLAN.md         internal build-tracking document, not
#                              customer-facing documentation
#   - Hostledger_Handover*.docx, ~$*.docx
#                              internal handoff/working documents, plus
#                              stray Word lock files
#   - .git*, scripts/          version control metadata and this build
#                              script aren't part of the shipped product
#   - dist/                   avoid a zip nesting a previous zip if this
#                              script is re-run
#   - /logo.png (project root) the working copy the logo was dropped as;
#                              assets/img/logo.png (the one the app
#                              actually reads) is kept

set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$PROJECT_ROOT"

VERSION=$(grep -oP "define\('HOSTLEDGER_VERSION',\s*'\K[^']+" includes/version.php || true)
if [ -z "$VERSION" ]; then
    echo "Could not read HOSTLEDGER_VERSION from includes/version.php — aborting." >&2
    exit 1
fi

STAGING_DIR=$(mktemp -d)
trap 'rm -rf "$STAGING_DIR"' EXIT

echo "Building Hostledger v${VERSION} release..."

rsync -a "$PROJECT_ROOT"/ "$STAGING_DIR"/hostledger/ \
    --exclude 'config.php' \
    --exclude 'install/install.lock' \
    --exclude 'storage/logs/*.log' \
    --exclude 'storage/cache/*' \
    --exclude '!.gitkeep' \
    --exclude 'PROJECT_PLAN.md' \
    --exclude 'Hostledger_Handover*.docx' \
    --exclude '~$*' \
    --exclude '.git' \
    --exclude '.gitignore' \
    --exclude 'scripts' \
    --exclude 'dist' \
    --exclude '/logo.png'

# rsync's --exclude doesn't easily express "delete file X but keep dir
# entry" alongside a glob exclude of the parent, so re-assert the .gitkeep
# placeholders explicitly in case the excludes above swept them up too.
mkdir -p "$STAGING_DIR/hostledger/storage/logs" "$STAGING_DIR/hostledger/storage/cache"
touch "$STAGING_DIR/hostledger/storage/logs/.gitkeep" "$STAGING_DIR/hostledger/storage/cache/.gitkeep"

# Sanity checks — fail loudly rather than ship something broken.
if [ -f "$STAGING_DIR/hostledger/config.php" ]; then
    echo "ERROR: config.php ended up in the staged build — aborting, do not ship this." >&2
    exit 1
fi
if [ ! -f "$STAGING_DIR/hostledger/config.sample.php" ]; then
    echo "ERROR: config.sample.php is missing from the staged build — aborting." >&2
    exit 1
fi
if [ ! -f "$STAGING_DIR/hostledger/install/index.php" ]; then
    echo "ERROR: install/index.php is missing from the staged build — aborting." >&2
    exit 1
fi

mkdir -p "$PROJECT_ROOT/dist"
OUT_ZIP="$PROJECT_ROOT/dist/hostledger-${VERSION}.zip"
rm -f "$OUT_ZIP"

( cd "$STAGING_DIR" && zip -rq "$OUT_ZIP" hostledger )

echo "Built: dist/hostledger-${VERSION}.zip"
echo ""
echo "Before distributing, manually confirm:"
echo "  1. Unzip it somewhere clean and run through docs/INSTALL.md end to end."
echo "  2. Check CHANGELOG.md matches what's actually in this build."
echo "  3. Decide license-key gating vs. unlocked source (see PROJECT_PLAN.md Phase 10) —"
echo "     this script does not add any gating; it ships the source as-is."
