function load_video {
  insmod all_video
}

function savedefault {
    if test "$should_savedefault" ; then
        saved_entry="${chosen}"
        save_env -f $grubenv saved_entry
    fi

    if ! test "$eosimage" ; then
        recordfail=1
        save_env -f $grubenv recordfail
    fi
}

if [ "x${timeout}" != "x-1" ]; then
    if keystatus; then
        if keystatus --shift; then
            set timeout=-1
        else
            set timeout=0
        fi
    else
        if sleep --interruptible 1 ; then
            set timeout=0
        fi
    fi
fi

if test "$eosimage" ; then
    set grubenv=($eosimage)/endless/grub/grubenv

    # ISO
    if test -f ($eosimage)/endless/endless.squash ; then
        loopback loop_squash ($eosimage)/endless/endless.squash
        loopback loop_img (loop_squash)/endless.img
        set image_path="/endless/endless.squash"

    # Dual boot
    elif test -f ($eosimage)/endless/endless.img ; then
        loopback loop_img ($eosimage)/endless/endless.img
        set image_path="/endless/endless.img"
    fi

    set root=(loop_img,gpt3)
    probe -u $eosimage --set=image_dev_uuid
    set kparams="endless.image.device=UUID=$image_dev_uuid endless.image.path=$image_path nohibernate"

    # test for Windows hibernation
    if test -f ($eosimage)/hiberfil.sys ; then
        if file --is-hibernated-hiberfil ($eosimage)/hiberfil.sys ; then
            set hibernation=true
        fi
    fi

    if test -f ($eosimage)/eosldr ; then
        # If we can't boot Endless OS, provide a dummy Windows entry which
        # reboots to the Windows boot menu.
        if test "$hibernation" ; then
            set found_windows=true
            menuentry "Microsoft Windows" {
                reboot
            }
        fi
    elif test -f ($eosimage)/endless/live ; then
        set kparams="$kparams endless.live_boot"
        set is_live=true
        export is_live
    else
        set should_savedefault=true
        export should_savedefault

        # search for other OSes
        insmod regexp
        for dev in ($bootdev,*); do
            regexp -s device '\((.*)\)' $dev

            if test "$grub_platform" == "efi"; then
                if test -f ($device)/EFI/MICROSOFT/BOOT/BOOTMGFW.EFI ; then
                    set found_windows=true
                    menuentry "Microsoft Windows" $device {
                        set booting_windows=true
                        savedefault
                        set root=$2
                        chainloader ($root)/EFI/MICROSOFT/BOOT/BOOTMGFW.EFI
                    }
                fi
            else
                probe -s is_bootable -b ($device)
                if test "$is_bootable" == "none"; then
                    continue
                fi

                if test ( -f ($device)/bootmgr -a -f ($device)/Boot/BCD ) ; then
                    set found_windows=true
                    # Windows Vista or later
                    menuentry "Microsoft Windows" $device {
                        set booting_windows=true
                        savedefault
                        set root=$2
                        chainloader +1
                    }
                elif test ( ( -f ($device)/ntldr -o -f ($device)/NTLDR ) -a ( -f ($device)/ntdetect.com -o -f ($device)/NTDETECT.COM ) -a ( -f ($device)/boot.ini -o -f ($device)/BOOT.INI ) ) ; then
                    set found_windows=true
                    # Windows XP
                    menuentry "Microsoft Windows" $device {
                        set booting_windows=true
                        savedefault
                        set root=$2
                        regexp -s devnum 'hd([0-9]+)' $root
                        if test "$devnum" != "0"; then
                            drivemap -s hd0 $root
                        fi
                        chainloader +1
                    }
                elif test "$eosimage" ; then
                    set found_windows=true
                    # Other Windows flavour?
                    menuentry "Unknown OS" $device {
                        set booting_windows=true
                        savedefault
                        set root=$2
                        chainloader +1
                    }
                fi
            fi
        done
    fi
else
    probe -u $root --set=rootuuid
    set grubenv=($root)/boot/grub/grubenv
fi

export grubenv

if test -f $grubenv ; then
    load_env -f $grubenv
fi

# load last selection. savedefault() only writes this entry on dual-boot
# systems.
if test "$saved_entry" ; then
    set default="${saved_entry}"
else
    set default=0
fi

# Show menu if we need to offer Windows, or if the last boot failed.
if test ( "$found_windows" -o "$recordfail" ) ; then
    set timeout=30
fi

# Increment loglevel if the last boot failed.
if test "$recordfail" ; then
    set kparams="$kparams loglevel=6"
fi

if test "$hibernation"; then
    menuentry "Endless OS (unavailable)" {
        echo -------------------------------------------------------------
        echo Endless OS is unavailable whilst Windows is hibernated.
        echo Please select Windows and shut down or reboot to use Endless.
        echo -------------------------------------------------------------
        echo
        echo Press ENTER to continue ...
        read
   }
else
    insmod blscfg
    bls_import
fi
