# Load partition table and file system modules
insmod part_gpt
insmod part_msdos
insmod fat
insmod iso9660
insmod ntfs
insmod ntfscomp
insmod exfat
insmod udf

# Use graphics-mode output
if loadfont "${prefix}/fonts/unicode.pf2" ; then
    insmod all_video
    set gfxmode="auto"
    terminal_input console
    terminal_output console
fi

# Enable serial console
insmod serial
insmod usbserial_common
insmod usbserial_ftdi
insmod usbserial_pl2303
insmod usbserial_usbdebug
if serial --unit=0 --speed=115200; then
    terminal_input --append serial
    terminal_output --append serial
fi

# Get a human readable platform identifier
if [ "${grub_platform}" == 'efi' ]; then
    archiso_platform='UEFI'
elif [ "${grub_platform}" == 'pc' ]; then
    archiso_platform='BIOS'
else
    archiso_platform="${grub_cpu}-${grub_platform}"
fi

# Set default menu entry
default=archlinux
timeout=15
timeout_style=menu


# Menu entries

menuentry "RaveOS Install Medium (%ARCH%, ${archiso_platform})" --class arch --class gnu-linux --class gnu --class os --id 'raveos' {
    set gfxpayload=keep
    linux /%INSTALL_DIR%/boot/%ARCH%/vmlinuz-linux archisobasedir=%INSTALL_DIR% archisosearchuuid=%ARCHISO_UUID%
    initrd /%INSTALL_DIR%/boot/%ARCH%/initramfs-linux.img
}

if [ "${grub_platform}" == 'efi' -a "${grub_cpu}" == 'x86_64' -a -f '/boot/memtest86+/memtest.efi' ]; then
    menuentry 'Run Memtest86+ (RAM test)' --class memtest86 --class memtest --class gnu --class tool {
        set gfxpayload=800x600,1024x768
        linux /boot/memtest86+/memtest.efi
    }
fi
if [ "${grub_platform}" == 'pc' -a -f '/boot/memtest86+/memtest' ]; then
    menuentry 'Run Memtest86+ (RAM test)' --class memtest86 --class memtest --class gnu --class tool {
        set gfxpayload=800x600,1024x768
        linux /boot/memtest86+/memtest
    }
fi
if [ "${grub_platform}" == 'efi' ]; then
    if [ "${grub_cpu}" == 'x86_64' -a -f '/shellx64.efi' ]; then
        menuentry 'UEFI Shell' --class efi {
            chainloader /shellx64.efi
        }
    elif [ "${grub_cpu}" == 'i386' -a -f '/shellia32.efi' ]; then
        menuentry 'UEFI Shell' --class efi {
            chainloader /shellia32.efi
        }
    elif [ "${grub_cpu}" == 'arm64' -a -f '/shellaa64.efi' ]; then
        menuentry 'UEFI Shell' --class efi {
            chainloader /shellaa64.efi
        }
    elif [ "${grub_cpu}" == 'riscv64' -a -f '/shellriscv64.efi' ]; then
        menuentry 'UEFI Shell' --class efi {
            chainloader /shellriscv64.efi
        }
    elif [ "${grub_cpu}" == 'loongarch64' -a -f '/shellloongarch64.efi' ]; then
        menuentry 'UEFI Shell' --class efi {
            chainloader /shellloongarch64.efi
        }
    fi

    menuentry 'UEFI Firmware Settings' --id 'uefi-firmware' {
        fwsetup
    }
fi

menuentry 'System shutdown' --class shutdown --class poweroff {
    echo 'System shutting down...'
    halt
}

menuentry 'System restart' --class reboot --class restart {
    echo 'System rebooting...'
    reboot
}


# GRUB init tune for accessibility
play 600 988 1 1319 4
