#!/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"

if [ "${GRUB_DISABLE_AUTOUPDATE:-}" = true ] || \
         [ "${GRUB_DISABLE_AUTOUPDATE:-}" = yes ]; then
    exit 0
fi

DIST="${GRUB_BOOTLOADER_ID:-$DEFAULT_DIST}"

# Auto-discover ESP mount point
EFI_DIR=
for candidate in /boot/efi /efi /boot; do
    findmnt -n "$candidate" > /dev/null 2>&1 || continue
    if [ "$(grub-probe -t gpt_parttype "$candidate" 2>/dev/null)" \
             = "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" ]; then
        EFI_DIR="$candidate"
        echo "Found ESP at $candidate"
        break;
    fi
done
if [ -z "$EFI_DIR" ]; then
    # Fallback: use /boot/efi without GUID check if it is mounted
    if findmnt -n /boot/efi > /dev/null 2>&1; then
        echo "warning: /boot/efi does not look like an ESP (GPT partition type GUID mismatch); using as fallback" >&2
        EFI_DIR="/boot/efi"
    else
        echo "No ESP found at /boot/efi, /efi, or /boot" >&2
        exit 1
    fi
fi

# Determine EFI architecture suffix
case "$(uname -m)" in
    x86_64)      EFI_ARCH=x64;         EFI_ARCH_UPPER=X64 ;;
    i?86)        EFI_ARCH=ia32;        EFI_ARCH_UPPER=IA32 ;;
    aarch64)     EFI_ARCH=aa64;        EFI_ARCH_UPPER=AA64 ;;
    loongarch64) EFI_ARCH=loongarch64; EFI_ARCH_UPPER=LOONGARCH64 ;;
    riscv64)     EFI_ARCH=riscv64;     EFI_ARCH_UPPER=RISCV64 ;;
    *)
        echo "Unsupported EFI architecture: $(uname -m)" >&2
        exit 1
        ;;
esac

# 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 <<EOF
$last_sbat_entry
EOF

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

# Probe all candidate EFI binaries
echo "Probing ALT Linux EFI binaries on ESP"

dist_shim=0; dist_grub=0
boot_is_grub=0; boot_is_shim=0; boot_grub=0

if is_alt_efi_binary shim "$EFI_DIR/EFI/$DIST/shim${EFI_ARCH}.efi"; then
    dist_shim=1
    echo "  $EFI_DIR/EFI/$DIST/shim${EFI_ARCH}.efi: ALT shim"
fi
if is_alt_efi_binary grub "$EFI_DIR/EFI/$DIST/grub${EFI_ARCH}.efi"; then
    dist_grub=1
    echo "  $EFI_DIR/EFI/$DIST/grub${EFI_ARCH}.efi: ALT grub"
fi
if is_alt_efi_binary grub "$EFI_DIR/EFI/BOOT/BOOT${EFI_ARCH_UPPER}.EFI"; then
    boot_is_grub=1
    echo "  $EFI_DIR/EFI/BOOT/BOOT${EFI_ARCH_UPPER}.EFI: ALT grub"
fi
if is_alt_efi_binary shim "$EFI_DIR/EFI/BOOT/BOOT${EFI_ARCH_UPPER}.EFI"; then
    boot_is_shim=1
    echo "  $EFI_DIR/EFI/BOOT/BOOT${EFI_ARCH_UPPER}.EFI: ALT shim"
fi
if is_alt_efi_binary grub "$EFI_DIR/EFI/BOOT/grub${EFI_ARCH}.efi"; then
    boot_grub=1
    echo "  $EFI_DIR/EFI/BOOT/grub${EFI_ARCH}.efi: ALT grub"
fi

INSTALL_ARGS=

# Determine installation mode (most specific first)
if [ "$dist_shim" = 1 ] && [ "$dist_grub" = 1 ]; then
    echo "Detected: Secure Boot (non-removable)"
    INSTALL_ARGS=""
elif [ "$boot_is_shim" = 1 ] && [ "$boot_grub" = 1 ]; then
    echo "Detected: Secure Boot (removable)"
    INSTALL_ARGS="--removable"
elif [ "$boot_is_grub" = 1 ] && [ "$dist_grub" = 1 ]; then
    echo "Detected: no Secure Boot (force-extra-removable)"
    INSTALL_ARGS="--force-extra-removable"
elif [ "$dist_grub" = 1 ]; then
    echo "Detected: no Secure Boot (non-removable)"
    INSTALL_ARGS=""
elif [ "$boot_is_grub" = 1 ]; then
    echo "Detected: no Secure Boot (removable)"
    INSTALL_ARGS="--removable"
else
    echo
    echo "ALT Linux EFI GRUB image was not found, nothing to update."
    echo "To install GRUB bootloader, please run: grub-efi-install"
    echo
    echo "If your system lacks NVRAM or you are getting persistent"
    echo "errors, please run:"
    echo "grub-efi-install --removable"
    echo
    exit 0
fi

echo "Updating grub in $EFI_DIR${INSTALL_ARGS:+ with $INSTALL_ARGS}"
grub-efi-install --efi-directory="$EFI_DIR" $INSTALL_ARGS
