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 drv
export hwclock
export def_bootlang
export def_keyboard
export def_timezone
export def_drv
export menu_color_normal
export menu_color_highlight
export pager

function menu_help {
    check_help
    grub_help
}

function check_help {
    if [ -z "${koptsok}" ]; then
        echo $"WARNING: nothing set"
        echo $" System will use defaults"
        echo  "  American English <> utc clock <> us "$"keyboard <> free drivers"
        echo ""
    fi
}

function kopts_help {
    echo $"Example boot command line arguments:"
    echo $" Setup"
    echo  "  lang=<language>_<variant> keytable=<layout>         tz=<region>/<city>"
    echo  "  hostname=<name>           username=<name>"
    echo $" Kernel"
    echo  "  blacklist=<module>"
    echo $" Video"
    echo  "  <module>.modeset=0"
    echo  "  radeon.modeset=0          nouveau.modeset=0         i915.modeset=0"
    echo  "  video=VGA-1:1920x1080e    DVI-0:800x600"
    echo ""
}

function grub_help {
    echo $"To use the menu:"
    echo $" press up or down to move between items"
    echo $" press enter or right to select the item"
    echo $" press e to edit the current item"
    echo $" press c to go to the grub command line"
    echo ""
    echo $"Simply edit the first line of most menus to control them"
    echo $"Select or edit the blank line to enter command line arguments"
    echo ""
}

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

function menu_continue {
    echo -n $"Press Enter to continue"
    read mcont
}

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 memtest_detect {
    if ! is_efi; then
        if [ -f /boot/memtest ]; then
            menuentry --class=memtest --hotkey=m "memtest" {linux16 /boot/$1}
        fi
    fi
}

function lang_setup {
    source /boot/grub/locales/${1}
    menu_reload
}

function lang_menu {
    menuentry --class=lang.${ll} "lang=${ll}" "${ll}" {lang_setup "${2}"}
}

function show_timezones {
    submenu --class=tz --hotkey=t "tz=${def_timezone}" {# set the timezone above with the editor
        if [ -n "$1" ];then
            if [ "$1" = "tz=${def_timezone}" ]; then
                default=0
                menuentry --class=cancel --hotkey=q "Cancel (${def_timezone})" {menu_reload}
                for tt in Africa America Asia Atlantic Australia Etc Europe Pacific; do
                    submenu --class=tz.${tt} "${tt}" {
                        menuentry --class=cancel --hotkey=q "Cancel (${def_timezone})" {menu_reload}
                        source "/boot/grub/tz/${1}"
                    }
                done
            else
                regexp -s newtz '^tz=(.*)' "$1"
                if [ -z "$newtz" ]; then
                    def_timezone="$1"
                else
                    def_timezone="$newtz"
                fi
                timezone="tz=$def_timezone"
                menu_reload
            fi
        fi
    }
}

function show_keymaps {
    submenu --class=kbd --hotkey=k "keytable=${def_keyboard}" {# set the timezone above with the editor
        if [ -n "$1" ]; then
            if [ "$1" = "keytable=${def_keyboard}" ]; then
                default=0
                menuentry --class=cancel --hotkey=q "Cancel (${def_keyboard})" {menu_reload}
                source /boot/grub/locales/keyboards
            else
                regexp -s newkey '^keytable=(.*)' "$1"
                if [ -z "$newkey" ]; then
                    def_keyboard="$1"
                else
                    def_keyboard="$newkey"
                fi
                keyboard="keytable=$def_keyboard"
                menu_reload
            fi
        fi
    }
}

function show_drivers {
    submenu --class=driver --hotkey=m "driver=${def_drv}" {# set the driver above with the editor
        if [ -n "$1" ]; then
            if [ "$1" = "driver=${def_drv}" ]; then
                default=0
                menuentry --class=cancel --hotkey=q "Cancel (${def_drv})" {menu_reload}
                source /boot/grub/mhwd.cfg
            else
                regexp -s newdrv '^driver=(.*)' "$1"
                if [ -z "${newdrv}" ]; then
                    def_drv="$1"
                else
                    def_drv="${newdrv}"
                fi
                newdrv="driver=${def_drv}"
                menu_reload
            fi
        fi
    }
}

