#!/usr/bin/env bash
# Builds PlayAgain's wireless core: public/cores/mgba-wireless/mgba-wireless.{js,wasm},
# one Game Boy Advance core with a Wireless Adapter on its link port (rfu.c,
# ported from gpSP). rfu.c is GPL-2.0-or-later, so this wasm is too, as
# distributed; this folder is its complete corresponding source.
#
# Reproducible: the same mGBA commit, patch and Emscripten give the same
# bytes. mGBA comes from `git archive` of the pinned commit (no .git, and
# SKIP_GIT, so its version string never carries a clone depth or "-dirty").
# The script writes SHA256SUMS beside itself; record the wasm's SHA-256 in
# ../README.md. Run it twice and compare.
#
#   bash public/cores/mgba-wireless/source/build.sh
#
# Needs: git, cmake, ninja, patch and a C compiler. Emscripten is installed
# into EMSDK_DIR on first run.
set -euo pipefail

MGBA_COMMIT="fd9dc5c2cbf234365068855851107e762fc6211b" # master, 2026-10-18
EMSCRIPTEN_VERSION="6.0.12"

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUT="$(cd "$HERE/.." && pwd)"
EMSDK_DIR="${EMSDK_DIR:-$HOME/.cache/playagain/emsdk}"
WORK="${WORK:-$HOME/.cache/playagain/mgba-wireless}"

# --- toolchain ---------------------------------------------------------------
if [ ! -x "$EMSDK_DIR/emsdk" ]; then
  git clone https://github.com/emscripten-core/emsdk.git "$EMSDK_DIR"
fi
"$EMSDK_DIR/emsdk" install "$EMSCRIPTEN_VERSION" >/dev/null
"$EMSDK_DIR/emsdk" activate "$EMSCRIPTEN_VERSION" >/dev/null
# shellcheck disable=SC1091
source "$EMSDK_DIR/emsdk_env.sh" >/dev/null 2>&1
emcc --version | head -1 | grep -q " $EMSCRIPTEN_VERSION " || { echo "need Emscripten $EMSCRIPTEN_VERSION" >&2; exit 1; }

# --- mGBA at the pinned commit, as a plain tree, patched ---------------------
mkdir -p "$WORK"
if [ ! -d "$WORK/git/.git" ]; then
  git clone --quiet https://github.com/mgba-emu/mgba.git "$WORK/git"
fi
git -C "$WORK/git" cat-file -e "$MGBA_COMMIT^{commit}" 2>/dev/null || git -C "$WORK/git" fetch --quiet origin
SRC="$WORK/src"
rm -rf "$SRC" "$WORK/build"
mkdir -p "$SRC"
git -C "$WORK/git" archive --format=tar "$MGBA_COMMIT" | tar -x -C "$SRC"
patch -s -p1 -d "$SRC" < "$HERE/mgba-fd9dc5c.patch"

# --- libmgba: wasm-gaming/mGBA-wasm's feature set, single-threaded ------------
MAP="-ffile-prefix-map=$SRC=mgba -ffile-prefix-map=$WORK/build=build -ffile-prefix-map=$HERE=pa"
emcmake cmake -S "$SRC" -B "$WORK/build" -G Ninja \
  -DCMAKE_BUILD_TYPE=Release -DSKIP_GIT=ON \
  -DCMAKE_C_FLAGS="-O3 -D_GNU_SOURCE -DDISABLE_THREADING $MAP" \
  -DBUILD_STATIC=ON -DBUILD_SHARED=OFF -DDISABLE_FRONTENDS=ON -DDISABLE_DEPS=ON \
  -DBUILD_QT=OFF -DBUILD_SDL=OFF -DBUILD_LIBRETRO=OFF -DBUILD_TEST=OFF -DBUILD_SUITE=OFF \
  -DBUILD_GL=OFF -DBUILD_GLES2=OFF -DBUILD_GLES3=OFF -DUSE_PTHREADS=OFF \
  -DUSE_ZLIB=OFF -DUSE_MINIZIP=OFF -DUSE_LIBZIP=OFF -DUSE_PNG=OFF -DUSE_SQLITE3=OFF \
  -DUSE_FFMPEG=OFF -DUSE_ELF=OFF -DUSE_LZMA=OFF -DUSE_LUA=OFF -DUSE_JSON_C=OFF \
  -DUSE_FREETYPE=OFF -DUSE_EDITLINE=OFF -DUSE_DISCORD_RPC=OFF -DUSE_EPOXY=OFF \
  -DENABLE_SCRIPTING=OFF -DENABLE_DEBUGGERS=OFF >/dev/null
cmake --build "$WORK/build" --target mgba >/dev/null

# --- the shim, against libmgba's own defines (they shape struct mCore) --------
DEFINES=$(grep -m1 '^  DEFINES' "$WORK/build/build.ninja" | cut -d= -f2-)
EXPORTS="_malloc,_free"
for f in init load attach_adapter set_keys run_frame push_packet take_packet last_to disconnect config stats \
         framebuffer read_audio battery_size save_battery frame_rate debug_memory; do
  EXPORTS="$EXPORTS,_mw_$f"
done
# shellcheck disable=SC2086
emcc -O3 -std=gnu11 -I"$SRC/include" -I"$WORK/build/include" \
  -D_GNU_SOURCE -DDISABLE_THREADING -DNDEBUG $DEFINES $MAP -Wno-deprecated-declarations \
  "$HERE/pa_mgba_wireless.c" "$HERE/rfu.c" "$WORK/build/libmgba.a" \
  -sMODULARIZE=1 -sEXPORT_NAME=createMgbaWirelessModule -sENVIRONMENT=web \
  -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=67108864 -sMAXIMUM_MEMORY=536870912 -sSTACK_SIZE=1048576 \
  -sEXPORTED_FUNCTIONS="$EXPORTS" -sEXPORTED_RUNTIME_METHODS=HEAPU8,HEAP16,HEAPU32 \
  -o "$OUT/mgba-wireless.js"

cp "$SRC/LICENSE" "$OUT/LICENSE.mgba.txt"
(cd "$HERE" && shasum -a 256 build.sh mgba-fd9dc5c.patch pa_mgba_wireless.c rfu.c ../mgba-wireless.js ../mgba-wireless.wasm > SHA256SUMS)
ls -l "$OUT/mgba-wireless.js" "$OUT/mgba-wireless.wasm"
shasum -a 256 "$OUT/mgba-wireless.wasm"
