# The reason why this package is a separate from the main one despite using the same sources
# is because akmods use the srpm to build the kmod package, and if the kmod package is included
# in the main package, akmods will reinstall the userspace package every time the kernel is updated.

# I made the mistake of combining the specs when I ported this from RPMFusion, but to be fair
# they barely document anything.

%global buildforkernels akmod
%global debug_package %{nil}
%global commit 2c9b67072b15d903fecde67c7f269abeafee4c25
%global commitdate 20230503
%global shortcommit %(c=%{commit}; echo ${c:0:7})
%global modulename v4l2loopback
%global _description %{expand:
This module allows you to create \"virtual video devices.\" Normal \(v4l2\) applications will read these devices as if they were ordinary video devices, but the video will not be read from e.g. a capture card but instead it is generated by another application.}


Name:           %{modulename}-kmod
Summary:        Kernel module (kmod) for V4L2 loopback devices
Version:        0.15.3
Release:        4%{?dist}
License:        GPL-2.0-or-later
URL:            https://github.com/v4l2loopback/v4l2loopback
Source0:        %{url}/archive/v%{version}/%{modulename}-%{version}.tar.gz
BuildRequires:  gcc
BuildRequires:  make
BuildRequires:  help2man
BuildRequires:  systemd-rpm-macros
BuildRequires:  kmodtool
Requires:       akmods
Requires:       %{modulename} = %{?epoch:%{epoch}:}%{version}
Requires:       %{modulename}-akmod-modules = %{?epoch:%{epoch}:}%{version}
Requires:       help2man
Requires:       kernel-devel
Conflicts:      dkms-%{modulename}
Packager:       Cappy Ishihara <cappy@fyralabs.com>

%{expand:%(kmodtool --target %{_target_cpu} --repo terrapkg.com --kmodname %{modulename} %{?buildforkernels:--%{buildforkernels}} %{?kernels:--for-kernels "%{?kernels}"} 2>/dev/null) }

%description %_description

%prep
%{?kmodtool_check}

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

%setup -q -c
(cd v4l2loopback-%{version}
)

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

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


%install
for kernel_version in %{?kernel_versions}; do
 mkdir -p %{buildroot}%{kmodinstdir_prefix}/${kernel_version%%___*}/%{kmodinstdir_postfix}/
 install -D -m 755 _kmod_build_${kernel_version%%___*}/v4l2loopback.ko %{buildroot}%{kmodinstdir_prefix}/${kernel_version%%___*}/%{kmodinstdir_postfix}/
 chmod a+x %{buildroot}%{kmodinstdir_prefix}/${kernel_version%%___*}/%{kmodinstdir_postfix}/*.ko
done
%{?akmod_install}

%changelog
%autochangelog
