#!/bin/sh
set -e

_SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
HIPERISO_ROOT="$(cd "$_SCRIPT_DIR/.." && pwd)"
GRUB2_INSTALL="$HIPERISO_ROOT/build/grub2-204/INSTALL"
PAYLOAD="$HIPERISO_ROOT/build/payload"
STAGING="$HIPERISO_ROOT/build/staging"

# ── Dependency checks ─────────────────────────────────────────────
command -v mcopy >/dev/null 2>&1 || { echo "ERROR: mtools (mmd/mcopy) required to build ESP image"; exit 1; }
command -v mkfs.vfat >/dev/null 2>&1 || { echo "ERROR: dosfstools (mkfs.vfat) required"; exit 1; }

GRUB_X64_DIR="$GRUB2_INSTALL/lib/grub/x86_64-efi"
[ -d "$GRUB_X64_DIR" ] || GRUB_X64_DIR="$HIPERISO_ROOT/src/grub2/grub/x86_64-efi"

KERNEL_SRC="$STAGING/efi/vmlinuz"
[ -f "$KERNEL_SRC" ] || KERNEL_SRC="$STAGING/vmlinuz"

EFI_BOOT_SRC="$STAGING/efi/BOOTX64.EFI"
[ -f "$EFI_BOOT_SRC" ] || EFI_BOOT_SRC="$STAGING/EFI/BOOT/BOOTX64.EFI"

# Clean only subdirectories this script fully repopulates.
# Preserve tool/ (languages.json, HiperisoGTK.glade, GUI binaries)
# and WebUI/plugson.www from build_gui_all.sh.
rm -rf "$PAYLOAD"/boot "$PAYLOAD"/config "$PAYLOAD"/hiperiso "$PAYLOAD"/EFI "$PAYLOAD"/grub
mkdir -p "$PAYLOAD/boot"
mkdir -p "$PAYLOAD/config"
mkdir -p "$PAYLOAD/hiperiso"
mkdir -p "$PAYLOAD/EFI/BOOT"
mkdir -p "$PAYLOAD/EFI/hiperiso/trace"
mkdir -p "$PAYLOAD/grub"
mkdir -p "$PAYLOAD/tool/x86_64"

# ── boot/ (BIOS boot images, vendored) ────────────────────────────
cp "$HIPERISO_ROOT/vendor/grub-i386-pc/boot.img" "$PAYLOAD/boot/boot.img"
cp "$HIPERISO_ROOT/vendor/grub-i386-pc/core.img" "$PAYLOAD/boot/core.img"
xz --check=crc32 "$PAYLOAD/boot/core.img"

# ── EFI/ (our custom GRUB2 EFI + hypervisor payloads) ───────────────
cp "$EFI_BOOT_SRC" "$PAYLOAD/EFI/BOOT/"
cp "$HIPERISO_ROOT/src/grub2/grub/grub.cfg" "$PAYLOAD/EFI/BOOT/"

cp "$KERNEL_SRC" "$PAYLOAD/EFI/hiperiso/vmlinuz"
cp "$STAGING/initramfs.cpio.gz" "$PAYLOAD/EFI/hiperiso/"
cp "$STAGING/OVMF.fd" "$PAYLOAD/EFI/hiperiso/"
cp "$STAGING/hiperiso-log" "$PAYLOAD/EFI/hiperiso/"
cp "$HIPERISO_ROOT/logging/trace-"*.events "$PAYLOAD/EFI/hiperiso/trace/"

# ── grub/ (configs, themes, modules from GRUB2 2.04 build) ──────────
cp "$HIPERISO_ROOT/src/grub2/grub/grub.cfg" "$PAYLOAD/grub/"
cp -a "$GRUB_X64_DIR" "$PAYLOAD/grub/x86_64-efi"

mkdir -p "$PAYLOAD/grub/i386-pc"
cp "$HIPERISO_ROOT/vendor/grub-i386-pc/"*.lst "$PAYLOAD/grub/i386-pc/" 2>/dev/null || true
cp "$HIPERISO_ROOT/vendor/grub-i386-pc/"*.mod "$PAYLOAD/grub/i386-pc/" 2>/dev/null || true

for cfg in checksum.cfg debug.cfg hwinfo.cfg keyboard.cfg localboot.cfg menulang.cfg power.cfg; do
    cp "$HIPERISO_ROOT/src/grub2/grub/$cfg" "$PAYLOAD/grub/" 2>/dev/null || true
done
for dir in distro help menu themes fonts; do
    cp -a "$HIPERISO_ROOT/src/grub2/grub/$dir" "$PAYLOAD/grub/" 2>/dev/null || true
done

if [ -d "$PAYLOAD/grub/help" ]; then
    tar -C "$PAYLOAD/grub" -czf "$PAYLOAD/grub/help.tar.gz" help
fi

if [ -d "$PAYLOAD/grub/menu" ]; then
    tar -C "$PAYLOAD/grub" -czf "$PAYLOAD/grub/menu.tar.gz" menu
fi

