insmod regexp

if [ "$grub_platform" = "pc" ]; then
    insmod setkey
fi

function hiperiso_pause {
    echo "press Enter to continue ......"
    read hisoTmpPause
}

function hiperiso_debug_pause {
    if [ -n "${vtdebug_flag}" ]; then
        echo "press Enter to continue ......"
        read hisoTmpPause
    fi
}

function hiperiso_cli_console {
    if [ -z "$hiso_display_mode" ]; then
        terminal_output  console
    elif [ "$hiso_display_mode" = "GUI" ]; then
        terminal_output  console
    fi
}

function hiperiso_gui_console {
    if [ -z "$hiso_display_mode" ]; then
        terminal_output  gfxterm
    elif [ "$hiso_display_mode" = "GUI" ]; then
        terminal_output  gfxterm
    fi
}

function hiperiso_language {
    configfile $hiso_grub_path/menulang.cfg
}

function hiperiso_diagnosis {
    vt_enum_video_mode
    configfile $hiso_grub_path/debug.cfg
}

function hiperiso_localboot {
    configfile $hiso_grub_path/localboot.cfg
}

function hiperiso_ext_menu {
    if [ -e $hiso_plugin_path/hiperiso/hiperiso_grub.cfg ]; then
        set hiperiso_new_context=1
        configfile $hiso_plugin_path/hiperiso/hiperiso_grub.cfg
        unset hiperiso_new_context
    else
       echo "hiperiso_grub.cfg NOT exist."
       echo -en "\n$HISOLANG_ENTER_EXIT ..."
       read hisoInputKey
    fi
}

function hiperiso_show_help {
    if [ -f $hiso_grub_path/help.tar.gz ]; then
        if [ -z "$hiso_help_txt_mem_addr" ]; then
            vt_load_file_to_mem "auto" $hiso_grub_path/help.tar.gz hiso_help_txt_mem
        fi
        loopback hiso_help_tarfs mem:${hiso_help_txt_mem_addr}:size:${hiso_help_txt_mem_size}
        vt_cur_menu_lang hisoCurLang
        if [ -f "(hiso_help_tarfs)/help/${hisoCurLang}.txt" ]; then
            cat "(hiso_help_tarfs)/help/${hisoCurLang}.txt"
        else
            cat "(hiso_help_tarfs)/help/en_US.txt"
        fi
        loopback -d hiso_help_tarfs
    fi
}

function hiperiso_load_menu_lang_file {
    if [ -f $hiso_grub_path/menu.tar.gz ]; then
        vt_load_file_to_mem "auto" $hiso_grub_path/menu.tar.gz hiso_menu_lang_mem
        loopback hiso_menu_tarfs mem:${hiso_menu_lang_mem_addr}:size:${hiso_menu_lang_mem_size}
    fi
}

function hiperiso_checksum {
    if [ -f "${hiso_iso_part}${HISO_CHKSUM_FILE_PATH}" ]; then
        configfile $hiso_grub_path/checksum.cfg
    fi
}

function iso_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    if vt_check_custom_boot "${hiso_chosen_path}" hiso_vcfg; then
        set hiso_chosen_path="${hiso_chosen_path}"
        vt_file_basefile "${hiso_chosen_path}" hiso_chosen_file
        export hiso_chosen_path
        export hiso_chosen_file
        hiperiso_debug_pause
        configfile "${hiso_iso_part}${hiso_vcfg}"
        true
    else
        hiperiso_boot "${hiso_chosen_path}"
    fi
}

function miso_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    hiperiso_boot "${hiso_chosen_path}" memdisk
}

function iso_unsupport_menuentry {
    echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
    echo -en "\n$HISOLANG_ENTER_EXIT ..."
    read hisoInputKey
}

function wim_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    hiperiso_boot "${hiso_chosen_path}"
}

function wim_unsupport_menuentry {
    iso_unsupport_menuentry
}

function efi_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    hiperiso_boot "${hiso_chosen_path}"
}

function efi_unsupport_menuentry {
    iso_unsupport_menuentry
}

function vhd_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    hiperiso_boot "${hiso_chosen_path}"
}

function vhd_unsupport_menuentry {
    iso_unsupport_menuentry
}

function hiso_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    hiperiso_boot "${hiso_chosen_path}"
}

function hiso_unsupport_menuentry {
    iso_unsupport_menuentry
}

function img_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    hiperiso_boot "${hiso_chosen_path}"
}

function img_unsupport_menuentry {
    iso_unsupport_menuentry
}

function mimg_common_menuentry {
    vt_chosen_img_path hiso_chosen_path hiso_chosen_size hiso_chosen_name

    if vt_check_password "${hiso_chosen_path}"; then
        return
    fi

    hiperiso_boot "${hiso_chosen_path}" memdisk
}

set HIPERISO_VERSION="1.0.0"

set HISO_PARAM_NO_ACPI=1

set HISO_DEFAULT_MENU_MODE=0

