#!/bin/sh
exit 0
quiet_boot="0"
. "$pkgdatadir/grub-mkconfig_lib"

export TEXTDOMAIN=grub
export TEXTDOMAINDIR="${datarootdir}/locale"

CLASS="--class gnu-linux --class gnu --class os"

GRUB_DEVICE="$(grub-probe --target=device /)"
GRUB_DEVICE_UUID="$(grub-probe --device ${GRUB_DEVICE} --target=fs_uuid 2>/dev/null)" || true
boot_device_id=$GRUB_DEVICE_UUID

LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}

if [ "x${GRUB_DISTRIBUTOR}" = "x" ]; then
    OS=GNU/Linux
else
    case ${GRUB_DISTRIBUTOR} in
    Ubuntu | Kubuntu | Kylin | NeoKylin | PksKylin)
        OS="${GRUB_DISTRIBUTOR} ${GRUB_DISTRIBUTOR_RELEASE}"
        ;;
    *)
        OS="${GRUB_DISTRIBUTOR} GNU/Linux"
        ;;
    esac
    CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1 | LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
fi

linux_entry() {
    title="$1"
    version="$2"
    args="$3"

    if [ -z "$boot_device_id" ]; then
        boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
    fi
    echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
    if [ "$quick_boot" = 1 ]; then
        echo "	recordfail" | sed "s/^/$submenu_indentation/"
    fi

    # Use ELILO's generic "efifb" when it's known to be available.
    # FIXME: We need an interface to select vesafb in case efifb can't be used.
    arch=$(uname -m)
    if [ x"$arch" = x"mips64" ]; then
        need_boot=1
    fi
    if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ] && [ "$arch" != "aarch64" ]; then
        echo "	load_video" | sed "s/^/$submenu_indentation/"
    else
        if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ] && [ "$arch" != "aarch64" ]; then
            echo "	load_video" | sed "s/^/$submenu_indentation/"
        fi
    fi
    if ([ "x$GRUB_GFXPAYLOAD_LINUX" != x ] || [ "$gfxpayload_dynamic" = 1 ]); then
        echo "	gfxmode \$linux_gfx_mode" | sed "s/^/$submenu_indentation/"
    fi

    echo "	insmod gzio" | sed "s/^/$submenu_indentation/"
    echo "	if [ x\$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi" | sed "s/^/$submenu_indentation/"

    if [ x$dirname = x/ ]; then
        if [ -z "${prepare_root_cache}" ]; then
            prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
        fi
        printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
    else
        if [ -z "${prepare_boot_cache}" ]; then
            prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
        fi
        printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
    fi
    if [ x"$quiet_boot" = x0 ]; then
        message="$(gettext_printf "Loading Linux %s ..." ${version})"
        sed "s/^/$submenu_indentation/" <<EOF
	echo	'$(echo "$message" | grub_quote)'
EOF
    fi
    if test -d /sys/firmware/efi && test -e "${linux}.efi.signed"; then
        sed "s/^/$submenu_indentation/" <<EOF
	linux	${rel_dirname}/${basename}.efi.signed root=${linux_root_device_thisversion} ro ${args}
EOF
    else
        sed "s/^/$submenu_indentation/" <<EOF
	linux	${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
EOF
    fi
    if test -n "${initrd}"; then
        # TRANSLATORS: ramdisk isn't identifier. Should be translated.
        if [ x"$quiet_boot" = x0 ]; then
            message="$(gettext_printf "Loading initial ramdisk ...")"
            sed "s/^/$submenu_indentation/" <<EOF
	echo	'$(echo "$message" | grub_quote)'
EOF
        fi
        sed "s/^/$submenu_indentation/" <<EOF
	initrd	${rel_dirname}/${initrd}
EOF
    fi

    if [ x"$dtbo" = x1 ]; then
        if [ x"$quiet_boot" = x0 ]; then
            message="$(gettext_printf "Loading device tree image ...")"
            sed "s/^/$submenu_indentation/" <<EOF
	echo	'$(echo "$message" | grub_quote)'
EOF
        fi
        sed "s/^/$submenu_indentation/" <<EOF
	devicetree ${rel_dirname}/${dtb}
EOF
    fi

    if [ x"$need_boot" = x1 ]; then
        echo "	boot" | sed "s/^/$submenu_indentation/"
    fi
    sed "s/^/$submenu_indentation/" <<EOF
}
EOF
}

machine=$(uname -m)
case "x$machine" in
xi?86 | xx86_64)
    list=
    for i in /boot/vmlinuz-* /vmlinuz-* /boot/kernel-*; do
        if grub_file_is_not_garbage "$i"; then list="$list $i"; fi
    done
    ;;
*)
    list=
    for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-*; do
        if grub_file_is_not_garbage "$i"; then list="$list $i"; fi
    done
    ;;
esac

