%patches	%_usrsrc/kernel/patches
%patches_dir	%buildroot%patches
%kernel_src	%_usrsrc/kernel/sources
%kernel_srcdir	%buildroot%kernel_src
%kernel_mainline	mainline
%kernel_latest		latest > 6.10

%base_arch	%(echo %_target_cpu | sed -e 's/^\\(i.86\\|pentium[234]\\|athlon\\(_xp\\)\\?\\|k6\\)$/i386/' -e 's/^arm.\\+$/arm/' -e 's/^aarch64$/arm64/' -e 's/^loongarch.*/loongarch/' -e 's/^mips.*/mips/' -e 's/^riscv.*/riscv/' -e 's/^ppc.*/powerpc/')

%_patch_list		%nil
%set_patch_list()	%global _patch_list %*
%add_patch_list()	%global _patch_list %_patch_list %*
%get_patch_list()	%( \
	for p in %_patch_list; do \
		dep=`rpmquery --dbpath %_dbpath --qf '%%{NAME} >= %%|SERIAL?{%%{SERIAL}:}|%%{VERSION}-%%{RELEASE}' "$p" 2>/dev/null` || dep="$p" \
		echo -n " $dep " \
	done \
)

%format_patch_list      %(rpmquery --dbpath %_dbpath --qf '\\n\\t%%{NAME}-%%{VERSION}-%%{RELEASE}' %_patch_list)

%_src_list	%nil

%source() \
Source%1: %2 \
%global _src_list %_src_list %{?3:%3/}%(f="%2"; printf "%%s" "${f##*/}") \
%nil

%__touch	/bin/touch

# Standard way for applying patches
%apply_patches() \
function in_the_patch_list() \
{ \
    for p in %_patch_list ; do \
        [ $p = $1 ] && return 0 \
    done \
    return 1 \
} \
function apply_patchset() \
{ \
    echo -e "----- Applying patchset $1 ------" \
    [ -f patches/APPLIED_$1 ] && echo "Already applied, ignoring... $1" && return 0 \
    if [ -f "%patches/apply/$1" ] ; then \
        echo "Call %patches/apply/$1" \
        /bin/sh %patches/apply/$1 %patches $1 %kversion $2 || return 1 \
    else \
        apply_recursive %patches/$1 %kversion $2 || return 1 \
    fi \
    %__touch ./patches/APPLIED_$1 \
} \
function apply_recursive() \
{ \
    local patchset \
    local i \
    for i in `ls -1 $1` ; do \
        [ -s $1/$i ] || continue \
        if [ -f $1/dont_apply_to_${2}_$i ] ; then \
            echo "Denied for the kernel version $2 ..." \
            continue \
        fi \
        if [ -d $1/$i ] ; then \
            patchset="${i##??_}" \
            if [ "$patchset" = "common" ] ; then \
                echo "Applying common patches ..." \
                apply_recursive "$1/$i" "$2" "$3" || return 1 \
                continue \
            fi \
            if [ "$patchset" = "$3" ] ; then \
                echo "Applying only for the label $3" \
                apply_recursive "$1/$i" "$2" "$3" || return 1 \
                continue \
            fi \
            if [ -z "${patchset##apply_to_*}" ] ; then \
                if [ "$patchset" = "apply_to_$2" ] ; then \
                    echo "Applying only for the kernel version $2 ..." \
                    apply_recursive "$1/$i" "$2" "$3" || return 1 \
                fi \
                continue \
            fi \
            if [ -z "${patchset##not_*}" ] ; then \
                patchset="${patchset##not_}" \
                if in_the_patch_list $patchset ; then \
                    echo "Skipping patches incompatible with $patchset" \
                else \
                    echo "Applying patches for kernels without $patchset" \
                    apply_recursive "$1/$i" "$2" "$3" || return 1 \
                fi \
                continue \
            fi \
            if in_the_patch_list $patchset ; then \
                apply_patchset $patchset "$3" || return 1 \
                if [ -f patches/APPLIED_$patchset ] ; then \
                    apply_recursive "$1/$i" "$2" "$3" || return 1; \
                fi \
            fi \
            continue \
        fi \
        echo "Applying patch $i ..." \
        %__cat $1/$i | patch -p1 -b || return 1 \
    done \
} \
set +x \
[ -d './patches' ] || %__mkdir './patches' \
for f in %_patch_list ; do \
    apply_patchset $f %1 || exit 1 \
done \
%nil

