# A few cross-distro definitions:
%kernel_build_shell_package %{lua: \
if ((tonumber(rpm.expand("0%{?suse_version}")) > 1500) or (tonumber(rpm.expand("0%{?sle_version}")) > 150300)) then \
    print( "bash-sh" ) \
else \
    print(  "" ) \
end
}
%usrmerged %{lua: susever = rpm.expand('%{?suse_version}'); if susever ~= '' and tonumber(susever) > 1550 then print('1') else print('0') end }
%kernel_module_package_release 1
%kernel_module_package_buildreqs modutils kernel-syms kmod-compat suse-kernel-rpm-scriptlets %kernel_build_shell_package

%cpu_arch %(case %_target_cpu in \
    # from rpm --eval '%ix86' \
    (i?86 | pentium3 | pentium4 | athlon | geode) \
        echo "i386" \
        ;; \
    (aarch64) \
        echo "arm64" \
        ;; \
    (*)\
        echo "%_target_cpu" \
        ;; \
esac)

%linux_arch %(case %cpu_arch in \
    (x86_64 | i386) \
        echo "x86" \
        ;; \
    (ppc*) \
        echo "powerpc" \
        ;; \
    (s390x) \
        echo "s390" \
        ;; \
    (arm64) \
        echo "arm64" \
        ;; \
    (armv*) \
        echo "arm" \
        ;; \
    (riscv*) \
        echo "riscv" \
        ;;
    (*) \
        echo "%cpu_arch" \
        ;; \
esac)

%linux_make_arch ARCH=%linux_arch

# Defines %flavors_to_build and %kernel_source() as a side effect.
%_kernel_module_package(n:v:r:t:f:Xp:bc:) \
%{expand:%( \
	subpkg=%{-t*}%{!-t:/usr/lib/rpm/kernel-module-subpackage} \
	echo "%%define _suse_kernel_module_subpackage(n:v:r:f:p:bc) %%{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 -si -C /usr/src/linux-obj/%_target_cpu/$flavor/ %linux_make_arch kernelrelease 2>/dev/null) \
	    kver=${krel%%-*} \
	    flavors_to_build="$flavors_to_build $flavor" \
	    echo "%%_suse_kernel_module_subpackage -n %{-n*}%{!-n:%name} -v %{-v*}%{!-v:%version} -r %{-r*}%{!-r:%release} %{-f} %{-p} %{-b} %{-c:-c} $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_" \
	%{-c:
		for fmt in DER PEM; do h=$(openssl x509 -inform $fmt -fingerprint -noout -in %{-c*}); if test -n "$h"; then break; fi; done \
		cert=$(echo "$h" | sed -rn 's/^SHA1 Fingerprint=//; T; s/://g; s/(.{8}).*/\\1/p')
		: The -n expanstion in kernel-cert-subpackage only works if
		: -n is actually passed to the macro. Fix this here, so that
		: we do not have to modify the modsign-repackage script
		sed "s|@CERTS@|$cert|g; s|%%{-n.}|%{-n*}%{!-n:%name}|g" /usr/lib/rpm/kernel-cert-subpackage \
		echo "%%global __spec_install_pre %%__spec_install_pre \\\\\
  mkdir -p %%buildroot/etc/uefi/certs; openssl x509 -in %{-c*} -inform $fmt -out %%buildroot/etc/uefi/certs/${cert}.crt -outform DER" } \
	)}

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

# 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:bc:) \
	%{expand:%%_kernel_module_package %{-x: }%{!-x:-X} %{-n} %{-v} %{-r} %{-s:-t %{-s*}} %{-f} %{-p} %{-b} %{-c} %*}

