#!ipxe

# Configuración inicial
set ISODIR ogLive

# Detectar si se está ejecutando en modo UEFI o BIOS
iseq ${platform} efi && goto uefi_boot || goto bios_boot

# BIOS Boot Logic
:bios_boot
echo "Running in BIOS mode..."
echo "Starting firstboot and secondboot flow using GRUB4DOS..."
chain http://__SERVERIP__/tftpboot/grub.exe --config-file="
    timeout 0
    set ISODIR=${ISODIR};

    # Firstboot
    find --set-root --ignore-floppies --ignore-cd /ogboot.me checkrange 0x07 parttype > nul;
    cmp /ogboot.me /ogboot.firstboot || goto secondboot;
    write /ogboot.firstboot iniciado;
    pause Firstboot passed. File updated. Press any key to continue...;
    chainloader +1;
    boot;

    # Secondboot
    :secondboot
    find --set-root --ignore-floppies --ignore-cd /ogboot.me checkrange 0x07 parttype > nul;
    cmp /ogboot.me /ogboot.secondboot || goto fallback;
    write /ogboot.secondboot iniciado;
    pause Secondboot passed. File updated. Press any key to continue...;
    chainloader +1;
    boot;

    # Fallback
    :fallback
    pause Firstboot and Secondboot failed. Press any key to continue...;
    quit;

    # OpenGnsys-CACHE
    echo "Booting OpenGnsys-CACHE"
    kernel tftp://__SERVERIP__/${ISODIR}/ogvmlinuz ro boot=oginit quiet splash vga=788 irqpoll acpi=on og2nd=sqfs ogprotocol=smb ogactiveadmin=false ogdebug=false ogupdateinitrd=true ogtmpfs=15 oglivedir=${ISODIR} INFOHOST
    initrd tftp://__SERVERIP__/${ISODIR}/oginitrd.img
    boot || echo "Failed to boot OpenGnsys-CACHE"

    # OpenGnsys-NET
    echo "Booting OpenGnsys-NET"
    kernel tftp://__SERVERIP__/${ISODIR}/ogvmlinuz ro boot=oginit quiet splash vga=788 irqpoll acpi=on og2nd=sqfs ogprotocol=smb ogactiveadmin=false ogdebug=false ogtmpfs=15 oglivedir=${ISODIR} INFOHOST
    initrd tftp://__SERVERIP__/${ISODIR}/oginitrd.img
    boot || echo "Failed to boot OpenGnsys-NET"

    # OpenGnsys-NET Default
    echo "Booting OpenGnsys-NET Default"
    kernel tftp://__SERVERIP__/ogLive/ogvmlinuz ro boot=oginit quiet splash vga=788 irqpoll acpi=on og2nd=sqfs ogprotocol=smb ogactiveadmin=false ogdebug=false ogtmpfs=15 oglivedir=ogLive INFOHOST
    initrd tftp://__SERVERIP__/ogLive/oginitrd.img
    boot || echo "Failed to boot OpenGnsys-NET Default"
exit

# UEFI Boot Logic
:uefi_boot
echo "Running in UEFI mode..."
set timeout 30

# Verificar y buscar cargadores en orden de prioridad
echo "Searching Grub loader..."
sanboot --no-describe --drive 0 --filename \EFI\grub\Boot\grubx64.efi && exit || echo "Grub not found."

echo "Searching rEFInd loader..."
sanboot --no-describe --drive 0 --filename \EFI\refind\shimx64.efi.signed && exit || echo "rEFInd not found."

echo "Searching Part-01-02 loader..."
sanboot --no-describe --drive 0 --filename \EFI\Part-01-02\Boot\ogloader.efi && exit || echo "Part-01-02 not found."

echo "Searching Part-01-03 loader..."
sanboot --no-describe --drive 0 --filename \EFI\Part-01-03\Boot\ogloader.efi && exit || echo "Part-01-03 not found."

echo "Searching Microsoft loader..."
sanboot --no-describe --drive 0 --filename \EFI\Microsoft\Boot\bootmgfw.efi && exit || echo "Microsoft loader not found."

echo "Searching Ubuntu loader..."
sanboot --no-describe --drive 0 --filename \EFI\ubuntu\grubx64.efi && exit || echo "Ubuntu loader not found."

# Fallback: Si no hay cargadores encontrados
echo "No bootable operating system detected by OpenGnsys. Falling back..."
exit
