#!/usr/bin/env bash
# Concatenate game sources into build/game.js, same pattern as raytracer / frameworkv3.
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/planet.js"
	"$SRC_DIR/easing.js"
	"$SRC_DIR/gameCore.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' " */"
} >> "$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

printf "DONE\n"
