#!/bin/sh
# Builds Brogue: Community Edition (https://github.com/tmewett/BrogueCE,
# AGPL-3.0 code, CC BY-SA 4.0 tiles) for the site, from the 1.15.1 release
# source with its SDL2 front end, with these changes (brogue.diff beside this
# script):
# - the play page sizes the game's canvas (wg_resize), so the game's own
#   window size keys (Page Up, Page Down, +, -) and full screen (F11, F12,
#   Alt+Enter) are off: the page owns full screen;
# - the main menu has no Quit, as the game is the page;
# - every turn is written to the game in progress at once, which is named
#   LastGame.broguesave (LastGame seeded.broguesave from a picked seed), so it
#   survives closing the page; a game left on starting another is renamed to
#   an unfinished game, and a run that ends becomes LastRecording.broguerec
#   at once, so it never opens again as a game in progress;
# - screenshots (Print Screen) are off, as they would land among the saves;
# - modifiers come from the key events (SDL_GetModState stays 0 in the
#   browser), so Shift and Ctrl moves work and Alt+Enter is ignored;
# - a run of classic Brogue in normal mode from a random seed reports its
#   score to the page (Module.wgGameOver) as it ends.
# The play page writes the tiles (tiles.png, served as the game file, and
# the tiles.bin the game made from it) to /data/assets before main() runs.
# The player's files (games in progress,
# saved games, recordings, high scores) are the files in the working folder,
# which the play page fills before main() and reads back as the game's save.
# main() is run by the play page (INVOKE_RUN=0).
#
# Needs git and emsdk 4.0.7 (source emsdk_env.sh first).
# Usage: sh build.sh [work dir]
# Writes brogue.js, brogue.wasm and tiles.bin next to this script, and
# tiles.png and the licence notices to public/games/brogue.
# For a newer Brogue CE, copy this folder to brogue-<new rev>-p1 and change
# the revision below; a change to the build here takes the next -p number.
set -eu

BROGUE_REV=1ba4240b7a928ddf0ffb772717bf1d433cd63804 # v1.15.1
EMSDK_VERSION=4.0.7

here=$(cd "$(dirname "$0")" && pwd)
work=${1:-${TMPDIR:-/tmp}/brogue-build}
src=$work/BrogueCE

case "$(emcc --version | head -1)" in
  *" $EMSDK_VERSION "*) ;;
  *) echo "warning: built and tested with emsdk $EMSDK_VERSION" >&2 ;;
esac

# Emscripten fetches these ports as GitHub archives; clone them instead.
. "$here/ports.sh"
port zlib madler/zlib v1.3.1 zlib-1.3.1 https://github.com/madler/zlib/archive/refs/tags/v1.3.1.tar.gz
port sdl2 libsdl-org/SDL release-2.32.0 SDL-release-2.32.0 https://github.com/libsdl-org/SDL/archive/release-2.32.0.zip
port sdl2_image libsdl-org/SDL_image release-2.6.0 SDL_image-release-2.6.0 https://github.com/libsdl-org/SDL_image/archive/refs/tags/release-2.6.0.zip

mkdir -p "$work"
# A checkout left by an earlier build is reset, so this folder's diff is the one applied.
[ -d "$src" ] || git clone -q https://github.com/tmewett/BrogueCE.git "$src"
git -C "$src" -c advice.detachedHead=false checkout -q -f "$BROGUE_REV"
git -C "$src" clean -q -fd src
git -C "$src" apply "$here/brogue.diff"

games=$here/../../games/brogue
mkdir -p "$games"
cp "$src/bin/assets/tiles.png" "$games/tiles.png"
cp "$src/bin/assets/LICENSE.txt" "$games/tiles-LICENSE.txt"
cp "$src/LICENSE.txt" "$games/LICENSE.txt"
cp "$src/bin/assets/tiles.bin" "$here/tiles.bin"

flags='-O2 -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sSDL2_IMAGE_FORMATS=["png"]'
cd "$src"
# shellcheck disable=SC2086
emcc $flags -std=gnu99 -Wno-parentheses -Wno-unused-result \
  -Isrc/brogue -Isrc/platform -Isrc/variants \
  -DDATADIR=/data -DBROGUE_SDL -DBROGUE_EXTRA_VERSION='""' \
  src/brogue/*.c src/variants/*.c \
  src/platform/main.c src/platform/platformdependent.c src/platform/null-platform.c \
  src/platform/sdl2-platform.c src/platform/tiles.c \
  -o "$here/brogue.js" \
  -sASYNCIFY -sASYNCIFY_STACK_SIZE=65536 -sSTACK_SIZE=8mb -sINITIAL_MEMORY=64mb -sALLOW_MEMORY_GROWTH \
  -sINVOKE_RUN=0 -sEXIT_RUNTIME=0 -sFORCE_FILESYSTEM -sEXPORTED_RUNTIME_METHODS=FS,callMain
