#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
BUILD_FILE="$ROOT/build/game.js"
SRC_DIR="$ROOT/src"

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

# Deterministic order (texture helpers before world/player/scene).
FILES=(
	"$SRC_DIR/textureColumn.js"
	"$SRC_DIR/levels.js"
	"$SRC_DIR/tiles.js"
	"$SRC_DIR/world.js"
	"$SRC_DIR/player.js"
	"$SRC_DIR/raycastScene.js"
	"$SRC_DIR/main.js"
)

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"
