#!/usr/bin/env bash
# make-agi-iigs-disk.sh - Build the 2MB ProDOS data volume that holds the
# IIgs AGI interpreter plus one AGI v2 game's resources. run-iigs.sh mounts
# it as the CFFA2 card's second partition (-hard2); GS/OS boots from the
# first partition and auto-mounts this one.
#
# A single 800KB 3.5" floppy can't hold the ~240KB AGI binary plus KQ3's
# ~644KB of required resources (878KB total), and MAME's IIgs 3.5 drive is
# 800K-GCR-only (it rejects a 1.44MB image). A CFFA2 hard-disk partition
# has no such cap, so the whole payload lands on one 2MB ProDOS volume.
#
# Usage: scripts/make-agi-iigs-disk.sh <output.2mg> [game-name]
#   game-name : subdirectory under examples/agi/gamedata/ (default kq3)
# Requires: toolchains/env.sh sourced (LLVM816_ROOT), built build/iigs/bin/AGI.
set -euo pipefail

repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
: "${LLVM816_ROOT:?make-agi-iigs-disk.sh: source toolchains/env.sh first}"
CADIUS="${CADIUS:-$LLVM816_ROOT/tools/cadius/cadius}"
BINDIR="${BINDIR:-$repo/build/iigs/bin}"

out=${1:?usage: make-agi-iigs-disk.sh <output.2mg> [game]}
game=${2:-kq3}
gamedata=$repo/examples/agi/gamedata/$game
agiBin=$BINDIR/AGI
VOL=AGI

# The AGI volume name and the game files use uppercase ProDOS names. AGI
# fopen()s LOGDIR / VOL.x etc. from its launch directory; GS/OS sets the
# prefix to the volume it launched from, and ProDOS lookups are
# case-insensitive, so the uppercase names match the lowercase fopen paths.
[ -x "$CADIUS" ]     || { echo "make-agi-iigs-disk.sh: cadius not found at $CADIUS" >&2; exit 2; }
[ -f "$agiBin" ]     || { echo "make-agi-iigs-disk.sh: $agiBin not built (make -f make/iigs.mk $agiBin)" >&2; exit 1; }
[ -d "$gamedata" ]   || { echo "make-agi-iigs-disk.sh: $gamedata is not an AGI v2 game directory" >&2; exit 1; }
[ -f "$gamedata/LOGDIR" ] || { echo "make-agi-iigs-disk.sh: $gamedata has no LOGDIR -- not AGI v2" >&2; exit 1; }

work=$(mktemp -d -t agi-iigs-disk.XXXXXX)
trap 'rm -rf "$work"' EXIT

rm -f "$out"
"$CADIUS" CREATEVOLUME "$out" "$VOL" 2048KB >/dev/null

# AGI interpreter as a ProDOS type $B3 (S16 / GS-OS application). It stays
# at the volume root so Finder lists and launches it.
cp "$agiBin" "$work/AGI#B30000"
"$CADIUS" ADDFILE "$out" "/$VOL" "$work/AGI#B30000" >/dev/null

# Game resources live under DATA/ -- JoeyLib forces every data-file open
# through that prefix (jlDataOpen), so they must sit in /VOL/DATA. GS/OS
# sets prefix 0 to the launch volume, so AGI's "DATA/LOGDIR" resolves here.
"$CADIUS" CREATEFOLDER "$out" "/$VOL/DATA" >/dev/null

# Game resources as plain ProDOS BIN ($06). Only the subset AGI v2 reads
# (the four DIR indexes, the VOL.x archives, OBJECT, WORDS.TOK) is copied;
# Sierra's original binary / OVL drivers are left behind.
added=0
for f in LOGDIR PICDIR VIEWDIR SNDDIR OBJECT WORDS.TOK; do
    if [ -f "$gamedata/$f" ]; then
        cp "$gamedata/$f" "$work/${f}#060000"
        "$CADIUS" ADDFILE "$out" "/$VOL/DATA" "$work/${f}#060000" >/dev/null
        added=$((added + 1))
    fi
done
for f in "$gamedata"/VOL.*; do
    [ -f "$f" ] || continue
    b=$(basename "$f")
    cp "$f" "$work/${b}#060000"
    "$CADIUS" ADDFILE "$out" "/$VOL/DATA" "$work/${b}#060000" >/dev/null
    added=$((added + 1))
done

free=$("$CADIUS" CATALOG "$out" 2>/dev/null | grep -oE 'Free : [0-9]+' | grep -oE '[0-9]+' | head -1)
echo "agi-iigs-disk: $out  (volume /$VOL, game $game)"
echo "  AGI + $added resource files, $free blocks free"
