%define buildforkernels akmod
%define debug_package %{nil}
%define short_name system76
%define upstream_name %{short_name}-dkms

Name:           %{short_name}-kmod
Version:        1.0.6
Release:        3%{?dist}
#TODO: Enhance Summary
Summary:        Kernel module for hotkeys, keyboard backlit and fan control on System76 laptops

License:        GPLv2+
URL:            https://github.com/pop-os/%{upstram_name}
#TODO: How to deal with system76-dkms-1.0.6.tar.gz vs 1.0.6.tar.gz?
Source0:        https://github.com/pop-os/%{upstream_name}/archive/%{version}.tar.gz
Patch0:         hyperbook-nh5-nh7.patch
Patch1:         kernel-6.11.patch
Patch2:         kernel-6.12.patch
BuildRequires:  kmodtool

%{expand:%(kmodtool --target %{_target_cpu} --repo rpmfusion --kmodname %{name} %{?buildforkernels:--%{buildforkernels}} %{?kernels:--for-kernels "%{?kernels}"} 2>/dev/null) }

%description
System76 DKMS is a kernel module which controls some of the hotkeys (including
those keyboard backlit related) and allows for custom fan control on newer
System76 laptops.

This package contains the system76 kernel module for kernel %{kversion}.


%package common
Summary:        Common files for system76 kernel module
Requires:       %{name} = %{version}-%{release}
BuildArch:      noarch

%description common
This package contains the common files (license, documentation, etc.) for the
system76 kernel module.


%prep
%setup -q -T -c
tar xf %{SOURCE0}
cd %{upstream_name}-%{version}
%patch 0 -p1
%patch 1 -p1
%patch 2 -p1
cd ..

# For common package
cp %{upstream_name}-%{version}/{README.md,LICENSE} .

%{?kmodtool_check}

kmodtool --target %{_target_cpu} --repo %{repo} --kmodname %{name} %{?buildforkernels:--%{buildforkernels}} %{?kernels:--for-kernels "%{?kernels}"} 2>/dev/null

for kernel_version in %{?kernel_versions}; do
    cp -al %{upstream_name}-%{version} _kmod_build_${kernel_version%%___*}
done


%build
for kernel_version in %{?kernel_versions}; do
    make %{?_smp_mflags} \
        -C "${kernel_version##*___}" \
        M=${PWD}/_kmod_build_${kernel_version%%___*} \
        modules
done


%install
for kernel_version in %{?kernel_versions}; do
    install -D -m 0755 _kmod_build_${kernel_version%%___*}/%{short_name}.ko \
        %{buildroot}%{kmodinstdir_prefix}/${kernel_version%%___*}/%{kmodinstdir_postfix}/%{short_name}.ko
done

%{?akmod_install}


%files common
%doc README.md
%license LICENSE


%changelog
* Thu Dec 12 2024 Marcin Zajaczkowski <mszpak ATT wp DOTT pl> - 1.0.6-3
- Backport support for Linux 6.12 - https://github.com/pop-os/system76-dkms/pull/71

* Sun Nov 10 2024 Marcin Zajaczkowski <mszpak ATT wp DOTT pl> - 1.0.6-2
- Backport support for Linux 6.11 - https://github.com/pop-os/system76-dkms/pull/68

* Sun Jan 12 2020 Marcin Zajaczkowski <mszpak ATT wp DOTT pl> - 1.0.6-1
- Initial RPM version based on bbswitch specification by Andrew Gunnerson
- Included patch with support for Hyperbook NH5 and NH7