#!/bin/sh
#
# Copyright (C) 2014 Colin Walters <walters@verbum.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2 of the licence or (at
# your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with this library. If not, see <https://www.gnu.org/licenses/>.

# Gracefully exit if ostree is not installed, or there's
# no system repository initialized.

# #need bind before update-grub 
# mount --bind /boot/ /sysroot/boot/
# mount --bind /boot/efi/ /sysroot/boot/efi/

if ! which ostree >/dev/null 2>/dev/null; then
    exit 0
fi
if ! test -d /ostree/repo; then
    exit 0
fi

# Gracefully exit if we can not find the grub2 'default' configuration as it is
# the case on new installations with bootupd where it is not needed.
if ! test -f /etc/default/grub; then
    exit 0
fi

# Gracefully exit if the grub2 configuration has BLS enabled,
# and the installed version has support for the blscfg module.
# Since there is no need to create menu entries for that case.
# See: https://src.fedoraproject.org/rpms/grub2/c/7c2bab5e98d
. /etc/default/grub

if [ -d /etc/default/grub.d ]; then
    for f in /etc/default/grub.d/*.cfg; do
        [ -f "$f" ] && . "$f"
    done
fi

if test -f /boot/grub2/.grub2-blscfg-supported && \
   test "${GRUB_ENABLE_BLSCFG}" = "true"; then
   exit 0
fi

# Make sure we're in the right environment
if ! test -n "${GRUB_DEVICE}"; then
    echo "This script must be run as a child of grub2-mkconfig" 1>&2
    exit 1
fi
ARCH=$(uname -m)

if [ "$ARCH" = "aarch64" ]; then
    export LANG=en_US.UTF-8
    export LANGUAGE=en_US:en
    export LC_ALL=en_US.UTF-8
fi

set -e

# Pick up stuff from grub's helper that we want to inject into our
# generated bootloader configuration.  Yes, this is pretty awful, but
# it's a lot better than reimplementing the config-generating bits of
# OSTree in shell script.

. /usr/share/grub/grub-mkconfig_lib

if [ -s /etc/initramfs-tools/conf.d/resume ];then
  resume_dev=$(cat /etc/initramfs-tools/conf.d/resume 2>/dev/null) || true
else
  resume_dev=$(cat /etc/fstab |grep -v "^#"|grep swap|head -n 1| awk '{print $1}') || true
fi

if [ -n "$resume_dev" ];then
  GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT resume=${resume_dev#*RESUME=}"
fi

###跨版本升级
upgrade_dir="/boot/krm-upgrade"
GRUB_CMDLINE_LINUX_UPGRADE=
if [ -d "$upgrade_dir" ];then
  GRUB_CMDLINE_LINUX_UPGRADE="krm-upgrade"
fi
export GRUB_CMDLINE_LINUX_UPGRADE

DEVICE=${GRUB_DEVICE_BOOT:-${GRUB_DEVICE}}

GRUB2_BOOT_DEVICE_ID="$(grub_get_device_id ${DEVICE})"
export GRUB2_BOOT_DEVICE_ID
GRUB2_PREPARE_ROOT_CACHE="$(prepare_grub_to_access_device ${DEVICE})"
export GRUB2_PREPARE_ROOT_CACHE

export GRUB_CMDLINE_LINUX_SECURITY

exec ostree admin instutil grub2-generate
