# Define the kmod package name here.
%define kmod_name r8152

# If kversion isn't defined on the rpmbuild line, define it here.
%if 0%{!?kversion:1} || "%{?kversion}" == ""
%{!?kversion: %define kversion 3.10.0-862.11.6.el7.%{_target_cpu}}
%endif

Name:    %{kmod_name}-kmod
Version: 2.10.0
Release: 1%{?dist}
Group:   System Environment/Kernel
License: GPLv2
Summary: %{kmod_name} kernel module(s)
URL:     http://www.realtek.com.tw/

BuildRequires: perl
BuildRequires: redhat-rpm-config
ExclusiveArch: x86_64

# Sources.
Source0:  r8152.53-%{version}.tar.bz2
Source5:  GPL-v2.0.txt
Source10: kmodtool-%{kmod_name}-el7.sh
Source20:  ELRepo-Makefile-%{kmod_name}
Patch0:  %{kmod_name}.patch
Patch1:  %{kmod_name}-h.patch

# Magic hidden here.
%if 0%{?update_kmodtool}
# Update kernel requirements in the kmodtool script.
%define kversion_required_base %( echo "%{kversion}" 2>/dev/null | grep -Eo '^[^-]+-[0-9]+' 2>/dev/null )
%define kversion_required_postfix %( echo "%{kversion}" 2>/dev/null | grep -Eo '\\.[^0-9-]+[^.]*(\\.[^.-]+)*$' 2>/dev/null )
%define kversion_required_distro %( echo "%{kversion_required_postfix}" 2>/dev/null | sed -re 's/^(.+)\\.[^.]+$/\\1/' 2>/dev/null )
%define kversion_required %{?kversion_required_base}%{?kversion_required_distro}
%{expand:%( kmodtool_temp="$( mktemp "%{_builddir}/kmodtool-%{kmod_name}-el7.sh.XXXXXX" )" >/dev/null 2>&1 ; %{__cp} "%{SOURCE10}" "${kmodtool_temp}" >/dev/null 2>&1 ; sed -i -re  's/(^Requires:[[:blank:]]+kernel[[:blank:]]+(>=|==)[[:blank:]]+).*$/\1%{kversion_required}/' "${kmodtool_temp}" >/dev/null 2>&1 ; sh "${kmodtool_temp}" rpmtemplate "%{kmod_name}" "%{kversion}" "" ; %{__rm} -f "${kmodtool_temp}" >/dev/null 2>&1 )}
%else
%{expand:%( sh "%{SOURCE10}" rpmtemplate "%{kmod_name}" "%{kversion}" "" )}
%endif

# Disable the building of the debug package(s).
#%define debug_package %{nil}

%description
This package provides the %{kmod_name} kernel module(s).
It is built to depend upon the specific ABI provided by a range of releases
of the same variant of the Linux kernel and not on any one specific build.

%prep
%setup -q -n %{kmod_name}-%{version}
%{__rm} -f Makefile*
%{__cp} -a %{SOURCE20} Makefile
echo "override %{kmod_name} * weak-updates/%{kmod_name}" > kmod-%{kmod_name}.conf
%patch0 -p0
%patch1 -p0

%build
KSRC=%{_usrsrc}/kernels/%{kversion}
%{__make} -C "${KSRC}" %{?_smp_mflags} modules M=$PWD

%install
%{__install} -d %{buildroot}/lib/modules/%{kversion}/extra/%{kmod_name}/
%{__install} %{kmod_name}.ko %{buildroot}/lib/modules/%{kversion}/extra/%{kmod_name}/
%{__install} -d %{buildroot}%{_sysconfdir}/depmod.d/
%{__install} kmod-%{kmod_name}.conf %{buildroot}%{_sysconfdir}/depmod.d/
%{__install} -d %{buildroot}%{_defaultdocdir}/kmod-%{kmod_name}-%{version}/
%{__install} %{SOURCE5} %{buildroot}%{_defaultdocdir}/kmod-%{kmod_name}-%{version}/
%{__install} -d %{buildroot}%{_sysconfdir}/udev/rules.d/
#%{__install} 50-usb-realtek-net.rules %{buildroot}%{_sysconfdir}/udev/rules.d/
%{__install} ReadMe.txt %{buildroot}%{_defaultdocdir}/kmod-%{kmod_name}-%{version}/

# strip the modules(s)
find "%{buildroot}" -type f -name '*.ko' -exec %{__strip} --strip-debug '{}' \;


# Sign the modules(s)
%if %{?_with_modsign:1}%{!?_with_modsign:0}
# If the module signing keys are not defined, define them here.
%{!?privkey: %define privkey %{_sysconfdir}/pki/SECURE-BOOT-KEY.priv}
%{!?pubkey: %define pubkey %{_sysconfdir}/pki/SECURE-BOOT-KEY.der}
for module in $(find %{buildroot} -type f -name \*.ko);
do %{__perl} /usr/src/kernels/%{kversion}/scripts/sign-file \
sha256 %{privkey} %{pubkey} $module;
done
%endif

%clean
%{__rm} -rf %{buildroot}

%changelog
* Mon Oct 05 2015 Philip J Perry <phil@elrepo.org> - 7.0-1
- Initial el7 build of the kmod package.
- Backported from kernel-3.10.90