#!/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_VERBOSE}" -gt 0 ]; then
    LOGLEVEL="info"
else
    LOGLEVEL="error"
fi

if [[ ${KERNEL_INSTALL_LAYOUT} == "uki" ]]; then
	if [[ -f ${KERNEL_INSTALL_CONF_ROOT:-/etc/kernel}/tries ]]; then
		echo "ERROR: Boot counting not supported, but tries file exists"
		exit 1
	fi

	if ! command -v kernel-bootcfg >/dev/null; then
		echo "ERROR: kernel-bootcfg command not available"
		exit 1
	fi

	if command -v systemctl >/dev/null; then
		if [[ $(systemctl is-enabled kernel-bootcfg-boot-successful.service) != enabled ]]; then
			echo "ERROR: kernel-bootcfg-boot-successful service not enabled"
			exit 1
		fi
	else
		[[ "${KERNEL_INSTALL_VERBOSE}" -gt 0 ]] && echo \
			"WARNING: Cannot check if kernel-bootcfg-boot-successful service is enabled"
	fi

	UKI="${KERNEL_INSTALL_BOOT_ROOT}/EFI/Linux/${KERNEL_INSTALL_ENTRY_TOKEN}-${KERNEL_VERSION}.efi"

	if [[ ${COMMAND} == add ]]; then
		if [[ ! -f ${UKI} ]]; then
			echo "layout=uki, but no UKI was found, exiting"
			exit 1
		fi

		unset PRETTY_NAME NAME VERSION_ID
		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

		if [[ -n "${PRETTY_NAME}" ]]; then
			TITLE="${PRETTY_NAME} ${KERNEL_VERSION} (UKI)"
		elif [[ -n "${NAME}" ]] && [[ -n "${VERSION_ID}" ]]; then
			TITLE="${NAME} ${VERSION_ID} ${KERNEL_VERSION} (UKI)"
		else
			TITLE="${KERNEL_INSTALL_ENTRY_TOKEN} ${KERNEL_VERSION} (UKI)"
		fi

		PARTLABEL="$(df "${UKI}" --output=source | tail -1 | xargs -r lsblk --output=PARTLABEL --noheadings)"
		if [[ -n ${PARTLABEL} ]]; then
			TITLE="${TITLE} on ${PARTLABEL}"
		fi

		[[ "${KERNEL_INSTALL_VERBOSE}" -gt 0 ]] && echo "Adding ${UKI} to UEFI boot entries..."
		kernel-bootcfg --loglevel "${LOGLEVEL}" --add-uki "${UKI}" --title "${TITLE}" --once || exit 1
	elif [[ ${COMMAND} == remove ]]; then
		[[ "${KERNEL_INSTALL_VERBOSE}" -gt 0 ]] && echo "Removing ${UKI} from UEFI boot entries..."
		kernel-bootcfg --loglevel "${LOGLEVEL}" --remove-uki "${UKI}" || exit 1
	fi