set HISO_MEM_DISK_STR="[Memdisk]"
set HISO_ISO_RAW_STR="Compatible Mode"
set HISO_GRUB2_MODE_STR="GRUB2 Mode"
set HISO_WIMBOOT_MODE_STR="WIMBOOT Mode"
set HISO_ISO_UEFI_DRV_STR="UEFI FS"

set HISO_F2_CMD="vt_browser_disk"
set HISO_F4_CMD="hiperiso_localboot"
set HISO_F5_CMD="hiperiso_diagnosis"
set HISO_F6_CMD="hiperiso_ext_menu"
set HISO_HELP_CMD="hiperiso_show_help"
set HISO_CHKSUM_CMD="hiperiso_checksum"
set HISO_HELP_TXT_LANGUAGE="en_US"
set HISO_CHKSUM_FILE_PATH="X"
set HISO_LANG_CMD="hiperiso_language"

if [ "$grub_platform" = "pc" ]; then
    set HISO_TEXT_MENU_VER="hiperiso $HIPERISO_VERSION BIOS  www.hiperiso.net"
else
    if [ "$grub_cpu" = "i386" ]; then
        set HISO_EFI_ARCH=ia32
        set HISO_TEXT_MENU_VER="hiperiso $HIPERISO_VERSION IA32  www.hiperiso.net"
    elif [ "$grub_cpu" = "arm64" ]; then
        set HISO_EFI_ARCH=aa64
        set HISO_TEXT_MENU_VER="hiperiso $HIPERISO_VERSION AA64  www.hiperiso.net"
    elif [ "$grub_cpu" = "mips64el" ]; then
        set HISO_EFI_ARCH=mips
        set HISO_TEXT_MENU_VER="hiperiso $HIPERISO_VERSION MIPS  www.hiperiso.net"
    else
        set HISO_EFI_ARCH=x64
        set HISO_TEXT_MENU_VER="hiperiso $HIPERISO_VERSION UEFI  www.hiperiso.net"
    fi
fi

vt_device $root  hiso_dev

if [ "$hiso_dev" = "tftp" ]; then
    set hiso_path=($root)
    for hisoid in 0 1 2 3; do
        if [ -f (hd$hisoid,2)/EFI/hiperiso/initramfs.cpio.gz ]; then
            set hiso_iso_part=(hd$hisoid,1)
            set hiso_efi_part=(hd$hisoid,2)
            set hisodev=hd$hisoid
            break
        fi
    done
    loadfont ascii

    if [ -n "$hiso_efi_part" ]; then
        set hiso_grub_path=$hiso_efi_part/grub
        vt_load_file_to_mem "auto" $hiso_grub_path/fonts/unicode.pf2 hiso_font_mem
        loadfont mem:${hiso_font_mem_addr}:size:${hiso_font_mem_size}
    fi

    if [ -f $hiso_iso_part/hiperiso/hiperiso.json ]; then
        set hiso_plugin_path=$hiso_iso_part
    else
        set hiso_plugin_path=$hiso_grub_path
        vt_load_plugin $hiso_plugin_path
    fi
else
    if [ "$prefix" = "(hiperisodisk)/grub" ]; then
        set hiso_path=(hiperisodisk)/hiperiso
    else
        set hiso_path=($root)/hiperiso
    fi

    set hisodev=$hiso_dev
    set hiso_iso_part=($hiso_dev,1)
    set hiso_efi_part=($hiso_dev,2)
    set hiso_grub_path=$hiso_efi_part/grub

    if [ -f $hiso_grub_path/fonts/unicode.pf2 ]; then
        vt_load_file_to_mem "auto" $hiso_grub_path/fonts/unicode.pf2 hiso_font_mem
        loadfont mem:${hiso_font_mem_addr}:size:${hiso_font_mem_size}
    fi

    set hiso_plugin_path=$hiso_iso_part
fi

vt_load_part_table $hisodev

hiperiso_load_menu_lang_file

if [ -f $hiso_iso_part/hiperiso/hiperiso.json ]; then
    clear
    vt_load_plugin $hiso_iso_part
    clear
else
    vt_check_json_path_case $hiso_iso_part
fi

vt_update_sb_policy

if [ -n "$HISO_MENU_LANGUAGE" ]; then
    vt_init_menu_lang "$HISO_MENU_LANGUAGE"
else
    vt_init_menu_lang en_US
fi

if [ -n "$HISO_MENU_TIMEOUT" ]; then
    set timeout=$HISO_MENU_TIMEOUT
else
    unset timeout
fi

if [ $HISO_DEFAULT_MENU_MODE -eq 0 ]; then
    set HISO_F3_CMD="vt_dynamic_menu 1 1"
else
    set HISO_F3_CMD="vt_dynamic_menu 1 0"
fi

terminal_output  console

if [ -n "$hiso_gfxmode" ]; then
    set gfxmode=$hiso_gfxmode
    set gfxpayload=keep