function show_languages {
    submenu --hotkey=l --class=lang "lang=${def_bootlang}" {# set the timezone above with the editor
        if [ -n "$1" ]; then
            if [ "$1" = "lang=${def_bootlang}" ]; then
                default=0
                menuentry --class=lang.${def_bootlang} "lang=${def_bootlang}" {menu_reload}
                menuentry --class=lang.cs "Čeština" {lang_setup "cs_CZ"}
                menuentry --class=lang.da "Dansk" {lang_setup "da_DK"}
                submenu --class=lang.de "Deutsch" {
                    for ll in de_DE de_AT de_BE de_CH de_LI de_LU; do
                        lang_menu ${ll}
                    done
                }
                submenu --class=lang.el "Ελληνικά" {
                    for ll in el_GR el_CY; do
                        lang_menu ${ll}
                    done
                }
                submenu --class=lang.en "English" {
                    for ll in en_US en_AG en_AU en_BW en_CA en_DK en_GB en_HK en_IE en_IN en_NG en_NZ en_PH en_SG en_ZA en_ZW; do
                        lang_menu ${ll}
                    done
                }
                submenu --class=lang.es "Español" {
                    for ll in es_ES es_AR es_BO es_CL es_CO es_CR es_DO es_EC es_GT es_HN es_MX es_NI es_PA es_PE es_PR es_PY es_SV es_US es_UY es_VE; do
                        lang_menu ${ll}
                    done
                }
                submenu --class=lang.fr "Français" {
                    for ll in fr_FR fr_BE fr_CA fr_CH fr_LU fr_NC; do
                        lang_menu ${ll}
                    done
                }
                menuentry --class=lang.he "עִבְרִית" {lang_setup "he_IL"}
                submenu --class=lang.it "Italiano" {
                    for ll in it_IT it_CH; do
                        lang_menu ${ll}
                    done
                }
                menuentry --class=lang.ja "日本語" {lang_setup "ja_JP"}
                submenu --class=lang.nl "Nederlands" {
                    for ll in nl_NL nl_AW nl_BE; do
                        lang_menu ${ll}
                    done
                }
                menuentry --class=lang.pl "Polski" {lang_setup "pl_PL"}
                submenu --class=lang.pt "Português" {
                    for ll in pt_BR pt_PT; do
                        lang_menu ${ll}
                    done
                }
                menuentry --class=lang.ro "Română" {lang_setup "ro_RO"}
                submenu --class=lang.ru "Русский" {
                    for ll in ru_RU ru_UA;do
                        lang_menu ${ll}
                    done
                }
                submenu --class=lang.zh "中文" {
                    for ll in zh_CN zh_TW; do
                        lang_menu ${ll}
                    done
                }
            else
                regexp -s newlang '^lang=(.*)' "$1"
                if [ -z "$newlang" ]; then
                    def_bootlang="$1"
                else
                    def_bootlang="$newlang"
                fi
                bootlang="lang=$def_bootlang"
                if [ -f /boot/grub/locales/${def_bootlang} ]; then
                    source /boot/grub/locales/${def_bootlang}
                fi
                menu_reload
            fi
        fi
    }
}

function boot_menu {
    if [ "${hwclock}" = "utc=yes" ]; then
        class="utc"
        title="utc"
        clockset=""
    elif [ "${timezone}${bootlang}" = "" ]; then
        class="unset"
        title="utc"
        clockset=""
    else
        title="${def_timezone}"
        class="tz.${def_timezone}"
        clockset="utc=yes"
    fi
    min2d="$MINUTE"
    if regexp '(^.$)' "$min2d"; then
        min2d="0${min2d}"
    fi
    hour2d="$HOUR"
    if regexp '(^.$)' "$hour2d"; then
        hour2d="0${hour2d}"
    fi
    menuentry --class=tz.$class --hotkey=u "$hour2d:$min2d $title" "$clockset" {
        hwclock="$2"
        menu_reload
    }
    show_timezones
    show_keymaps
    show_languages
    show_drivers
    default=5
}

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

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

function grub_bios_installed {
    if ! is_efi;  then
        menuentry "" {true}
        menuentry --class=tromjaro "Installed BIOS-System on $installed" {exit}
    fi
}

function grub_efi_installed {
    if is_efi; then
        menu "" {true}
        menu --class=tromjaro "Installed EFI-System on $installed" {exit}
    fi
}


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}"
        elif [ "${optname}" = "driver" ]; then
            def_drv="${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" "driver drv"; 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 keystatus --shift; then
        disable_gfxterm=true
    fi

    if [ "${disable_gfxterm}" != "true" ]; then
        if loadfont /boot/grub/unicode.pf2 ; then
            if [ -z "$gfxmode" ] ; then
                gfxmode=1024x768,auto # 1280x1024,1024x768,800x600,640x480 possible value
                gfxpayload=keep
            fi
            if keystatus --ctrl; then
                gfxok=""
                pager=0
                while [ -z "$gfxok" ]; do
                    echo "gfxmode=$gfxmode"
                    echo "Input new mode or Enter to confirm"
                    prompt
                    read gfxmodein
                    if [ -n "$gfxmodein" ]; then
                        gfxmode="$gfxmodein"
                        echo ""
                    else
                        gfxok="true"
                    fi
                done
            fi
            insmod all_video
            insmod gfxterm
            if terminal_output --append gfxterm; then
                terminal_output --remove console
            else
                disable_gfxterm=true
            fi
        else
            disable_gfxterm=true
        fi
    fi

    if [ "${disable_gfxterm}" != "true"  -a -f "${grub_theme}" ]; then
        insmod gfxmenu
        themedir=""
        regexp -s themedir '^(.*)/.*$' "${grub_theme}"
        for themefont in $themedir/*.pf2 $themedir/f/*.pf2; do
            if [ -f "${themefont}" ]; then
                loadfont "${themefont}"
            fi
        done
        for imagetype in jpg jpeg png tga; do
            for themeimage in ${themedir}/*.${imagetype}; do
                if [ -f "${themeimage}" ]; then
                    if [ "${imagetype}" = "jpg" ]; then
                        insmod jpeg
                    else
                        insmod $imagetype
                    fi
                fi
            done
        done
        theme=${grub_theme}
        export theme
    fi

    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}" "${drv}" "${auto}"; do
    if [ -n "${kopt}" ]; then
        kopts="${kopts} ${kopt}"
    fi
done
if [ -n "${bootlang}${keyboard}${timezone}${drv}" ]; then
    koptsok="true"
fi

boot_menu

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
menuentry --class=type --hotkey=tab "" {# set arguments above with the editor
    getargs $@
    pager=0
    if [ -z "$args" ]; then
        check_help
        kopts_help
        echo $"Input command line options, press Enter to continue"
        prompt
        read newinput
    else
        newinput="$args"
    fi
    new2cli "$newinput"
    pager=1
    menu_reload
}
menuentry --hotkey=h --class=help "help" {
    pager=0
    menu_help
    menu_continue
    pager=1
}
efi_detect
memtest_detect
search --no-floppy --file /etc/issue --set installed
grub_bios_installed
grub_efi_installed
grub_reboot
