#!/usr/bin/env bash
# Builds PlayAgain's melonDS core: public/cores/melonds/melonds.{js,wasm}.
#
# Reproducible: the same melonDS commit and Emscripten give the same bytes.
# melonDS comes from `git archive` of the pinned commit (a plain tree, no
# .git), built by its own CMake into two static libraries (core, teakra):
# the interpreter, the software renderers, no JIT, no OpenGL, no GDB stub,
# no frontend, no net. The shim is then linked against them. The script
# refuses other Emscripten versions and writes SHA256SUMS beside itself;
# record the wasm's SHA-256 in ../README.md. Run it twice and compare.
#
#   bash public/cores/melonds/source/build.sh
#
# Needs: git, cmake, ninja and a C++ compiler. Emscripten is installed into
# EMSDK_DIR on first run.
set -euo pipefail

MELONDS_COMMIT="497aa6cdd26f45f8064b78916b00a773eab4ded7" # master, 2026-11-04
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/melonds}"

# --- 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; }

# --- melonDS at the pinned commit, as a plain tree ---------------------------
mkdir -p "$WORK"
if [ ! -d "$WORK/git/.git" ]; then
  git clone --quiet https://github.com/melonDS-emu/melonDS.git "$WORK/git"
fi
git -C "$WORK/git" cat-file -e "$MELONDS_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 "$MELONDS_COMMIT" | tar -x -C "$SRC"

# --- libcore + libteakra: interpreter, software renderers, single-threaded ----
MAP="-ffile-prefix-map=$SRC=melonds -ffile-prefix-map=$WORK/build=build -ffile-prefix-map=$HERE=pa"
emcmake cmake -S "$SRC" -B "$WORK/build" -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_QT_SDL=OFF -DENABLE_OGLRENDERER=OFF -DENABLE_GDBSTUB=OFF -DENABLE_JIT=OFF -DENABLE_LTO_RELEASE=ON \
  -DCMAKE_C_FLAGS="-Wno-everything $MAP" -DCMAKE_CXX_FLAGS="-Wno-everything $MAP" >/dev/null
cmake --build "$WORK/build" --target core >/dev/null
CORE_A="$WORK/build/src/libcore.a"
TEAKRA_A="$(find "$WORK/build/src/teakra" -name libteakra.a | head -1)"

# --- the shim, linked against both -------------------------------------------
EXPORTS="_malloc,_free"
for f in init load_rom set_user set_clock power_on reset set_keys set_touch set_lid set_mic_blow push_mic \
         run_frame fb_top fb_bottom read_audio audio_rate frame_rate state_size save_state load_state digest \
         sram_ptr sram_size sram_dirty load_sram rumble stop_reason mp_set_instance mp_push_rx mp_pop_tx \
         set_bios bios_info; do
  EXPORTS="$EXPORTS,_pa_$f"
done

# shellcheck disable=SC2086
em++ "$HERE/pa_melonds.cpp" "$CORE_A" "$TEAKRA_A" \
  -I"$SRC/src" -I"$WORK/build/src" \
  -std=c++17 -fwrapv -O3 -flto -DNDEBUG -Wno-everything $MAP \
  -sMODULARIZE=1 -sEXPORT_NAME=createMelondsModule -sENVIRONMENT=web,worker \
  -sFILESYSTEM=0 -sMALLOC=emmalloc -sALLOW_MEMORY_GROWTH=1 \
  -sINITIAL_MEMORY=134217728 -sSTACK_SIZE=2097152 \
  -sEXPORTED_FUNCTIONS="$EXPORTS" -sEXPORTED_RUNTIME_METHODS=HEAPU8,HEAP16,HEAPU32 \
  -o "$OUT/melonds.js"

cp "$SRC/LICENSE" "$OUT/LICENSE.melonds.txt"
(cd "$HERE" && shasum -a 256 build.sh pa_melonds.cpp ../melonds.js ../melonds.wasm > SHA256SUMS)
ls -l "$OUT/melonds.js" "$OUT/melonds.wasm"
shasum -a 256 "$OUT/melonds.wasm"
