#!/usr/bin/env bash
#shellcheck disable=SC2034

# Copyright 2023-2024 Gentoo Authors
# This script is installed by sys-kernel/installkernel, it is executed by
# systemd's kernel-install, NOT by the traditional installkernel. I.e. this
# plugin is run when the systemd USE flag is enabled or
# SYSTEMD_KERNEL_INSTALL=1 is set in the environment.

COMMAND="${1}"
KERNEL_VERSION="${2}"
BOOT_DIR_ABS="${3}"
KERNEL_IMAGE="${4}"

if [[ ${KERNEL_INSTALL_LAYOUT} != "grub" && ${KERNEL_INSTALL_LAYOUT} != "uki" ]]; then
	exit 0
fi

# kernel-install insists on using the EFI partition as BOOT_ROOT. And ignores
# the variable if it does not point to the root of a FAT partition.
# This is what we want for systemd-boot, but it is definitely not what we want
# for GRUB. grub-install (by default) installs a grub efi executable that
# always loads /boot/grub/grub.cfg, this will break if the BOOT_ROOT is not
# /boot which may be the case if the ESP is not mounted there.
#
# Instead ignore what kernel-install tells us, and mirror what the -systemd
# variant of this plugin does and always install to /boot
KERNEL_INSTALL_BOOT_ROOT="/boot"

: "${GRUB_CFG:=${KERNEL_INSTALL_BOOT_ROOT}/grub/grub.cfg}"

if [[ -f /etc/os-release ]]; then
	# shellcheck source=/dev/null
	. /etc/os-release
elif [[ -f /usr/lib/os-release ]]; then
	# shellcheck source=/dev/null
	. /usr/lib/os-release
fi

# Set sane default if no or broken os-release
: "${NAME:=Linux}"

# The default GRUB_LINUX_KERNEL_GLOBS from /etc/grub/10_linux expanded
# with efistub dir
: "${GRUB_LINUX_KERNEL_GLOBS:="
	/vmlinuz-* /vmlinux-* /kernel-*
	/efi/EFI/${NAME}/vmlinuz-*.efi
	/efi/EFI/${NAME}/vmlinux-*.efi
	/efi/EFI/${NAME}/kernel-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/vmlinuz-*
	${KERNEL_INSTALL_BOOT_ROOT}/vmlinux-*
	${KERNEL_INSTALL_BOOT_ROOT}/kernel-*
	${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}/vmlinuz-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}/vmlinux-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}/kernel-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/EFI/EFI/${NAME}/vmlinuz-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/EFI/EFI/${NAME}/vmlinux-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/EFI/EFI/${NAME}/kernel-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/efi/EFI/${NAME}/vmlinuz-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/efi/EFI/${NAME}/vmlinux-*.efi
	${KERNEL_INSTALL_BOOT_ROOT}/efi/EFI/${NAME}/kernel-*.efi
"}"
export GRUB_LINUX_KERNEL_GLOBS

if [[ ${COMMAND} == add || ${COMMAND} == remove ]]; then
	# do nothing if somehow GRUB is not installed
	if ! command -v grub-mkconfig >/dev/null; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo \
			"grub-mkconfig command not available"
		exit 0
	fi

	if [[ -f ${GRUB_CFG} ]]; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo \
			"Backing up existing grub config as ${GRUB_CFG}~"
		cp --dereference --preserve=all "${GRUB_CFG}"{,~} || { echo "Failed to save existing config" && exit 1; }
	fi

	[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo \
		"Generating new GRUB config as ${GRUB_CFG}"
	dname="$(dirname "${GRUB_CFG}")"
	mkdir -p "${dname}" || { echo "Failed to mkdir ${dname}" && exit 1; }
	# Exit non-fatally to ensure emerge does not fail completely in containers
	grub-mkconfig -o "${GRUB_CFG}" || { echo "grub-mkconfig failed" && exit 0; }
fi
