#!/usr/bin/env bash
# Concatenate game sources into build/game.js, same pattern as raytracer / frameworkv3.
# Also copies the PHP API into build/ so a local `php -S` and the FTP deploy
# serve the same tree. data/config.php and any sqlite are NEVER copied.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
BUILD_FILE="$ROOT/build/game.js"
SRC_DIR="$ROOT/src"

mkdir -p "$ROOT/build"

printf "Build started, output file: %s\n" "$BUILD_FILE"
rm -f "$BUILD_FILE"

FILES=(
	"$SRC_DIR/rng.js"
	"$SRC_DIR/i18n.js"
	"$SRC_DIR/planet.js"
	"$SRC_DIR/blocks.js"
	"$SRC_DIR/easing.js"
	"$SRC_DIR/gameCore.js"
	"$SRC_DIR/flameFx.js"
	"$SRC_DIR/miniLayout.js"
	"$SRC_DIR/miniPredict.js"
	"$SRC_DIR/online.js"
	"$SRC_DIR/offline.js"
	"$SRC_DIR/reward.js"
	"$SRC_DIR/designer.js"
	"$SRC_DIR/loginPrompt.js"
	"$SRC_DIR/avatar.js"
	"$SRC_DIR/main.js"
)

{
	printf '%s\n' "/*"
	printf '%s\n' " * AUTO-GENERATED FILE — DO NOT EDIT BY HAND."
	printf '%s\n' " * Built by src/build.sh from the sources in src/."
	printf '%s\n' " */"
	printf '%s' "var PLANET_CATALOG = "
	cat "$ROOT/server/lib/planets.json"
	printf ';\n'
	printf '%s' "var BLOCK_REGISTRY = "
	cat "$ROOT/server/lib/blocks.json"
	printf ';\n'
	printf '%s' "var I18N_CATALOG = "
	cat "$ROOT/server/lib/i18n.json"
	printf ';\n'
} >> "$BUILD_FILE"

for file in "${FILES[@]}"
do
	if [[ ! -f "$file" ]]
	then
		printf "Missing source file: %s\n" "$file" >&2
		exit 1
	fi
	printf "\n/*\n * Start File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"
	printf "Building %s\n" "$file"
	cat "$file" >> "$BUILD_FILE"
	printf "\n/*\n * End File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"
done

# PHP API lives next to the static game so api.php, auth.php and callback.php
# share a cookie path. Never copy secrets or the database.
SERVER="$ROOT/server"
cp -f "$SERVER/api.php" "$SERVER/auth.php" "$SERVER/callback.php" \
	"$SERVER/logout.php" "$SERVER/router.php" "$SERVER/.htaccess" \
	"$ROOT/build/"
mkdir -p "$ROOT/build/lib"
cp -f "$SERVER/lib/bootstrap.php" "$SERVER/lib/Store.php" \
	"$SERVER/lib/Planets.php" "$SERVER/lib/planets.json" \
	"$SERVER/lib/Blocks.php" "$SERVER/lib/blocks.json" \
	"$SERVER/lib/i18n.json" "$SERVER/lib/I18n.php" \
	"$ROOT/build/lib/"
mkdir -p "$ROOT/build/data"
cp -f "$SERVER/data/.htaccess" "$ROOT/build/data/.htaccess"
# Belt and braces: a leftover local config or db must never enter build/.
rm -f "$ROOT/build/data/config.php" "$ROOT/build/data/config.lock"
rm -f "$ROOT/build/data/"*.sqlite "$ROOT/build/data/"*.sqlite-journal
rm -rf "$ROOT/build/data/sessions"

# t1256: stamp content-hash query strings on JS/CSS so browsers pick up
# a new bundle even when they have an old game.js or style.css cached.
# Locales live inside game.js (I18N_CATALOG), so the game.js hash covers
# i18n.json as well. Idempotent: an existing ?v=... is replaced.
asset_hash() {
	sha256sum "$1" | cut -c1-12
}
INDEX="$ROOT/build/index.html"
GAME_HASH="$(asset_hash "$ROOT/build/game.js")"
CSS_HASH="$(asset_hash "$ROOT/build/style.css")"
FW_HASH="$(asset_hash "$ROOT/build/framework.js")"
I18N_HASH="$(asset_hash "$ROOT/server/lib/i18n.json")"
# i18n.json is inlined into game.js, so a catalog change already bumps GAME_HASH.
# I18N_HASH is logged so a deploy can be compared against the catalog file.
sed -i -E "s/href=\"style\.css(\?v=[^\"]*)?\"/href=\"style.css?v=${CSS_HASH}\"/" "$INDEX"
sed -i -E "s/src=\"framework\.js(\?v=[^\"]*)?\"/src=\"framework.js?v=${FW_HASH}\"/" "$INDEX"
sed -i -E "s/src=\"game\.js(\?v=[^\"]*)?\"/src=\"game.js?v=${GAME_HASH}\"/" "$INDEX"
printf "Asset hashes: game.js=%s style.css=%s framework.js=%s i18n.json=%s\n" \
	"$GAME_HASH" "$CSS_HASH" "$FW_HASH" "$I18N_HASH"

printf "DONE\n"