# Standard way for installing patches
%install_patches \
%__mkdir_p %patches_dir/%patch_name \
for i in %_src_list ; do \
    %__install -p -D -m644 %_sourcedir/"${i##*/}" %patches_dir/%patch_name/"$i" \
done \
%nil


# Standard %%post script for kernel-image-* packages
%post_kernel_image	%{error:%%post_kernel_image is obsolete, rely on /usr/lib/rpm/boot_kernel.filetrigger instead}

# Standard %%preun script for kernel-image-* packages
%preun_kernel_image	%{error:%%preun_kernel_image is obsolete, rely on /usr/lib/rpm/boot_kernel.filetrigger instead}

# Standard %%post script for kernel-headers-* packages
%post_kernel_headers() 	%{warning %%post_kernel_headers is obsolete, rely on /usr/lib/rpm/kheaders.filetrigger instead}

# Standard %%postun script for kernel-headers-* packages
%postun_kernel_headers() %{warning %%postun_kernel_headers is obsolete, rely on /usr/lib/rpm/kheaders.filetrigger instead}

# Standard %%post script for kernel-modules-* packages
%post_kernel_modules()	%{warning %%post_kernel_modules is obsolete, rely on /usr/lib/rpm/boot_kernel.filetrigger instead}

# Standard %%postun script for kernel-modules-* packages
%postun_kernel_modules()	%{warning %%postun_kernel_modules is obsolete, rely on /usr/lib/rpm/boot_kernel.filetrigger instead}

# Kernel per-arch configuration
%_kernel_config_offset 10000
%_kernel_arches %nil

%set_kernel_arches() %global _kernel_arches %* \
%( \
    echo "ExclusiveArch: %*" \
    n=%_kernel_config_offset \
    for arch in %_kernel_arches; do \
        echo "Source$n: config-%kernel_base_version-%flavour.$arch" \
        n=$(($n+1)) \
    done \
)

%get_kernel_config() %{expand:%%SOURCE%( \
    n=%_kernel_config_offset \
    for arch in %_kernel_arches; do \
	if [ "$arch" = "%1" ]; then \
	    echo $n \
	    exit \
	fi \
	n=$((n+1)) \
    done \
    echo "__No_such_architecture__%{1}" \
)}

%_kernel_version_code() %( set -f \
     IFS=. \
     f(){ v1=$1 && shift || v1=0; v2=$1 && shift || v2=0; v3=$1 && shift || v3=0; s=.e$1 && shift || s=; echo "$(($v1 * 65536 + $v2 * 256 + $v3))$s"; } \
     set -- %{1} \
     f $* )

%setup_kernel_module() \
    %global kflavour %{1} \
    %{expand: %(%_rpmlibdir/query-kEVR.sh --dbpath %_dbpath kernel-headers-modules-%{1})} \
    %{?!kepoch: %global kepoch %nil} \
    %{?!kversion: %global kversion %nil} \
    %{?!krelease: %global krelease %nil} \
    %setup_kernel_module_from_globals
%setup_kernel_module_from_globals() \
    %global kbuildrelease %( a="%{krelease}"; echo "${a#alt}") \
    %global kcode %_kernel_version_code %kversion \
    \
%requires_kimage \
BuildRequires: kernel-headers-modules-%kflavour = %{?kepoch}%kversion-%krelease

# For writing Requires in modules:
%requires_kimage %{?kimage:Requires(pre,postun): %kimage}

# A macro to produce a specific kind of checkinstall subpkg.
%update_kernel_modules_checkinstall() \
%define stem %(a="%{1}"; a="${a#kernel-modules-}"; a="${a%%-%kflavour}"; echo "$a")\
%define subname kernel-update-%stem-%kflavour-checkinstall\
%package -n %subname\
Summary: Immediately test that update-kernel would suggest to install this modules pkg\
Group: Other\
Requires(pre): %{1} = %EVR\
Requires(pre): update-kernel\
\
%description -n %subname\
%summary.\
\
To test this, this package requires %{1} (makes it installed),\
and then the %%pre script checks that update-kernel sees our modules pkg in\
the APT database as a module related to the %%kflavour kernel-image.\
\
%pre -n %subname\
set -o pipefail\
exec <&-\
%_sbindir/update-kernel -t %kflavour -f --dry-run 2>&1 \\\
  | grep -Fe '%stem is installed, trying to update' >/dev/null\
\
%files -n %subname\
%nil

#==========================================
#local variables:
#mode: rpm-spec
#fill-column: 80
#end:
