#! /bin/sh
set -e

. "$pkgdatadir/grub-mkconfig_lib"

BIOS_BASE_EFI_PATH="\\EFI\\BOOT\\"
UOS_LOADER_PATH="/boot/efi/EFI/BOOT/"
UOS_LOADER_CFG_FILE="/boot/efi/EFI/BOOT/OSLoader.cfg"
UOS_FSTART_KERNEL_FILE="/boot/efi/EFI/BOOT/kernel"

# loop-AES arranges things so that /dev/loop/X can be our root device, but
# the initrds that Linux uses don't like that.
case ${GRUB_DEVICE} in
  /dev/loop/*|/dev/loop[0-9])
    GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
    # We can't cope with devices loop-mounted from files here.
    case ${GRUB_DEVICE} in
      /dev/*) ;;
      *) exit 0 ;;
    esac
  ;;
esac

gettext_printf "Clear existing fast start config in EFI/BOOT/\n" >&2
if [ -f ${UOS_LOADER_CFG_FILE} ]; then
  rm -f ${UOS_LOADER_CFG_FILE}
fi

gettext_printf "Clear existing fast start efifbimage files in EFI/BOOT/\n" >&2
if [ -f ${UOS_FSTART_KERNEL_FILE} ]; then
  rm -f ${UOS_FSTART_KERNEL_FILE}
fi

gettext_printf "Clear existing fast start initrd files in EFI/BOOT/\n" >&2
rm -f ${UOS_LOADER_PATH}initrd*


is_fstart_enable=false
if [ "x${DEEPIN_FSTART}" != "x" ]; then
  is_fstart_enable=${DEEPIN_FSTART}
fi

if [ "${is_fstart_enable}" != "true" ]; then
  gettext_printf "Fast start disable, exit!\n" >&2
  exit 0
fi

# Default to disabling partition uuid support to maintian compatibility with
# older kernels.
GRUB_DISABLE_LINUX_PARTUUID=${GRUB_DISABLE_LINUX_PARTUUID-true}

# btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
# and mounting btrfs requires user space scanning, so force UUID in this case.
if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ) \
    || ( [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
  && [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ] ) \
    || ( ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
  && ! test -e "/dev/disk/by-partuuid/${GRUB_DEVICE_PARTUUID}" ) \
    || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  LINUX_ROOT_DEVICE=${GRUB_DEVICE}
elif [ "x${GRUB_DEVICE_UUID}" = "x" ] \
    || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
  LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
else
  LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi

case x"$GRUB_FS" in
    xbtrfs)
  rootsubvol="`make_system_path_relative_to_its_root /`"
  rootsubvol="${rootsubvol#/}"
  if [ "x${rootsubvol}" != x ]; then
      GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
  fi;;
    xzfs)
  rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
  bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
  LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
  ;;
esac

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

machine=`uname -m`
case "$machine" in
    i?86) GENKERNEL_ARCH="x86" ;;
    mips|mips64) GENKERNEL_ARCH="mips" ;;
    mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
    arm*) GENKERNEL_ARCH="arm" ;;
    *) GENKERNEL_ARCH="$machine" ;;
esac

#Everytime the script execute, we firstly clear the existing fast start config files in EFI/BOOT/,
#cause we will generate new config files if current default kernel support fast start,
#or if no fast start support kernel, we need only clear all existing fast start config.

while [ "x$list" != "x" ]; do
  #detect all kernel on devices, but we only focus on the first one as it is the default kernel
  linux=`version_find_latest $list`
  case $linux in
    *.efi.signed)
      # We handle these in linux_entry.
      list=`echo $list | tr ' ' '\n' | grep -vx $linux | tr '\n' ' '`
      continue
      ;;
  esac
  #gettext_printf "Found linux image: %s\n" "$linux" >&2
  basename=`basename $linux`
  dirname=`dirname $linux`
  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}"

  if [ -f /lib/modules/${version}/efifbimage ]; then
    gettext_printf "Found linux image %s does support fast start, continue!\n" "$linux" >&2
    cp -f /lib/modules/${version}/efifbimage ${UOS_FSTART_KERNEL_FILE}
    gettext_printf "Copied efifbimage to EFI/BOOT/\n" >&2
  else
    #if the default kernel does not support fast start, we need do nothing, just break the while and quit
    gettext_printf "Found linux image %s does not support fast start, do nothing!\n" "$linux" >&2
    sync
    break
  fi

  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}"
      cp -f ${dirname}/${i} ${UOS_LOADER_PATH}
    done
    gettext_printf "Copied found initrd image %s to EFI/BOOT/\n" "$(echo $initrd_display)" >&2
  fi

  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

  #Generate new new fast start config and copy efifbimage and initrd to \\EFI\\BOOT\\

  gettext_printf "Generate new fast start files EFI/BOOT/OSLoader.cfg\n" >&2
  echo "${BIOS_BASE_EFI_PATH}kernel" > ${UOS_LOADER_CFG_FILE}

  initrd_path=
  if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    for i in ${initrd}; do
      initrd_path="${initrd_path} ${BIOS_BASE_EFI_PATH}${i}"
    done
  fi
  initrd_path=`echo ${initrd_path}`  #Remove the first and last spaces

  extra_args=`echo ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}`

  echo "initrd=${initrd_path} root=${linux_root_device_thisversion} ro ${extra_args}" >> ${UOS_LOADER_CFG_FILE}
  echo "" >> ${UOS_LOADER_CFG_FILE}
  sync
  break
done

exit 0