else
    set gfxmode=1024x768
    set gfxpayload=keep
fi

if [ "$hiso_display_mode" = "CLI" ]; then
    terminal_output  console
elif [ "$hiso_display_mode" = "serial" ]; then
    if [ -n "$hiso_serial_param" ]; then
        serial $hiso_serial_param
    fi
    terminal_input   serial
    terminal_output  serial
elif [ "$hiso_display_mode" = "serial_console" ]; then
    if [ -n "$hiso_serial_param" ]; then
        serial $hiso_serial_param
    fi
    terminal_input   serial console
    terminal_output  serial console
else
    if [ "$hiso_gfxmode" = "max" ]; then
        set gfxmode=1024x768
        terminal_output  gfxterm
        vt_enum_video_mode
        vt_get_video_mode 0 hisoCurMode
        terminal_output console
        set gfxmode=$hisoCurMode
        terminal_output gfxterm
    elif [ "$hiso_res_fit" = "1" ]; then
        terminal_output  gfxterm
    fi

    if [ -n "$hiso_theme" ]; then
        vt_set_theme
    else
        set theme=$hiso_grub_path/themes/hiperiso/theme.txt
    fi
    terminal_output  gfxterm
fi

if [ "$grub_platform" = "efi" ]; then
    set mouse_delta=4000
fi

if [ -n "$HISO_DEFAULT_KBD_LAYOUT" ]; then
    set_keyboard_layout "$HISO_DEFAULT_KBD_LAYOUT"
fi

if [ -n "$HISO_PLUGIN_PATH_CASE_MISMATCH" ]; then
    clear
    echo "$HISO_PLUGIN_PATH_CASE_MISMATCH"
    echo -e "\n\nPath case does not match! hiperiso directory and hiperiso.json MUST be all lowercase!"
    echo -e "\n\npress ENTER to continue ..."
    read hisoInputKey
fi

if [ -n "$HISO_PLUGIN_SYNTAX_ERROR" ]; then
    clear
    echo -e "\n Syntax error detected in hiperiso.json, please check! \n"
    echo -e "\n press ENTER to continue ..."
    read hisoInputKey
fi

vt_clear_key

export theme
export gfxmode
export gfxpayload
export hisodev
export hiso_path
export vtdebug_flag
export hiso_iso_fs
export hiso_iso_part
export hiso_efi_part
export HIPERISO_VERSION
export HISO_CUR_VIDEO_MODE
export HISO_EFI_ARCH
export HISO_MEM_DISK_STR
export HISO_ISO_RAW_STR
export HISO_GRUB2_MODE_STR
export HISO_WIMBOOT_MODE_STR
export HISO_ISO_UEFI_DRV_STR
export HISO_F2_CMD
export HISO_F3_CMD
export HISO_F4_CMD
export HISO_F5_CMD
export HISO_F6_CMD
export HISO_HELP_CMD
export HISO_CHKSUM_CMD
export HISO_HELP_TXT_LANGUAGE
export HISO_CHKSUM_FILE_PATH
export HISO_LANG_CMD

set hiperiso_img_count=0
vt_list_img $hiso_iso_part hiperiso_img_count

if [ $hiperiso_img_count -gt 0 ]; then
    if [ $HISO_DEFAULT_MENU_MODE -eq 0 ]; then
        vt_dynamic_menu 0 0
    else
        vt_dynamic_menu 0 1
    fi
else
    if [ -n "$HISO_NO_ISO_TIP" ]; then
        NO_ISO_MENU="No ISO or supported IMG files found, $HISO_NO_ISO_TIP"
    elif [ -n "$HISO_DEFAULT_SEARCH_ROOT" ]; then
        NO_ISO_MENU="No ISO or supported IMG files found, please check HISO_DEFAULT_SEARCH_ROOT"
    else
        NO_ISO_MENU="No ISO or supported IMG files found"
    fi
    menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
        echo -e "\n    Rebooting ... "
        reboot
    }
fi

if [ -n "$HISO_DEFAULT_IMAGE" ]; then
    if regexp --set 1:hisoHotkey --set 2:hisoDefault "(F[2-9])>(.*)" "$HISO_DEFAULT_IMAGE"; then
        set default="$hisoDefault"
        if [ -z "$HISO_MENU_TIMEOUT" ]; then
            set timeout=0
        else
            set timeout=$HISO_MENU_TIMEOUT
        fi
        export timeout
        export default

        vt_fn_mutex_lock 1

        if [ "$hisoHotkey" = "F2" ]; then
            unset timeout
            vt_browser_disk
        elif [ "$hisoHotkey" = "F4" ]; then
            hiperiso_localboot
        elif [ "$hisoHotkey" = "F5" ]; then
            hiperiso_diagnosis
        elif [ "$hisoHotkey" = "F6" ]; then
            hiperiso_ext_menu
        fi

        vt_fn_mutex_lock 0

        unset timeout
        unset default
    fi
fi
