#!/bin/bash
# Build the patched ROM from the base ROM and install it where RetroArch starts it.
# usage: ./build.sh [patch.py flags]     (default flags: r=20)
set -e
. "$(dirname "$0")/config.sh"
mkdir -p "$WORK/backups"
FLAGS="${*:-r=20}"
"$TOOLS/m68kasm.sh" "$TOOLS/ygoalie.s" 2FE040 "$TOOLS/ygoalie.bin" >/dev/null   # fix 9 (Y = goalie)
python3 "$TOOLS/patch.py" "$BASE_ROM" "$WORK/build.bin" $FLAGS
if [ -f "$ROM" ] && ! cmp -s "$ROM" "$WORK/build.bin"; then
  cp "$ROM" "$WORK/backups/nhl94-$(date +%Y%m%d-%H%M%S).bin"
fi
cp "$WORK/build.bin" "$ROM"
# the season ROM (project MEGAdriveHARDJOB) is built on top; the IPS on the site is the season ROM
"$TOOLS/season/build.sh"
python3 "$TOOLS/make_site_data.py"
python3 "$TOOLS/mkips.py" "$BASE_ROM" "$ROMDIR/MEGAdriveHARDJOB/NHL94-season.bin" "$TOOLS/../download/NHL94-v5.1W-jdc-patch.ips"
echo "installed: $ROM  md5 $(md5sum "$ROM" | cut -d' ' -f1)"
echo "season ROM: $ROMDIR/MEGAdriveHARDJOB/NHL94-season.bin  md5 $(md5sum "$ROMDIR/MEGAdriveHARDJOB/NHL94-season.bin" | cut -d' ' -f1)"
