#!/usr/bin/env bash
# Plan 46 work item 7 (determinism L4): an exported release build replays a debug build's recorded
# match and compares every hash (MwNetRecording: the rink every pass, the session at every screen
# change) - the one check that a debug and a release build agree (a
# GDScript runtime error, an assert, OS.is_debug_build in lockstep code would part them).
#   tools/bin/release-replay [TICKS=20000] [live_match options]      (net-determinism's options)
# The recording: the editor's Godot (a debug build) plays a local match with live_match.gd's bots on
# the pads into out/net/release.mwnet, and replays it once itself. The replay: this checkout's HEAD
# (commit first: the working tree's changes aren't in it) exported with the "Linux" preset as a
# release build with test/ kept and game/test/screens/release_replay.gd as its main scene (official
# export templates run no -s script), run headless on that file, the ROM named on its command line
# (--rom=), its user data in a scratch folder. The shipped build is untouched.
# Output: out/builds/release-replay/ (the build), out/net/release-replay.log.
source "$(dirname "${BASH_SOURCE[0]}")/_common.sh"
set -uo pipefail
ticks=20000
if [[ $# -gt 0 && "$1" != --* ]]; then ticks="$1"; shift; fi
opts=("$@")
[[ ${#opts[@]} -eq 0 ]] && opts=(--humans "1+2.3")
[[ -z "$(git -C "$MW_ROOT" status --porcelain -- game)" ]] \
  || echo "release-replay: game/ has changes not committed - the release build is HEAD's" >&2
mkdir -p "$MW_ROOT/out/net"
file="$MW_ROOT/out/net/release.mwnet"
log="$MW_ROOT/out/net/release-replay.log"
rom="$MW_ROOT/game/rom/mlh.gen"
filter='^Godot Engine\|ObjectDB instances\|resources still in use\|^   at: \(cleanup\|clear\)\|^\s*$'
proof=(-s res://test/screens/net_determinism.gd --)
"$(dirname "${BASH_SOURCE[0]}")/godot-headless" "${proof[@]}" --record "$file" --ticks "$ticks" "${opts[@]}" \
  2>&1 | grep -v "$filter" | grep -E '^recorded|SCRIPT ERROR|Parse Error'
[[ ${PIPESTATUS[0]} -eq 0 ]] || { echo "release-replay: the recording failed"; exit 1; }
echo "debug:   $("$(dirname "${BASH_SOURCE[0]}")/godot-headless" "${proof[@]}" --replay "$file" 2>&1 | grep '^RESULT')"

# the release build: HEAD's game/, test/ kept, the proof's replay its main scene
builds="$MW_ROOT/out/builds"
src="$builds/src-release-replay"
out="$builds/release-replay"
rm -rf "$src" "$out"; mkdir -p "$src" "$out"
git -C "$MW_ROOT" archive HEAD game | tar -x -C "$src"
sed -i 's|^exclude_filter="test/\*, |exclude_filter="|' "$src/game/export_presets.cfg"
cat > "$src/game/test/screens/release_replay.tscn" <<'SCENE'
[gd_scene format=3]

[ext_resource type="Script" path="res://test/screens/release_replay.gd" id="1"]

[node name="ReleaseReplay" type="Node"]
script = ExtResource("1")
SCENE
sed -i 's|^run/main_scene=.*|run/main_scene="res://test/screens/release_replay.tscn"|' "$src/game/project.godot"
for so in "$MW_ROOT"/game/addons/mw_audio/bin/libmw_audio.linux.template_release.x86_64.so \
    "$MW_MAIN"/game/addons/mw_audio/bin/libmw_audio.linux.template_release.x86_64.so; do
  if [[ -f "$so" ]]; then mkdir -p "$src/game/addons/mw_audio/bin"; cp "$so" "$src/game/addons/mw_audio/bin/"; break; fi
done
godot="$(mw_godot)" || exit 1
tpl="$(dirname "$godot")/editor_data/export_templates/$GODOT_VERSION.stable"
[[ -f "$tpl/linux_release.x86_64" ]] || "$MW_TOOLS/setup/fetch_tools.sh" templates
bin="$(mw_godot_headless)" || exit 1
mkdir -p "$(dirname "$bin")/editor_data/export_templates"
ln -sfn "$tpl" "$(dirname "$bin")/editor_data/export_templates/$GODOT_VERSION.stable"
"$bin" --headless --path "$src/game" --import > "$builds/import-release-replay.log" 2>&1 || true
"$bin" --headless --path "$src/game" --export-release "Linux" "$out/mightyweenie.x86_64" \
  > "$builds/export-release-replay.log" 2>&1
[[ -s "$out/mightyweenie.x86_64" ]] || { tail -20 "$builds/export-release-replay.log"; echo "release-replay: the export failed"; exit 1; }
chmod +x "$out/mightyweenie.x86_64"
mkdir -p "$out/userdata"
# (APPIMAGE unset: one inherited from a launcher that is an AppImage would be taken for the game's own)
env -u APPIMAGE XDG_DATA_HOME="$out/userdata" timeout 900 "$out/mightyweenie.x86_64" --headless -- \
  --replay "$file" --rom="$rom" > "$log" 2>&1
status=$?
grep '^release-replay:' "$log"
echo "release: $(grep '^RESULT' "$log" || echo "no result (out/net/release-replay.log)")"
exit "$status"
