#!/bin/sh

DEFAULT_DIST="altlinux"

if [ ! -d /sys/firmware/efi ]; then
    echo "Not booted in EFI mode, unable to update EFI GRUB"
    exit 0
fi

GRUB_SYSCONF=/etc/sysconfig/grub2

if [ ! -f "$GRUB_SYSCONF" ]; then
    echo "There is no $GRUB_SYSCONF, nothing to do"
    exit 0
fi

. "$GRUB_SYSCONF"

DIST="${GRUB_BOOTLOADER_DIST:-$DEFAULT_DIST}"
EFI_DIR="/boot/efi"
BOOTCSV_DIR="/usr/lib/shim"
GRUB_CFG="$EFI_DIR/EFI/$DIST/grub.cfg"

GRUB_REMOVABLE=

# Use .sbat section to determine ALT efi binary
is_alt_efi_binary() {
    local expected_package_name="$1"
    local efi_binary="$2"

    [ -r "$efi_binary" ] || return 1

    local sbat_component sbat_gen
    local vendor_name vendor_package_name vendor_version vendor_url
    local last_sbat_entry

    last_sbat_entry="$(grub-dumpsbat "$efi_binary" 2>/dev/null |\
                          tr -d '\0' | tail -n1)"

    IFS=, read -r sbat_component sbat_gen vendor_name vendor_package_name \
          vendor_version vendor_url <<< "$last_sbat_entry"

    if [ "$vendor_name" = "ALT Linux" ] && \
           [ "$vendor_package_name" = "$expected_package_name" ]; then
        return 0
    else
        return 1
    fi
}


echo "Searching for ALT Linux GRUB efi image to update"

do_update=0
if is_alt_efi_binary grub $EFI_DIR/EFI/BOOT/BOOT*.EFI; then
    do_update=1
    GRUB_REMOVABLE="--removable"
    echo "Found $EFI_DIR/EFI/BOOT/BOOT*.EFI (ALT Linux GRUB)"
else
    echo "Skipping $EFI_DIR/EFI/BOOT/BOOT*.EFI (not ALT Linux GRUB)"
fi

if is_alt_efi_binary shim $EFI_DIR/EFI/BOOT/BOOT*.EFI && \
        is_alt_efi_binary grub $EFI_DIR/EFI/BOOT/grub*.efi; then
    do_update=1
    GRUB_REMOVABLE="--removable"
    echo "Found $EFI_DIR/EFI/BOOT/grub*.efi (ALT Linux GRUB)"
else
    echo "Skipping $EFI_DIR/EFI/BOOT/grub*.efi (not ALT Linux GRUB)"
fi

if is_alt_efi_binary grub $EFI_DIR/EFI/$DIST/grub*.efi; then
    do_update=1

    # "--removable" and "--force-extra-removable" are mutually exclusive options,
    # so use "--removable" for systems which lack NVRAM only.
    # Try to use "--force-extra-removable" in all other cases to workaround
    # some buggy UEFI firmwares which lose vendor UEFI boot variable when no
    # EFI/BOOT directory is present. (but only when BOOT<efiarch>.CSV exists).
    # Use --force-extra-removable only if /usr/lib/shim/BOOT*.CSV is available
    # to prevent fb*.efi cyclic reboot on system being updated.
    if [ "$(stat $BOOTCSV_DIR/BOOT*.CSV > /dev/null 2>&1; echo $?)" = "0" ]; then
        GRUB_REMOVABLE="--force-extra-removable"
    else
        GRUB_REMOVABLE=""
    fi
    echo "Found $EFI_DIR/EFI/$DIST/grub*.efi (ALT Linux GRUB)"
else
    echo "Skipping $EFI_DIR/EFI/$DIST/grub*.efi (not ALT Linux GRUB)"
fi

if [ "$do_update" = "0" ]; then
    echo
    echo "ALT Linux EFI GRUB image was not found, nothing to update. Please run:"
    echo "grub-install && grub-efi-autoupdate"
    echo
    echo "If your system lacks NVRAM or you are getting persistent"
    echo "errors, please run:"
    echo "grub-install --removable && grub-efi-autoupdate"
    echo
    exit 0
fi

case "$GRUB_AUTOUPDATE_FORCE" in
    true|yes) GRUB_FORCE="--force";;
esac

echo "Updating grub in $EFI_DIR${GRUB_REMOVABLE:+ with $GRUB_REMOVABLE}"
grub-install $GRUB_FORCE $GRUB_REMOVABLE
