#!/bin/bash
#
# download_sources.sh — Download and extract hiperiso external sources
# ---------------------------------------------------------------------------
# Downloads: Linux kernel, QEMU, EDK2 (for OVMF), GRUB2.
# Version numbers are variables (override via environment) — never hardcoded.
#
# Usage:  ./download_sources.sh [build_dir]
#
set -euo pipefail

# ── Version variables (override with e.g. KERNEL_VERSION=6.1 ./download_sources.sh) ──
KERNEL_VERSION="${KERNEL_VERSION:-6.12}"
QEMU_VERSION="${QEMU_VERSION:-9.0.0}"
EDK2_TAG="${EDK2_TAG:-edk2-stable202405}"
GRUB2_VERSION="${GRUB2_VERSION:-2.12}"
BUSYBOX_VERSION="${BUSYBOX_VERSION:-1.36.1}"

# ── Source URLs ─────────────────────────────────────────────────────────────
KERNEL_MAJOR="${KERNEL_VERSION%%.*}"
KERNEL_URL="https://cdn.kernel.org/pub/linux/kernel/v${KERNEL_MAJOR}.x/linux-${KERNEL_VERSION}.tar.xz"
QEMU_URL="https://download.qemu.org/qemu-${QEMU_VERSION}.tar.xz"
EDK2_URL="https://github.com/tianocore/edk2.git"
GRUB2_URL="https://ftpmirror.gnu.org/grub/grub-${GRUB2_VERSION}.tar.xz"
BUSYBOX_URL="https://busybox.net/downloads/busybox-${BUSYBOX_VERSION}.tar.bz2"

# ── Directories ─────────────────────────────────────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
BUILD_DIR="${1:-$REPO_ROOT/build}"
DL_DIR="$BUILD_DIR/downloads"
mkdir -p "$DL_DIR"

# Download and extract one tarball, renaming the top dir to a canonical name.
# Usage: fetch <url> <archive_name> <extracted_topdir> <canonical_dir>
fetch() {
    local url="$1" archive="$2" topdir="$3" canonical="$4"
    local target="$BUILD_DIR/$canonical"
    if [ -d "$target" ]; then
        echo "  [skip] $canonical already present"
        return 0
    fi
    echo "  [download] $url"
    if [ ! -s "$DL_DIR/$archive" ]; then
        rm -f "$DL_DIR/$archive"
        if ! wget -c -q --show-progress -O "$DL_DIR/$archive" "$url"; then
            rm -f "$DL_DIR/$archive"
            echo "ERROR: failed to download $archive from $url" >&2
            return 1
        fi
    fi
    echo "  [extract]  $archive -> $canonical"
    rm -rf "$target" "$BUILD_DIR/$topdir"
    tar -xf "$DL_DIR/$archive" -C "$BUILD_DIR"
    mv "$BUILD_DIR/$topdir" "$target"
}

echo "=== hiperiso: downloading sources into $BUILD_DIR ==="

fetch "$KERNEL_URL"  "linux-${KERNEL_VERSION}.tar.xz"     "linux-${KERNEL_VERSION}"     "linux"
fetch "$QEMU_URL"    "qemu-${QEMU_VERSION}.tar.xz"        "qemu-${QEMU_VERSION}"        "qemu"

# EDK2 upstream submodules are not fully reliable for reproducible CI-style
# clones. We shallow-clone the main tree only and let the build step fall back
# to a system OVMF if an in-tree OVMF build cannot complete.
EDK2_TARGET="$BUILD_DIR/edk2"
if [ -d "$EDK2_TARGET" ]; then
    echo "  [skip] edk2 already present"
else
    echo "  [clone] $EDK2_URL ($EDK2_TAG, shallow clone)"
    if ! git -c advice.detachedHead=false clone --depth 1 \
        --branch "$EDK2_TAG" \
        "$EDK2_URL" "$EDK2_TARGET"; then
        rm -rf "$EDK2_TARGET"
        echo "ERROR: failed to clone edk2 ($EDK2_TAG) from $EDK2_URL" >&2
        exit 1
    fi
fi

fetch "$GRUB2_URL"   "grub-${GRUB2_VERSION}.tar.xz"       "grub-${GRUB2_VERSION}"       "grub2"

GRUB2_204_URL="https://ftp.gnu.org/gnu/grub/grub-2.04.tar.xz"
if [ ! -f "$DL_DIR/grub-2.04.tar.xz" ]; then
    echo "  [download] $GRUB2_204_URL"
    wget -q -O "$DL_DIR/grub-2.04.tar.xz" "$GRUB2_204_URL" || {
        echo "  [warn] grub-2.04 download failed (needed for full module build)"
    }
else
    echo "  [skip] grub-2.04.tar.xz already present"
fi

fetch "$BUSYBOX_URL" "busybox-${BUSYBOX_VERSION}.tar.bz2" "busybox-${BUSYBOX_VERSION}" "busybox"

echo "=== Sources ready in $BUILD_DIR ==="