# disable locale if arch is mips or arm
case "x$machine" in
xmips*)
    export LC_ALL=C
    ;;
xaarch*)
    export LC_ALL=C
    ;;
esac

prepare_boot_cache=
prepare_root_cache=
title_correction_code=
linux=$(version_find_latest $list)
basename=$(basename $linux)
dirname=$(dirname $linux)
rel_dirname=$(make_system_path_relative_to_its_root $dirname)
boot_dirname=$(dirname $dirname)
version=$(echo $basename | sed -e "s,^[^0-9]*-,,g")

alt_version=$(echo $version | sed -e "s,\.old$,,g")
linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
initrd_early=
for i in ${GRUB_EARLY_INITRD_LINUX_STOCK} \
    ${GRUB_EARLY_INITRD_LINUX_CUSTOM}; do
    if test -e "${dirname}/${i}"; then
        initrd_early="${initrd_early} ${i}"
    fi
done

initrd_real=
for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
    "initrd-${version}" "initramfs-${version}.img" \
    "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
    "initrd-${alt_version}" "initramfs-${alt_version}.img" \
    "initramfs-genkernel-${version}" \
    "initramfs-genkernel-${alt_version}" \
    "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
    "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
    if test -e "${dirname}/${i}"; then
        initrd_real="${i}"
        break
    fi
done

initrd=
if test -n "${initrd_early}" || test -n "${initrd_real}"; then
    initrd="${initrd_early}${initrd_real}"

    initrd_display=
    for i in ${initrd}; do
        initrd_display="${initrd_display} ${dirname}/${i}"
    done
    gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
fi
dtb=
for i in "dtb-${version}" "dtb-${alt_version}" "dtb"; do
    if test -e "${dirname}/${i}"; then
        dtb="$i"
        break
    fi
done

config=
for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}"; do
    if test -e "${i}"; then
        config="${i}"
        break
    fi
done

initramfs=
if test -n "${config}"; then
    initramfs=$(grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \")
fi

if test -z "${initramfs}" && test -z "${initrd_real}"; then
    # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
    # no initrd or builtin initramfs, it can't work here.
    if [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ||
        [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ]; then

        linux_root_device_thisversion=${GRUB_DEVICE}
    else
        linux_root_device_thisversion=PARTUUID=${GRUB_DEVICE_PARTUUID}
    fi
fi
dtbo=
if [ "x$IS_PHYTIUM" = "xtrue" ] && [ "x$FT_EFI" != "xtrue" ]; then
    dtb=
    if [ "$(systemd-detect-virt)" != "none" ]; then
        dtb_name="u-boot-general.dtb"
    else
        if [ "x$IS_FT2000p" = "xtrue" ]; then
            dtb_name="ft2000plus.dtb"
        else
            dtb_name="u-boot-general.dtb"
        fi
    fi
    for i in "dtb-${version}/device-tree/phytium/${dtb_name}" "dtb-${alt_version}/device-tree/phytium/${dtb_name}"; do
        if test -e "${dirname}/${i}"; then
            dtbo=1
            dtb="${i}"
            break
        fi
    done
fi
echo "###============" ${rel_dirname}/${dtb} "=============###"
backup_time=$(date "+%Y-%m-%d %H:%M:%S")
if [ -f /etc/.bootinfo ];then
	backup_time=$(cat /etc/.bootinfo | grep -Ev "^#|^$" | grep ROLLBACK_TIME | awk '{split($0,arr,"=");print arr[2]}')
fi
ROLLBACK_DESC="$OS"
menu_entry=$(gettext_printf "Roll back to %s (%s)" "$ROLLBACK_DESC" "$backup_time")
GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT loglevel=0"
if test -e "$dtbo_dirname/dtbo.img"; then
    dtbo=1
fi

if [ -f /etc/.bootinfo ] && [ -f /usr/bin/kybackup ]; then
    RECOVERY_DEV_UUID=$(cat /etc/.bootinfo | grep -Ev "^#|^$" | grep RECOVERY_DEV_UUID | awk '{split($0,arr,"=");print arr[2]}')
    SNAPSHOT_ENABLED=$(cat /etc/.bootinfo | grep -Ev "^#|^$" | grep SNAPSHOT_ENABLED | awk '{split($0,arr,"=");print arr[2]}')
    ROLLBACK_ENABLED=$(cat /etc/.bootinfo | grep -Ev "^#|^$" | grep ROLLBACK_ENABLED | awk '{split($0,arr,"=");print arr[2]}')
    if [ "x$ROLLBACK_ENABLED" = "x" ]; then
	return;
    elif [ -n "$RECOVERY_DEV_UUID" ] && [ $SNAPSHOT_ENABLED -eq 1 ] && [ $ROLLBACK_ENABLED -eq 1 ]; then
        linux_entry "$menu_entry" "${version}" "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}  rollback-backup"
    fi
fi
