if [ -n "${__included_kopidel_boot_image_methods-}" ]; then
	return 0
fi
readonly __included_kopidel_boot_image_methods=1

is_img_efi_bootable() (
	if rpm -q grub-efi &>/dev/null; then
		return 0
	fi
	return 1
)

is_img_legacy_bootable() (
	if rpm -q grub-pc &>/dev/null; then
		return 0
	fi
	return 1
)

grub_efi_target() (
	case "$(uname -m)" in
	x86_64)       echo x86_64-efi ;;
	i?86)         echo i386-efi ;;
	aarch64)      echo arm64-efi ;;
	loongarch64)  echo loongarch64-efi ;;
	riscv64)      echo riscv64-efi ;;
	esac
)

# grub-install copies only the requested modules, but the reserve is taken
# from the whole platform directory, deliberately with room to spare.
grub_reserve_mb() (
	reserve=8
	targets=()
	if is_img_legacy_bootable; then
		targets+=( i386-pc )
	fi
	if is_img_efi_bootable; then
		targets+=( "$(grub_efi_target)" )
	fi

	for target in "${targets[@]}"; do
		if [ -d "$GRUB_LIBDIR/$target" ]; then
			(( reserve += $(du -sm "$GRUB_LIBDIR/$target" | cut -f 1) ))
		fi
	done

	echo "$reserve"
)
