set timeout="1"

load_env ORDER DEBUG

if [ -z "$ORDER" ]; then
    set ORDER="primary secondary"
fi

set ORDER="$ORDER reboot"

for slot in $ORDER; do
    if [ -z "$default" ]; then
        set default="$slot"
    else
        # Contrary to what the documentation says, GRUB (2.06) does
        # not support using titles or IDs in the fallback variable, so
        # we translate to indices.
        if [ "$slot" = "primary" ]; then
            set fallback="$fallback 0"
        elif [ "$slot" = "secondary" ]; then
            set fallback="$fallback 1"
        elif [ "$slot" = "net" ]; then
            set fallback="$fallback 2"
        elif [ "$slot" = "reboot" ]; then
            set fallback="$fallback 3"
	fi
    fi
done

if [ "$DEBUG" ]; then
    set log="debug"
else
    set log="loglevel=4"
fi

submenu "primary" "$log" {
    set slot="$1"
    set append="console=ttyS0 root=PARTLABEL=$slot $2"
    set root=(hd0,gpt3)
    source /boot/grub/grub.cfg
}

submenu "secondary" "$log"  {
    set slot="$1"
    set append="console=ttyS0 root=PARTLABEL=$slot $2"
    set root=(hd0,gpt4)
    source /boot/grub/grub.cfg
}

submenu "net" "$log"  {
    net_dhcp

    if [ "$net_efinet0_dhcp_next_server" -a "$net_efinet0_dhcp_boot_file" ]; then
	set initrd=(tftp,$net_efinet0_dhcp_next_server)/$net_efinet0_dhcp_boot_file
	loopback initrd $initrd
	set root=(initrd)

	set slot="$1"
	set append="console=ttyS0 root=/dev/ram0 ramdisk_size=65536 $2"
	source /boot/grub/grub.cfg
    else
	if [ -z "$net_efinet0_dhcp_next_server" ]; then
	    echo "No TFTP server supplied in DHCP response"
	fi
	if [ -z "$net_efinet0_dhcp_boot_file" ]; then
	    echo "No bootfile supplied in DHCP response"
	fi
    fi
}

submenu "reboot" {
    reboot
}
