#!/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} == "compat" ]]; then
	# 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_ROOT="/boot"
	SUFFIX=
elif [[ ${KERNEL_INSTALL_LAYOUT} == "efistub"  ]]; then
	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}"

	# For efistub layout, we relocate /boot to the ESP
	KERNEL_INSTALL_ROOT="${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}"
	# Some vendors enforce that booted EFI executables have the .efi suffix.
	# We also want to be able to distinguish between an UKI and a regular
	# kernel image with an .efi suffix.
	SUFFIX=".efi"
else
	exit 0
fi

if [[ ${COMMAND} == add ]]; then
	mkdir -p "${KERNEL_INSTALL_ROOT}" || exit 1

	[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Installing kernel image for ${KERNEL_VERSION}..."
	install -m 0644 "${KERNEL_IMAGE}" "${KERNEL_INSTALL_ROOT}/kernel-${KERNEL_VERSION}${SUFFIX}" || exit 1

	INITRD="${KERNEL_INSTALL_STAGING_AREA}/initrd"
	if [[ -f ${INITRD} ]]; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Installing initramfs image for ${KERNEL_VERSION}..."
		install -m 0644 "${INITRD}" "${KERNEL_INSTALL_ROOT}/initramfs-${KERNEL_VERSION}.img" || exit 1
	fi

	# Copy microcode to ESP so we can add it to the efistub entry
	for CPU in intel amd; do
		if [[ -f "${KERNEL_INSTALL_STAGING_AREA}/microcode-${CPU}" ]]; then
			[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Copying microcode for ${CPU} CPU..."
			install -m 0644 "${KERNEL_INSTALL_STAGING_AREA}/microcode-${CPU}" "${KERNEL_INSTALL_ROOT}/${CPU}-uc.img" || exit 1
		fi
	done
elif [[ ${COMMAND} == remove ]]; then
	[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Removing kernel and initramfs image for ${KERNEL_VERSION}..."
	rm -f \
		"${KERNEL_INSTALL_ROOT}/kernel-${KERNEL_VERSION}${SUFFIX}" \
		"${KERNEL_INSTALL_ROOT}/initramfs-${KERNEL_VERSION}.img" || exit 1
fi
