#!/bin/bash
# The game folder for friends on Windows, pointed at a server, zipped:
#   deploy/package-windows [host]   (default: deploy/eu.host) -> dist/Zone4-<host>.zip
# It is game-589 as the Mac plays it (the patched Zone4Local.exe, the English text, the fixed UI
# images and lua), with every service in the server list at <host> on the game's own ports, the
# stage files as the game shipped them (not the dev panel's enemy HP), and Play.bat to start it.
# Left out: the official launcher, the unpatched Zone4.exe, crash dumps, screenshots and the
# patch tools' .orig/.thai/.rules copies. MANIFEST.txt (deploy/manifest.py) lists what every file is
# against an official 589 install. dist/Zone4/ stays as the staging folder: its files are hard links
# into game-589 where they are the same.
set -euo pipefail
ROOT=$(cd "$(dirname "$0")/.." && pwd)
HOST=${1:-$(cat "$ROOT/deploy/eu.host" 2>/dev/null)} || true
[ -n "$HOST" ] || { echo "usage: deploy/package-windows <server IPv4> (or put it in deploy/eu.host)"; exit 1; }
G="$ROOT/game-589" B="$ROOT/game-589/.enemy-hp-backup"
OUT="$ROOT/dist/Zone4" ZIP="$ROOT/dist/Zone4-$HOST.zip"
mkdir -p "$OUT"

changed=$(cd "$B" 2>/dev/null && find . -type f ! -name mode | sed 's|^\.||') || true
rsync -a --delete --link-dest="$G" \
  --exclude '/Zone4Launcher*' --exclude /LauncherData --exclude /Zone4.exe --exclude /Zone4Local-noLAA.exe \
  --exclude '*.dmp' --exclude /Screenshots --exclude '*.orig' --exclude '*.thai' --exclude '*.rules' \
  --exclude /Zone4Local.exe.json --exclude .enemy-hp-backup --exclude .DS_Store \
  --exclude /Zone4_LIST_THAI.dat --exclude /Zone4_LIST_P_THAI.dat --exclude /Play.bat --exclude /README.txt \
  --exclude /MANIFEST.txt --exclude /SHA256SUMS.txt \
  --exclude-from=<(printf '%s\n' "$changed") \
  "$G/" "$OUT/"
[ -z "$changed" ] || rsync -a --exclude mode "$B/" "$OUT/"

# The server list: 2 tables x 6 services x 3 hosts, each u32 length + C string; the ports
# (u32 length + ":port" per service) as the game shipped them. Written as new files, never into
# a hard link.
python3 - "$OUT" "$HOST" "$G/Zone4_LIST_P_THAI.dat.orig" "$G/Zone4_LIST_P_THAI.dat" <<'EOF'
import os, shutil, struct, sys
out, host, ports, ports_now = sys.argv[1:]
h = host.encode() + b"\0"
for name in ("Zone4_LIST_THAI.dat", "Zone4_LIST_P_THAI.dat"):
    if os.path.exists(os.path.join(out, name)):
        os.remove(os.path.join(out, name))
open(os.path.join(out, "Zone4_LIST_THAI.dat"), "wb").write((struct.pack("<I", len(h)) + h) * 36)
shutil.copyfile(ports if os.path.exists(ports) else ports_now, os.path.join(out, "Zone4_LIST_P_THAI.dat"))
EOF

printf '@echo off\r\ncd /d "%%~dp0"\r\nstart "" Zone4Local.exe -thai-exe-\r\n' > "$OUT/Play.bat"
SUM=$(shasum -a 256 "$OUT/Zone4Local.exe" | cut -d' ' -f1)
sed 's/$/\r/' > "$OUT/README.txt" <<EOF
Zone4 (Thai client 589, in English) for our own server at $HOST.

1. Unzip this folder somewhere you can write, like C:\\Games\\Zone4 (not Program Files).
2. If the game says a DLL is missing, run source\\VC_redist.x86.exe, then source\\dxsetup\\DXSETUP.exe.
3. Make your account with the sign-up link you were sent.
4. Double-click Play.bat and log in with that account.

Windows may say it protected your PC, because the game isn't signed: More info, then Run anyway.
Zone4Local.exe's SHA-256 is $SUM

MANIFEST.txt says what every file here is: the official game's own, or what we changed or added and
with which script (the scripts are in the source folder shared with this one).
EOF
python3 "$ROOT/deploy/manifest.py" "$OUT" "$HOST"

rm -f "$ZIP"
(cd "$ROOT/dist" && zip -qr -X "$ZIP" Zone4)
echo "$ZIP ($(du -h "$ZIP" | cut -f1)), Zone4Local.exe SHA-256 $SUM"