elif [[ ${KERNEL_INSTALL_LAYOUT} == "efistub"  ]]; then
	if ! command -v kernel-bootcfg >/dev/null; then
		echo "ERROR: kernel-bootcfg command not available"
		exit 1
	fi

	if command -v systemctl >/dev/null; then
		if [[ $(systemctl is-enabled kernel-bootcfg-boot-successful.service) != enabled ]]; then
			echo "ERROR: kernel-bootcfg-boot-successful service not enabled"
			exit 1
		fi
	else
		[[ "${KERNEL_INSTALL_VERBOSE}" -gt 0 ]] && echo \
			"WARNING: Cannot check if kernel-bootcfg-boot-successful service is enabled"
	fi

	unset PRETTY_NAME NAME VERSION_ID
	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}"

	KERNEL_IMAGE="${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}/kernel-${KERNEL_VERSION}.efi"
	INITRD="${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}/initramfs-${KERNEL_VERSION}.img"

	if [[ ${COMMAND} == add ]]; then
		if [[ ! -f ${KERNEL_IMAGE} ]]; then
			echo "No kernel image was found, exiting"
			exit 1
		fi

		KERNEL_BOOTCFG_ARGS=(
			--loglevel "${LOGLEVEL}"
			--once
		)

		# From 90-loadentry.install
		if [[ -n "${KERNEL_INSTALL_CONF_ROOT}" ]]; then
			if [[ -f "${KERNEL_INSTALL_CONF_ROOT}/cmdline" ]]; then
				BOOT_OPTIONS="$(tr -s "${IFS}" ' ' <"${KERNEL_INSTALL_CONF_ROOT}/cmdline")"
			fi
		elif [[ -f /etc/kernel/cmdline ]]; then
			BOOT_OPTIONS="$(tr -s "${IFS}" ' ' </etc/kernel/cmdline)"
		elif [[ -f /usr/lib/kernel/cmdline ]]; then
			BOOT_OPTIONS="$(tr -s "${IFS}" ' ' </usr/lib/kernel/cmdline)"
		else
			BOOT_OPTIONS="$(tr -s "${IFS}" '\n' </proc/cmdline | grep -ve '^BOOT_IMAGE=' -e '^initrd=' | tr '\n' ' ')"
		fi

		# Append microcode to cmdline if it exists
		for CPU in intel amd; do
			UCODE="${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}/${CPU}-uc.img"
			if [[ -f "${UCODE}" ]]; then
				UCODE_EFI_PATH="${UCODE#"${KERNEL_INSTALL_BOOT_ROOT}"}"
				UCODE_EFI_PATH_DOS="${UCODE_EFI_PATH//\//\\}"
				BOOT_OPTIONS="${BOOT_OPTIONS} initrd=${UCODE_EFI_PATH_DOS}"
			fi
		done

		# Append initramfs to cmdline if it exists
		if [[ -f ${INITRD} ]]; then
			INITRD_EFI_PATH="${INITRD#"${KERNEL_INSTALL_BOOT_ROOT}"}"
			INITRD_EFI_PATH_DOS="${INITRD_EFI_PATH//\//\\}"
			BOOT_OPTIONS="${BOOT_OPTIONS} initrd=${INITRD_EFI_PATH_DOS}"
		fi

		if [[ -n "${BOOT_OPTIONS}" ]]; then
			KERNEL_BOOTCFG_ARGS+=( --cmdline "${BOOT_OPTIONS}" )
		fi

		if [[ -n "${PRETTY_NAME}" ]]; then
			TITLE="${PRETTY_NAME} ${KERNEL_VERSION} (plain)"
		elif [[ -n "${NAME}" ]] && [[ -n "${VERSION_ID}" ]]; then
			TITLE="${NAME} ${VERSION_ID} ${KERNEL_VERSION} (plain)"
		else
			TITLE="${KERNEL_INSTALL_ENTRY_TOKEN} ${KERNEL_VERSION} (plain)"
		fi

		PARTLABEL="$(df "${KERNEL_IMAGE}" --output=source | tail -1 | xargs -r lsblk --output=PARTLABEL --noheadings)"
		if [[ -n ${PARTLABEL} ]]; then
			TITLE="${TITLE} on ${PARTLABEL}"
		fi

		KERNEL_BOOTCFG_ARGS+=(
			--add-uki "${KERNEL_IMAGE}"
			--title "${TITLE}"
		)

		[[ "${KERNEL_INSTALL_VERBOSE}" -gt 0 ]] && echo "Adding ${KERNEL_IMAGE} to UEFI boot entries with cmdline ${BOOT_OPTIONS}..."
		kernel-bootcfg "${KERNEL_BOOTCFG_ARGS[@]}" || exit 1
	elif [[ ${COMMAND} == remove ]]; then
		[[ "${KERNEL_INSTALL_VERBOSE}" -gt 0 ]] && echo "Removing ${KERNEL_IMAGE} from UEFI boot entries..."
		kernel-bootcfg --loglevel "${LOGLEVEL}" --remove-uki "${KERNEL_IMAGE}" || exit 1
	fi
else
	exit 0
fi
