#! /bin/sh
set -e

# grub-mkconfig helper script.

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

. "$pkgdatadir/grub-mkconfig_lib"

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

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

if [ -f /etc/default/locale ];then
  LOCALE=$(cat /etc/default/locale | grep ^LANG=.*$ | head -n 1 | sed 's/"//g')
  LOCALE=${LOCALE#LANG=}
fi

if [ -z "${LOCALE}" ] && [ -f /etc/deepin-installer.conf ];then
  LOCALE=$(cat /etc/deepin-installer.conf | grep ^DI_LOCALE=.*$ | head -n 1 | sed 's/"//g')
  LOCALE=${LOCALE#DI_LOCALE=}
fi

LOCALE=${LOCALE:-en_US.UTF-8}

NEED_BOOT=false
case $(uname -m) in 
  loongson | mips*)
    NEED_BOOT=true
  ;;
esac

title_correction_code=

# disable locale if it's not x86
machine=`uname -m`
if [ x"$machine" != x"x86_64" ];then
  export LC_ALL=C
fi

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

  echo "menuentry '$(gettext_printf "Deepin Restore")' ${CLASS} \$menuentry_id_option 'gnulinux-simple-recovery' {" | sed "s/^/$submenu_indentation/"
  if [ "$NEED_BOOT" != "true" ];then
  echo "	insmod gzio" | sed "s/^/$submenu_indentation/"
  fi
  prepare_boot_cache="$(prepare_grub_to_access_device ${LIVE_DEVICE_BOOT} | grub_add_tab)"
  printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
  if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
    message="$(gettext_printf "Loading Linux %s ..." ${version})"
    sed "s/^/$submenu_indentation/" << EOF
EOF
  fi
    sed "s/^/$submenu_indentation/" << EOF
	linux	${rel_dirname}/${basename} ro ${args}
EOF
  if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
      message="$(gettext_printf "Loading initial ramdisk ...")"
      sed "s/^/$submenu_indentation/" << EOF
EOF
    fi
    sed "s/^/$submenu_indentation/" << EOF
	initrd	${rel_dirname}/${initrd}
EOF
  fi
  if [ "$NEED_BOOT" = "true" ];then
    sed "s/^/$submenu_indentation/" << EOF
	boot
EOF
  fi
  sed "s/^/$submenu_indentation/" << EOF
}
EOF
}

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
	# fix error when no vmlinuz found
  gettext_printf "not found valid vmlinuz %s\n" "/ missing" >&2
	return
fi

gettext_printf "Found  image: %s\n" "$linux" >&2
basename=`basename $linux`
dirname=`dirname $linux`
version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`

initrd=
for i in "initrd.img-${version}"; do
  if test -e "${dirname}/${i}" ; then
    initrd="$i"
    break
  fi
done

if test -n "${initrd}" ; then
  gettext_printf "Found initrd image: %s\n" "${dirname}/${initrd}" >&2
fi

args="boot=live toram=filesystem.squashfs splash quiet locales=${LOCALE}"

arch=$(uname -m)
if [ "${arch}" = "aarch64" ]; then
    args="$args console=tty"
fi

# for Loongson 3B3000, should remove kernel param `quiet` or the kernel will stuck with `no rootfs specified` and panic.
lscpu | grep 'name:.*3B3000.*' &&  args=$(echo $args | sed 's#quiet##')


if findmnt -n /boot 2>&1 >/dev/null ;then
  LIVE_DEVICE_BOOT=$(findmnt -n /boot | awk '{print $2}')
  rel_dirname=""
else
  LIVE_DEVICE_BOOT=$(findmnt -n / | awk '{print $2}')
  rel_dirname="/boot"
fi

ROOT_UUID=$(findmnt -n / -o UUID)
args="$args live-media-path=usr/doppel root=UUID=${ROOT_UUID}"

linux_entry "${OS}" "${version}" simple 

echo "$title_correction_code"
