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

# Copyright 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 [[ -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}"
: "${ID:=linux}"

if [[ $(getconf LONG_BIT) == 64 ]]; then
	: "${REFIND_ICON:=/usr/lib64/refind/refind/icons/os_${ID}.png}"
else
	: "${REFIND_ICON:=/usr/lib/refind/refind/icons/os_${ID}.png}"
fi

if [[ ! -f ${REFIND_ICON} ]]; then
	echo "REFIND_ICON=${REFIND_ICON} not found"
	exit 1
fi

# Refind does not discover kernels in the bls layout
if [[ ${KERNEL_INSTALL_LAYOUT} == "compat" || ${KERNEL_INSTALL_LAYOUT} == "grub" ]]; then
	KERNEL_INSTALL_BOOT_ROOT="/boot"
	if [[ ${COMMAND} == add ]]; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Copying rEFInd icon file..."
		install -m 0644 "${REFIND_ICON}" "${KERNEL_INSTALL_BOOT_ROOT}/kernel-${KERNEL_VERSION}.png" || exit 1
		if [[ -L ${KERNEL_INSTALL_BOOT_ROOT}/kernel ]]; then
			ln -sf "kernel-${KERNEL_VERSION}.png" \
				"${KERNEL_INSTALL_BOOT_ROOT}/kernel.png" || exit 1
		elif [[ -f ${KERNEL_INSTALL_BOOT_ROOT}/kernel ]]; then
			install -m 0644 "${KERNEL_INSTALL_BOOT_ROOT}/kernel-${KERNEL_VERSION}.png" \
				"${KERNEL_INSTALL_BOOT_ROOT}/kernel.png" || exit 1
		fi
	elif [[ ${COMMAND} == remove ]]; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Removing rEFInd icon file..."
		rm -f "${KERNEL_INSTALL_BOOT_ROOT}/kernel-${KERNEL_VERSION}.png" || exit 1
	fi
elif [[ ${KERNEL_INSTALL_LAYOUT} == "uki" ]]; then
	UKI_DIR="${KERNEL_INSTALL_BOOT_ROOT}/EFI/Linux"
	if [[ ${COMMAND} == add ]]; then
		if [[ -f ${KERNEL_INSTALL_CONF_ROOT:-/etc/kernel}/tries ]]; then
			echo "ERROR: Boot counting not supported, but tries file exists"
			exit 1
		fi
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Copying rEFInd icon file..."
		install -m 0644 "${REFIND_ICON}" "${UKI_DIR}/${KERNEL_INSTALL_ENTRY_TOKEN}-${KERNEL_VERSION}.png" || exit 1
		if [[ -L ${UKI_DIR}/${KERNEL_INSTALL_ENTRY_TOKEN}.efi ]]; then
			ln -sf "${KERNEL_INSTALL_ENTRY_TOKEN}-${KERNEL_VERSION}.png" \
				"${UKI_DIR}/${KERNEL_INSTALL_ENTRY_TOKEN}.png" || exit 1
		elif [[ -f ${UKI_DIR}/${KERNEL_INSTALL_ENTRY_TOKEN}.efi ]]; then
			install -m 0644 "${UKI_DIR}/${KERNEL_INSTALL_ENTRY_TOKEN}-${KERNEL_VERSION}.png" \
				"${UKI_DIR}/${KERNEL_INSTALL_ENTRY_TOKEN}.png" || exit 1
		fi
	elif [[ ${COMMAND} == remove ]]; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Removing rEFInd icon file..."
		rm -f "${UKI_DIR}/${KERNEL_INSTALL_ENTRY_TOKEN}-${KERNEL_VERSION}.png" || exit 1
	fi
elif [[ ${KERNEL_INSTALL_LAYOUT} == "efistub" ]]; then
	EFISTUB_DIR="${KERNEL_INSTALL_BOOT_ROOT}/EFI/${NAME}"
	if [[ ${COMMAND} == add ]]; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Copying rEFInd icon file..."
		install -m 0644 "${REFIND_ICON}" "${EFISTUB_DIR}/kernel-${KERNEL_VERSION}.png" || exit 1
		if [[ -L ${EFISTUB_DIR}/kernel.efi ]]; then
			ln -sf "kernel-${KERNEL_VERSION}.png" \
				"${EFISTUB_DIR}/kernel.png" || exit 1
		elif [[ -f ${EFISTUB_DIR}/kernel.efi ]]; then
			install -m 0644 "${EFISTUB_DIR}/kernel-${KERNEL_VERSION}.png" \
				"${EFISTUB_DIR}/kernel.png" || exit 1
		fi
	elif [[ ${COMMAND} == remove ]]; then
		[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo "Removing rEFInd icon file..."
		rm -f "${EFISTUB_DIR}/kernel-${KERNEL_VERSION}.png" || exit 1
	fi
fi
