#!/bin/sh

quiet_boot="0"
. "$pkgdatadir/grub-mkconfig_lib"

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

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

if [ -z "$DEEPIN_AB_RECOVERY_BACKUP_UUID" ]; then
    exit 0
fi

GRUB_DEVICE=$DEEPIN_AB_RECOVERY_BACKUP_DEVICE
GRUB_DEVICE_UUID=$DEEPIN_AB_RECOVERY_BACKUP_UUID
boot_device_id=$GRUB_DEVICE_UUID

LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}

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 ${dtbo_rel_dirname}/dtbo.img
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="$DEEPIN_AB_RECOVERY_LINUX"
initrd="$DEEPIN_AB_RECOVERY_INITRD"
basename=`basename $linux`
dirname=`dirname $linux`
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}"

config=
for i in "${boot_dirname}/config-${version}" "${boot_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 -n "${initrd}" ; then
  gettext_printf "Found initrd image: %s\n" "${dirname}/${initrd}" >&2
elif test -z "${initramfs}" ; then
  # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
  # no initrd or builtin initramfs, it can't work here.
  linux_root_device_thisversion=${GRUB_DEVICE}
fi

dtbo_dirname=`dirname $dirname`
rel_dirname=`make_system_path_relative_to_its_root $dirname`
dtbo_rel_dirname=`make_system_path_relative_to_its_root $dtbo_dirname`
backup_time=$(date '+%Y/%-m/%-d %T' -d @$DEEPIN_AB_RECOVERY_BACKUP_TIME)
menu_entry=$(printf "$(gettext -d deepin-ab-recovery 'Roll back to %s (%s)')" "$DEEPIN_AB_RECOVERY_OS_DESC" "$backup_time")

if test -e "$dtbo_dirname/dtbo.img"; then
	dtbo=1
fi


linux_entry "$menu_entry" "${version}" "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
