# A few cross-distro definitions:
%kernel_module_package_release 1
%kernel_module_package_buildreqs module-init-tools kernel-syms

# Defines %flavors_to_build and %kernel_source() as a side effect.
%_kernel_module_package(n:v:r:t:f:Xp:b) \
%{expand:%( \
	subpkg=%{-t*}%{!-t:/usr/lib/rpm/kernel-module-subpackage} \
	echo "%%define _suse_kernel_module_subpackage(n:v:r:f:p:b) %%{expand:%%(cd %_sourcedir; cat $subpkg; echo %%%%nil)}" \
	flavors_to_build= \
	flavors="%*" \
	for flavor in $(ls /usr/src/linux-obj/%_target_cpu 2>/dev/null); do \
	    case " $flavors " in \
	    (*" $flavor "*) \
		[ -n "%{-X}" ] && continue ;; \
	    (*) \
		[ -z "%{-X}" -a -n "$flavors" ] && continue ;; \
	    esac \
	    krel=$(make -s -C /usr/src/linux-obj/%_target_cpu/$flavor kernelrelease) \
	    kver=${krel%%-*} \
	    flavors_to_build="$flavors_to_build $flavor" \
	    echo "%%_suse_kernel_module_subpackage -n %{-n*}%{!-n:%name}-kmp -v %{-v*}%{!-v:%version} -r %{-r*}%{!-r:%release} %{-p} %{-b} $flavor $kver" \
	done \
	echo "%%global flavors_to_build${flavors_to_build:-%%nil}" \
	echo "%%{expand:%%(test -z '%flavors_to_build' && echo %%%%internal_kmp_error)}" \
	echo "%%global kernel_source() /usr/src/linux-obj/%_target_cpu/%%%%{1}" \
	echo "%%global kernel_module_package_moddir() updates" \
	\
	echo "%package -n %{-n*}%{!-n:%name}-kmp-_dummy_" \
	echo "Version: %version" \
	echo "Summary: %summary" \
	echo "Group: %group" \
	echo "%description -n %{-n*}%{!-n:%name}-kmp-_dummy_" \
	)}

# kernel_module_package: simply pass on all options and arguments.
%kernel_module_package(n:v:r:t:f:xp:b) \
	%{expand:%%_kernel_module_package %{-x:-X} %{-n} %{-v} %{-r} %{-t} %{-f} %{-p} %*}

# suse_kernel_module_package: invert the meaning of the -x flag. (You are not
# supposed to understand why a simple %{-x:}%{!-x:-x} won't work.)
%suse_kernel_module_package(n:v:r:s:f:xp:b) \
	%{expand:%%_kernel_module_package %{-x: }%{!-x:-X} %{-n} %{-v} %{-r} %{-s:-t %{-s*}} %{-f} %{-p} %*}