# ── tool/ (installer scripts + vendored assets + binary tools) ─────
cp "$HIPERISO_ROOT/src/installer/tool/hiperiso_lib.sh" "$PAYLOAD/tool/"
cp "$HIPERISO_ROOT/src/installer/tool/HiperisoWorker.sh" "$PAYLOAD/tool/"
cp "$HIPERISO_ROOT/src/installer/tool/create_hiperiso_iso_part_dm.sh" "$PAYLOAD/tool/"
cp "$HIPERISO_ROOT/assets/languages.json" "$PAYLOAD/tool/languages.json"
cp "$HIPERISO_ROOT/assets/HiperisoGTK.glade" "$PAYLOAD/tool/HiperisoGTK.glade"

for tool in ash hexdump mkexfatfs mount.exfat-fuse xzcat; do
    cp "$HIPERISO_ROOT/vendor/tool-x86_64/$tool" "$PAYLOAD/tool/x86_64/"
    chmod +x "$PAYLOAD/tool/x86_64/$tool"
done

# ── Top-level scripts ───────────────────────────────────────────────
cp "$HIPERISO_ROOT/src/installer/Hiperiso.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/HiperisoQt.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/HiperisoGtk.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/HiperisoWeb.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/HiperisoPlugson.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/Hiperiso2Disk.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/CreatePersistentImg.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/ExtendPersistentImg.sh" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/hisocli" "$PAYLOAD/"
cp "$HIPERISO_ROOT/src/installer/hisolnk" "$PAYLOAD/"

# ── config/ ─────────────────────────────────────────────────────────
cp "$HIPERISO_ROOT/config/hiperiso.json.example" "$PAYLOAD/config/"

# ── version ─────────────────────────────────────────────────────────
echo "1.0.0" > "$PAYLOAD/hiperiso/version"

rm -f "$PAYLOAD/log.txt"

sed -i 's/ventoy/hiperiso/g' "$PAYLOAD/grub/i386-pc/moddep.lst" 2>/dev/null || true
sed -i 's/ventoy/hiperiso/g' "$PAYLOAD/grub/x86_64-efi/moddep.lst" 2>/dev/null || true

# ── Build ESP disk image (32MB FAT16, what installer writes to Part 2) ──
ESP_IMG="$PAYLOAD/hiperiso/hiperiso.disk.img"
dd if=/dev/zero of="$ESP_IMG" bs=1M count=32 2>/dev/null
mkfs.vfat -F 16 -n "HISOEFI" "$ESP_IMG" >/dev/null 2>&1

mmd -i "$ESP_IMG" ::/EFI
mmd -i "$ESP_IMG" ::/EFI/BOOT
mmd -i "$ESP_IMG" ::/EFI/hiperiso
mmd -i "$ESP_IMG" ::/EFI/hiperiso/trace
mmd -i "$ESP_IMG" ::/grub
mmd -i "$ESP_IMG" ::/tool
mmd -i "$ESP_IMG" ::/hiperiso
mcopy -i "$ESP_IMG" "$PAYLOAD/EFI/BOOT/BOOTX64.EFI" ::/EFI/BOOT/
mcopy -i "$ESP_IMG" "$PAYLOAD/EFI/BOOT/grub.cfg" ::/EFI/BOOT/
mcopy -i "$ESP_IMG" "$PAYLOAD/EFI/hiperiso/vmlinuz" ::/EFI/hiperiso/
mcopy -i "$ESP_IMG" "$PAYLOAD/EFI/hiperiso/initramfs.cpio.gz" ::/EFI/hiperiso/
mcopy -i "$ESP_IMG" "$PAYLOAD/EFI/hiperiso/OVMF.fd" ::/EFI/hiperiso/
mcopy -i "$ESP_IMG" "$PAYLOAD/EFI/hiperiso/hiperiso-log" ::/EFI/hiperiso/

for f in "$PAYLOAD"/EFI/hiperiso/trace/*.events; do
    mcopy -s -i "$ESP_IMG" "$f" ::/EFI/hiperiso/trace/ 2>/dev/null || true
done

mcopy -i "$ESP_IMG" "$PAYLOAD/grub/grub.cfg" ::/grub/
for cfg in checksum.cfg debug.cfg hwinfo.cfg keyboard.cfg localboot.cfg menulang.cfg power.cfg; do
    [ -f "$PAYLOAD/grub/$cfg" ] && mcopy -i "$ESP_IMG" "$PAYLOAD/grub/$cfg" ::/grub/
done
[ -f "$PAYLOAD/grub/help.tar.gz" ] && mcopy -i "$ESP_IMG" "$PAYLOAD/grub/help.tar.gz" ::/grub/
[ -f "$PAYLOAD/grub/menu.tar.gz" ] && mcopy -i "$ESP_IMG" "$PAYLOAD/grub/menu.tar.gz" ::/grub/
for dir in themes fonts distro help menu; do
    [ -d "$PAYLOAD/grub/$dir" ] && {
        mmd -i "$ESP_IMG" "::/grub/$dir"
        mcopy -s -i "$ESP_IMG" "$PAYLOAD/grub/$dir/." "::/grub/$dir/" 2>/dev/null || true
    }
done

for f in "$PAYLOAD"/tool/x86_64/*; do
    [ -f "$f" ] && mcopy -i "$ESP_IMG" "$f" ::/tool/
done
mcopy -i "$ESP_IMG" "$PAYLOAD/hiperiso/version" ::/hiperiso/

xz --check=crc32 "$ESP_IMG"

echo "=== hiperiso release package assembled ==="
du -sh "$PAYLOAD"
find "$PAYLOAD" -type f | wc -l
echo "files"
