#!/bin/bash
# Script to fix the bootloader on our existing ISO

set -e

echo "Fixing bootloader on existing ISO..."

# Check if ISO exists
if [ ! -f "build/debian-atomic-installer.iso" ]; then
    echo "Error: ISO file not found at build/debian-atomic-installer.iso"
    echo "Please create the ISO first with: just create-iso"
    exit 1
fi

echo "Original ISO found: $(ls -lh build/debian-atomic-installer.iso)"

# Create a working directory
mkdir -p build/iso-fix
cd build/iso-fix

# Extract the existing ISO using podman
echo "Extracting existing ISO using podman..."
podman run --rm \
    -v "$(pwd):/work" \
    -v "$(pwd)/../debian-atomic-installer.iso:/iso/debian-atomic-installer.iso:ro" \
    -w /work \
    debian:trixie \
    bash -c "
        apt-get update
        apt-get install -y xorriso
        xorriso -indev /iso/debian-atomic-installer.iso -osirrox on -extract / .
    "

# Install GRUB in a Debian container
echo "Installing GRUB bootloader components..."
podman run --rm \
    -v "$(pwd):/work" \
    -w /work \
    debian:trixie \
    bash -c "
        # Configure apt-cacher-ng proxy
        if [ -n \"$APT_CACHER_NG_PROXY\" ]; then
            echo \"Acquire::http::Proxy \\\"$APT_CACHER_NG_PROXY\\\";\" > /etc/apt/apt.conf.d/99proxy
            echo \"Acquire::https::Proxy \\\"$APT_CACHER_NG_PROXY\\\";\" >> /etc/apt/apt.conf.d/99proxy
        fi
        
        apt-get update
        apt-get install -y grub-efi-amd64 grub-pc-bin grub-common isolinux xorriso
        
        # Create proper bootloader structure
        mkdir -p /work/boot/grub/x86_64-efi
        mkdir -p /work/boot/grub/i386-pc
        mkdir -p /work/isolinux
        
        # Copy GRUB bootloader files
        cp /usr/lib/grub/x86_64-efi/*.mod /work/boot/grub/x86_64-efi/ 2>/dev/null || echo 'EFI modules not found'
        cp /usr/lib/grub/i386-pc/*.mod /work/boot/grub/i386-pc/ 2>/dev/null || echo 'PC modules not found'
        cp /usr/lib/grub/x86_64-efi/grub.efi /work/boot/grub/x86_64-efi/ 2>/dev/null || echo 'EFI grub not found'
        cp /usr/lib/grub/i386-pc/grub.efi /work/boot/grub/i386-pc/ 2>/dev/null || echo 'PC grub not found'
        
        # Create isolinux files for legacy boot
        cp /usr/lib/ISOLINUX/isolinux.bin /work/isolinux/ 2>/dev/null || echo 'isolinux.bin not found'
        cp /usr/lib/syslinux/modules/bios/ldlinux.c32 /work/isolinux/ 2>/dev/null || echo 'ldlinux.c32 not found'
        
        # Create isolinux config
        cat > /work/isolinux/isolinux.cfg << 'EOF'
DEFAULT install
TIMEOUT 50
PROMPT 1

LABEL install
    KERNEL /boot/vmlinuz
    APPEND root=live:CDLABEL=DEBIAN_ATOMIC_INSTALLER quiet initrd=/boot/initrd.img
    
LABEL install-safe
    KERNEL /boot/vmlinuz
    APPEND root=live:CDLABEL=DEBIAN_ATOMIC_INSTALLER nomodeset initrd=/boot/initrd.img
EOF
        
        # Create bootable ISO
        xorrisofs -o ../debian-atomic-installer-bootable.iso \
            -b isolinux/isolinux.bin \
            -c isolinux/boot.cat \
            -boot-info-table \
            -no-emul-boot \
            -boot-load-size 4 \
            -r \
            -V 'DEBIAN_ATOMIC_INSTALLER' \
            /work
    "

cd ../..

echo "Bootable ISO created: build/debian-atomic-installer-bootable.iso"
echo "Size: $(ls -lh build/debian-atomic-installer-bootable.iso | awk '{print $5}')"

echo "✅ Bootloader fix completed!" 