#!/usr/bin/env bash
# Install GNO/ME 2.0.6 prerequisites needed by demos/buildGno.sh and
# scripts/runInGno.sh:
#   - tools/cadius/cadius                (built via installCadius.sh)
#   - tools/gno/dist/*.shk               (18 GNO Base Distribution archives)
#   - tools/gsos/6.0.4 - *.po            (3 Apple IIgs System 6.0.4 disks)
#
# nulib2 (used to extract the .shk archives during disk assembly) is
# installed by scripts/installDeps.sh.  After this completes, run
# `bash tools/gno/buildDisk.sh` once to assemble tools/gno/gnobase.po.
#
# Upstream sources are not redistributed; this script fetches them on
# first run from the URLs below.  Both archives have been mirrored at
# multiple sites for decades — if curl 404s, set GNO_DIST_URL or
# GSOS_DISK_URL to a working mirror and re-run.
#
# See docs/USAGE.md "Running under GNO/ME" for the build-and-run flow.

set -euo pipefail
source "$(dirname "$0")/common.sh"

# ---- Upstream URLs (overridable via env) ---------------------------------
# GNO/ME Base Distribution v2.0.6 (Devin Reade, 1999-02-15).  The original
# gno.org distribution path; if it's down, an Internet Archive item or any
# Apple II archive mirror will serve the same files.
: "${GNO_DIST_URL:=http://www.gno.org/pub/apple2/gs.specific/gno/base/v206.19990215}"

# Apple IIgs System 6.0.4 PO disk images, mirrored at apple2.org.za (used
# by feedback_gsos_604_installed).  System.Disk.po is required by
# runInGno.sh; the Fonts disks are pulled along since toolbox demos load
# fonts from them.
: "${GSOS_DISK_URL:=https://mirrors.apple2.org.za/Apple%20II%20Documentation%20Project/Apple%20II%20Operating%20Systems/Apple%20IIGS/Apple%20IIGS%20System%20Software/Apple%20IIGS%20System%206.0.4/PO%20Disk%20Images}"


# ---- 1. cadius (delegated to installCadius.sh) ---------------------------
CADIUS_BIN="$TOOLS_DIR/cadius/cadius"
if [ ! -x "$CADIUS_BIN" ]; then
    log "cadius missing — invoking installCadius.sh"
    bash "$(dirname "$0")/installCadius.sh"
else
    log "cadius already installed at $CADIUS_BIN"
fi


# ---- 2. nulib2 (sanity-check; installed by installDeps.sh) ---------------
if ! haveCmd nulib2; then
    die "nulib2 not in PATH; run scripts/installDeps.sh first (or \`sudo apt-get install nulib2\`)"
fi


# ---- 3. GNO/ME 2.0.6 .shk archives ---------------------------------------
GNO_DIST="$PROJECT_ROOT/tools/gno/dist"
install -d "$GNO_DIST"
GNO_SHK_STEMS=(
    gno.01 gno.02 gno.03 gno.04 gno.05 gno.06 gno.07 gno.08
    gno.09 gno.10 gno.11 gno.12 gno.13 gno.14 gno.15 gno.16
    gnoboot gnohfs
)
for stem in "${GNO_SHK_STEMS[@]}"; do
    dest="$GNO_DIST/${stem}.shk"
    if [ -s "$dest" ]; then
        continue
    fi
    log "downloading ${stem}.shk"
    if ! curl -fL --retry 3 --progress-bar -o "$dest.part" "$GNO_DIST_URL/${stem}.shk"; then
        rm -f "$dest.part"
        die "failed to fetch ${stem}.shk from $GNO_DIST_URL — set GNO_DIST_URL=<mirror> and re-run"
    fi
    mv "$dest.part" "$dest"
done


# ---- 4. GS/OS 6.0.4 system disks -----------------------------------------
GSOS_DIR="$PROJECT_ROOT/tools/gsos"
install -d "$GSOS_DIR"
GSOS_DISKS=("System.Disk" "Fonts" "Fonts2")
for disk in "${GSOS_DISKS[@]}"; do
    dest="$GSOS_DIR/6.0.4 - ${disk}.po"
    if [ -s "$dest" ]; then
        continue
    fi
    log "downloading 6.0.4 - ${disk}.po"
    # Disk filenames contain spaces and a hyphen — URL-encode the same way
    # the mirror serves them (space -> %20).
    src="$GSOS_DISK_URL/6.0.4%20-%20${disk}.po"
    if ! curl -fL --retry 3 --progress-bar -o "$dest.part" "$src"; then
        rm -f "$dest.part"
        die "failed to fetch 6.0.4 - ${disk}.po from $GSOS_DISK_URL — set GSOS_DISK_URL=<mirror> and re-run"
    fi
    mv "$dest.part" "$dest"
done


log "GNO/ME 2.0.6 + GS/OS 6.0.4 prerequisites installed"
log "  • $TOOLS_DIR/cadius/cadius"
log "  • $GNO_DIST/*.shk  (18 archives)"
log "  • $GSOS_DIR/6.0.4 - *.po  (3 system disks)"
log "next: bash tools/gno/buildDisk.sh   # one-time, assembles gnobase.po"
log "then: bash demos/buildGno.sh <name>  # see docs/USAGE.md \"Running under GNO/ME\""
