#! /bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

arch=$(uname -m)
if [ x"$arch" = x"mips64" ];then
        GRUB_DISABLE_RECOVERY=true
        GRUB_START=boot
fi

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"
OS=GNU/Linux

title_correction_code=

linux_entry ()
{
  os="$1"
  version="$2"
  type="$3"
  args="quiet splash acpi_backlight=vendor  ${GRUB_CMDLINE_LINUX_DEFAULT}"

  echo "menuentry 'UOS Backup & Restore' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"

  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
  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}
	$GRUB_START
EOF
  fi

  if test -n "${dtbo}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; 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}
EOF
  fi
  sed "s/^/$submenu_indentation/" << EOF
}
EOF

if [ x"$arch" = x"mips64" ];then
echo "
title uos Recovery
        kernel /dev/fs/ext2@wd0$linux
        initrd /dev/fs/ext2@wd0${dirname}/$initrd
        args console=tty loglevel=0 $args " >> /boot/boot.cfg
        if grep -wqs "/boot" /proc/mounts ; then
                sed -e 's@boot/@@g' -i /boot/boot.cfg
        fi
fi
}

submenu_indentation=""
is_top_level=true

linux=/boot/recovery/vmlinuz
if test -e "${dirname}/${linux}"; then
    printf "Found deepin recovery image: %s\n" "$linux" >&2
else
    printf "Missing deepin recovery image: %s\n" "$linux" >&2
    exit
fi

basename=`basename $linux`
dirname=`dirname $linux`
rel_dirname=`make_system_path_relative_to_its_root $dirname`

initrd=rootfs.cpio.gz
if ! test -e "${dirname}/${initrd}" ; then
   gettext_printf "Cannot found deepin recovery initrd\n" >&2
   exit
fi

dtbo=
if test -e "/boot/dtbo.img"; then
   dtbo=`make_system_path_relative_to_its_root /boot/dtbo.img`
fi

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

echo "$title_correction_code"
