#!/bin/sh
# Builds the Flare engine (https://github.com/flareteam/flare-engine, GPL-3.0-or-later)
# from upstream source, the way its distribution/emscripten/make_emscripten.sh
# does, with these changes (flare.diff beside this script):
# - the play page writes the game data under / and the player's saves and
#   settings under /flare_data before main() runs (no IndexedDB here), and
#   main() is run by the play page (INVOKE_RUN=0);
# - the window takes the canvas size the page gives it, at start
#   (Module.wgWidth/wgHeight) and when the page resizes (wg_resize); full
#   screen is the page's own button;
# - images are loaded without threads, as this build has none;
# - before a map loads, the game shows its loading screen and asks the page
#   (Module.wgMapReady) each frame until the page has downloaded the map's
#   files, so the download doesn't stop the page.
#
# It also packs the game, Flare: Empyrean Campaign
# (https://github.com/flareteam/flare-game, CC-BY-SA-3.0 art and data, OFL
# fonts), with pack.py: one zip (start-<hash>.zip) holds the text, maps,
# every file under 100 KB and the files in pack-start.txt (what the game read
# before its first map, but the hero's gear), plus wg/files.json (the other files) and
# wg/maps.json (deps.py: for each map, the big files it uses and the maps it
# leads to); every other file goes to R2 on its own as <name>-<hash>.<ext>.
#
# Needs git, python3 and emsdk 4.0.7 (source emsdk_env.sh first).
# Usage: sh build.sh [work dir]
# Writes flare.js and flare.wasm next to this script, and the files for R2 to
# <work dir>/r2/flare/ (upload them to wasmgame-files under the same keys).
# For a newer engine, copy this folder to flare-<new rev>-p1 and change the
# revisions below; a change to the build here takes the next -p number.
set -eu

FLARE_ENGINE_REV=1969baeb89ebf94cf22e2bc2ed4e3d8413714d4f
FLARE_GAME_REV=af6eee6d339ac98011864bfe89da837fe7769c28
EMSDK_VERSION=4.0.7

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

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
port sdl2_ttf libsdl-org/SDL_ttf release-2.20.2 SDL_ttf-release-2.20.2 https://github.com/libsdl-org/SDL_ttf/archive/release-2.20.2.zip
port sdl2_mixer libsdl-org/SDL_mixer release-2.8.0 SDL_mixer-release-2.8.0 https://github.com/libsdl-org/SDL_mixer/archive/release-2.8.0.zip
port freetype freetype/freetype VER-2-13-3 freetype-VER-2-13-3 https://github.com/freetype/freetype/archive/VER-2-13-3.zip
port harfbuzz harfbuzz/harfbuzz 3.2.0 harfbuzz-3.2.0 https://github.com/harfbuzz/harfbuzz/releases/download/3.2.0/harfbuzz-3.2.0.tar.xz
port ogg emscripten-ports/ogg version_1 Ogg-version_1 https://github.com/emscripten-ports/ogg/archive/version_1.zip
port vorbis emscripten-ports/vorbis version_1 Vorbis-version_1 https://github.com/emscripten-ports/vorbis/archive/version_1.zip

# checkout <dir> <repo> <rev>
checkout() {
  [ -d "$1" ] || git clone -q "https://github.com/$2.git" "$1"
  git -C "$1" checkout -q -f "$3"
  git -C "$1" clean -q -fdx
}
checkout "$work/flare-engine" flareteam/flare-engine $FLARE_ENGINE_REV
checkout "$work/flare-game" flareteam/flare-game $FLARE_GAME_REV

cd "$work/flare-engine"
git apply "$here/flare.diff"
emcc -Isrc/ src/*.cpp -O3 \
  -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=4GB -sSTACK_SIZE=1MB -sENVIRONMENT=web \
  -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 '-sSDL2_IMAGE_FORMATS=["png","jpg"]' -sUSE_SDL_TTF=2 \
  -sUSE_SDL_MIXER=2 '-sSDL2_MIXER_FORMATS=["ogg"]' \
  -sINVOKE_RUN=0 '-sEXPORTED_RUNTIME_METHODS=["FS","callMain","UTF8ToString"]' -sEXPORTED_FUNCTIONS=_main,_wg_resize \
  -o "$work/flare.js"
cp "$work/flare.js" "$work/flare.wasm" "$here/"

python3 "$here/pack.py" "$work/flare-engine/mods" "$work/flare-game/mods" "$work/r2" "$here/pack-start.txt"
ls -l "$work/r2/flare" | head
