#!/bin/sh
set -e

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"

. "$pkgdatadir/grub-mkconfig_lib"

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

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

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

linux_entry ()
{
  os="$1"
  version="$2"
  type="$3"

  OS_NAME="$(eval $(grep ^NAME= /etc/os-release) ; echo ${NAME})"
  OS_VERS="$(eval $(grep ^VERSION= /etc/os-release) ; echo ${VERSION})"
  title=$(printf '%s Linux Initialize Restore %s' \
                     "${OS_NAME}" "${OS_VERS}")

  echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-advanced-backuprestore' {" | sed "s/^/$submenu_indentation/"

  if [ x$arch = xsw_64 ]; then
    echo "	echo Loading..." | sed "s/^/$submenu_indentation/"
    echo "	set boot=(\${root})/boot/" | sed "s/^/$submenu_indentation/"
    if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    message="$(gettext_printf "Loading initial ramdisk ...")"
    initrd_path=
    for i in ${initrd}; do
      initrd_path="${initrd_path} ${rel_dirname}/${i}"
    done
    sed "s/^/$submenu_indentation/" << EOF
	echo	'$(echo "$message" | grub_quote)'
	linux.boot	\${boot}$(echo $initrd_path)
EOF
  message="$(gettext_printf "Loading Linux %s ..." ${version})"
  sed "s/^/$submenu_indentation/" << EOF
	echo	'$(echo "$message" | grub_quote)'
	linux.vmlinux	\${boot}${rel_dirname}/${basename} root=${linux_root_device_thisversion} ${swargs}
	boot
EOF

  fi
  if test -n "${fdt}" ; then
    message="$(gettext_printf "Loading fdt ...")"
    sed "s/^/$submenu_indentation/" << EOF
	echo	'$(echo "$message" | grub_quote)'
	devicetree	${rel_dirname}/${fdt}
EOF
  fi
  sed "s/^/$submenu_indentation/" << EOF
}
EOF
  else
    if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
        echo "    load_video" | sed "s/^/$submenu_indentation/"
        if grep -qx "CONFIG_FB_EFI=y" "${config}" 2> /dev/null \
            && grep -qx "CONFIG_VT_HW_CONSOLE_BINDING=y" "${config}" 2> /dev/null; then
            echo "        set gfxpayload=keep" | sed "s/^/$submenu_indentation/"
        fi
    else
        if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
            echo "        load_video" | sed "s/^/$submenu_indentation/"
        fi
        echo "    set gfxpayload=$GRUB_GFXPAYLOAD_LINUX" | sed "s/^/$submenu_indentation/"
    fi

    echo "        insmod gzio" | 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
    message="$(gettext_printf "Loading Linux %s ..." ${version})"
    sed "s/^/$submenu_indentation/" << EOF
          echo    '$(echo "$message" | grub_quote)'
          linux   ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
EOF
    if test -n "${initrd}" ; then
      # TRANSLATORS: ramdisk isn't identifier. Should be translated.
      message="$(gettext_printf "Loading initial ramdisk ...")"
      initrd_path=
      for i in ${initrd}; do
        initrd_path="${initrd_path} ${rel_dirname}/${i}"
      done
      sed "s/^/$submenu_indentation/" << EOF
          echo    '$(echo "$message" | grub_quote)'
          initrd  $(echo $initrd_path)
EOF
    fi
    if test -n "${fdt}" ; then
      message="$(gettext_printf "Loading fdt ...")"
      sed "s/^/$submenu_indentation/" << EOF
          echo    '$(echo "$message" | grub_quote)'
          devicetree      ${rel_dirname}/${fdt}
EOF
    fi
    sed "s/^/$submenu_indentation/" << EOF
}
EOF
    fi
}
if [ "`mount | grep 'backup'`" ] || [ "`blkid | grep backup`" ]; then
  submenu_indentation=""
  for i in /boot/vmlinuz-*;do
    if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
  done

  linux=`version_find_latest $list`

  if [ -z ${linux} ];then
    gettext_printf "not found valid vmlinuz %s\n" "/ missing" >&2
      return
  fi

  gettext_printf "Found linux image: %s\n" "$linux" >&2

  basename=`basename $linux`
  dirname=`dirname $linux`
  version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
  rel_dirname=`make_system_path_relative_to_its_root $dirname`
  linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"

  #backup restore
  backuppart="`blkid |grep 'backup'|cut -d ':' -f 1`"
  if [[ ${backuppart:0:12} == "/dev/mapper/" ]];then
    backuplvm=$(echo ${backuppart:12} | sed 's/-backup/\/backup/' | sed 's/--/-/')
  elif [[ ${backuppart:0:14} == "/dev/kylinsec/" ]];then
    backuplvm=${backuppart:5}
  fi
  backuplvm=$(echo $backuplvm | sed 's/\//\\\ \//g'|sed 's/ //g')
  backuplvm="rd.lvm.lv="${backuplvm}

  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 "initramfs-${version}.img";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
    if [ "x${GRUB_ENABLE_BLSCFG}" != "xtrue" ]; then
           gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
    fi
  fi

  if test -z "${initramfs}" && test -z "${initrd_real}" ; then
    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

  backup_md_uuid=`cat /etc/mdadm.conf 2> /dev/null | grep backup | awk '{print $5}' | awk -F "=" '{print $2}'`
  if [ ! -z $backup_md_uuid ];then
      mdarg="rd.md.uuid=$backup_md_uuid"
  else
      mdarg=""
  fi

  args="${GRUB_CMDLINE_LINUX} rd.break=kyrestore $mdarg $backuplvm "

  swargs="${GRUB_CMDLINE_LINUX} rd.break=kyrestore $mdarg "

  arch="$(uname -m)"

  linux_entry "${OS}" "${version}" advanced

  echo "$title_correction_code"
fi
