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

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

FILES=(
	"$SRC_DIR/textureColumn.js"
	"$SRC_DIR/ppda/depthDecode.js"
	"$SRC_DIR/ppda/viewFrame.js"
	"$SRC_DIR/ppda/compositor.js"
	"$SRC_DIR/ppda/loadPpda.js"
	"$SRC_DIR/ppda/atlasRuntime.js"
	"$SRC_DIR/levelFormat.js"
	"$SRC_DIR/camera.js"
	"$SRC_DIR/floorCast.js"
	"$SRC_DIR/floorGpu.js"
	"$SRC_DIR/lighting.js"
	"$SRC_DIR/levels.js"
	"$SRC_DIR/tiles.js"
	"$SRC_DIR/world.js"
	"$SRC_DIR/player.js"
	"$SRC_DIR/npc.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 "Building editor.js\n"
rm -f "$EDITOR_FILE"
for file in "$SRC_DIR/levelFormat.js" "$SRC_DIR/editorApp.js"
do
	printf "\n/* %s */\n" "$file" >> "$EDITOR_FILE"
	cat "$file" >> "$EDITOR_FILE"
done

printf "DONE\n"
