insmod font
insmod keylayouts
insmod part_msdos
insmod part_gpt

export loaded
export koptsok
export disable_gfxterm
export bootarch
export firmware
export firmware_found
export from
export fromiso
export bootlang
export keyboard
export timezone
export hwclock
export def_bootlang
export def_keyboard
export def_timezone
export menu_color_normal
export menu_color_highlight
export pager



function menu_reload {
    configfile /boot/grub/grub.cfg
}


function prompt {
    echo -n "> "
}

function is_efi {
    if [ "${grub_platform}" = "efi" ]; then
        return 0
    fi
    return 1
}

function efi_detect {
    if is_efi ; then
        submenu --class=find.efi --hotkey=f "Detect EFI bootloaders" {
            efi_found=false

            for efi in (*,gpt*)/efi/*/*.efi (*,gpt*)/efi/*/*/*.efi (*,gpt*)/*.efi (*,gpt*)/*/*.efi ; do
                regexp --set=1:efi_device '^\((.*)\)/' "${efi}"
                if [ -e "${efi}" ]; then
                    efi_found=true

                    menuentry --class=efi "${efi}" "${efi_device}" {
                        root="${2}"
                        chainloader "${1}"
                    }
                fi
            done

            if [ "${efi_found}" != true ]; then
                menuentry --hotkey=q --class=find.none "No EFI files detected." {menu_reload}
            else
                menuentry --hotkey=q --class=cancel "Cancel" {menu_reload}
            fi
        }
    fi
}


function boot_defaults {
    source /boot/grub/defaults.cfg
}

function grub_reboot {
    menuentry " " {true}
    menuentry --class=reboot "Reboot!" {reboot}
}




function check {
    cliname="$1"
    varname="$2"
    if [ "${optname}" != "${cliname}" ]; then
        return 1
    fi
    if [ -z "$varname" ]; then
        varname="${cliname}"
    fi
    if [ -z "${optval}" ]; then
        eval ${varname}="${cliname}"
    else
        eval ${varname}="${cliname}=${optval}"
        if [ "${optname}" = "lang" ]; then
            lang_setup ${optval}
        elif [ "${optname}" = "keytable" ]; then
            def_keyboard="${optval}"
        elif [ "${optname}" = "tz" ]; then
            def_timezone="${optval}"
        fi
    fi
    return 0
}

function checkinput {
    newinput="$1"
    newoutput=""
    for newcli in ${newinput} ; do
    optname=""
    optval=""
    optdone=""
    regexp -s optname '^(.+)=.*$' "${newcli}"
    if [ -z "${optname}" ]; then
        optname="${newcli}"
    else
        regexp -s optval '^.+=(.*)$' "${newcli}"
    fi
    for tocheck in "lang bootlang" "keytable keyboard" "tz timezone" "utc hwclock"; do
        if check $tocheck ; then
            optdone="Y"
            break
        fi
    done
    if [ -z "${optdone}" ]; then
        if [ -z "${newoutput}" ]; then
            newoutput="${newcli}"
        else
            newoutput="${newoutput} ${newcli}"
        fi
    fi
    done
}

function new2cli {
    newinput="$1"
    if [ -n "$newinput" ]; then
        checkinput "$newinput"
        if [ -n "${newoutput}" ]; then
            newinput="${newoutput}"
            if [ -n "$clinput" ]; then
                clinput="$clinput $newinput"
            else
                clinput="$newinput"
            fi
            export clinput
        fi
    fi
}

function getargs {
    args=""
    for a in $@; do
        if [ -n "${a}" ]; then
            args="${args} ${a}"
        fi
    done
}

function bootcheck {
    getargs
}

pager=1

insmod regexp
insmod datehook
if ! is_efi; then
    insmod part_gpt
fi

if is_efi; then
    insmod efi_gop
    insmod efi_uga
fi

if [ -z "${loaded}" ]; then
    iscd=""
    auto=""
    regexp -s iscd '^(cd)' "${root}"
    if [ "${iscd}" != "cd" -a -e (cd) ]; then
        probe -s cdp -u cd
        probe -s hdp -u $root
        if [ -n "$cdp" -a "$cdp" = "$hdp" ]; then
            iscd="cd"
        fi
    fi
    if [ "${root}" != "hd96" -a "${iscd}" != "cd" ]; then
        auto="${auto}"
    fi
    source /boot/grub/variable.cfg
    loaded=true



    if [ "${disable_gfxterm}" != "true" ]; then
        if loadfont /boot/grub/unicode.pf2 ; then
            if [ -z "$gfxmode" ] ; then
                gfxmode=1024x768,auto
                gfxpayload=keep
            fi
            insmod efi_gop
            insmod all_video
            insmod efi_uga
            insmod ieee1275_fb
            insmod vbe
            insmod vga
            insmod video_bochs
            insmod video_cirrus
            insmod gfxterm
            if terminal_output --append gfxterm; then
                terminal_output --remove console
            else
                disable_gfxterm=true
            fi
        else
            disable_gfxterm=true
        fi
    fi

        insmod gfxmenu
        insmod jpeg
        insmod png
        theme=($root)/boot/grub/themes/manjaro-live/theme.txt
        export theme

    menu_color_normal=cyan/blue
    menu_color_highlight=white/blue

    if [ -n "${iso_path}" ]; then
        auto="img_dev=/dev/disk/by-uuid/${rootuuid} img_loop=${iso_path}"
    fi
    
    export auto
fi

boot_defaults

for kopt in "${clinput}" "${bootlang}" "${keyboard}" "${timezone}" "${hwclock}" "${auto}"; do
    if [ -n "${kopt}" ]; then
        kopts="${kopts} ${kopt}"
    fi
done
if [ -n "${bootlang}${keyboard}${timezone}" ]; then
    koptsok="true"
fi


source /boot/grub/kernels.cfg
if [ -n "$clinput" ]; then
    menuentry --class=edit "$clinput" {
        if [ -n "$1" ]; then
            clinput="$@"
        else
            unset clinput
        fi
        menu_reload
    }
fi


efi_detect
grub_bios_installed
grub_efi_installed
grub_reboot
