%{?!packager: %define packager Brian Behlendorf <behlendorf1@llnl.gov>}

%if ! 0%{?rhel}%{?fedora}%{?mageia}%{?suse_version}%{?openEuler}
%define not_rpm 1
%endif

# Exclude input files from mangling
%global __brp_mangle_shebangs_exclude_from ^/usr/src/.*$

%define module  @PACKAGE@
%define mkconf  scripts/dkms.mkconf

Name:           %{module}-dkms

Version:        @VERSION@
Release:        @RELEASE@%{?dist}
Summary:        Kernel module(s) (dkms)

Group:          System Environment/Kernel
License:        @ZFS_META_LICENSE@
URL:            https://github.com/openzfs/zfs
Source0:        %{module}-%{version}.tar.gz
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildArch:      noarch

Requires:       dkms >= 2.2.0.3
Requires(post): dkms >= 2.2.0.3
Requires(preun): dkms >= 2.2.0.3
Requires:       gcc, make, perl, diffutils
Requires(post): gcc, make, perl, diffutils
%if 0%{?rhel}%{?fedora}%{?mageia}%{?suse_version}%{?openEuler}
Requires:       kernel-devel >= @ZFS_META_KVER_MIN@, kernel-devel <= @ZFS_META_KVER_MAX@.999
Requires(post): kernel-devel >= @ZFS_META_KVER_MIN@, kernel-devel <= @ZFS_META_KVER_MAX@.999
Obsoletes:      spl-dkms <= %{version}
%endif
Provides:       %{module}-kmod = %{version}
AutoReqProv:    no

%if (0%{?fedora}%{?suse_version}%{?openEuler}) || (0%{?rhel} && 0%{?rhel} < 9)
# We don't directly use it, but if this isn't installed, rpmbuild as root can
# crash+corrupt rpmdb
# See issue #12071
BuildRequires:  ncompress
%endif

%description
This package contains the dkms ZFS kernel modules.

%prep
%setup -q -n %{module}-%{version}

%build
%{mkconf} -n %{module} -v %{version} -f dkms.conf

%install
if [ "$RPM_BUILD_ROOT" != "/" ]; then
    rm -rf $RPM_BUILD_ROOT
fi
mkdir -p $RPM_BUILD_ROOT/usr/src/
cp -rf ${RPM_BUILD_DIR}/%{module}-%{version} $RPM_BUILD_ROOT/usr/src/

%clean
if [ "$RPM_BUILD_ROOT" != "/" ]; then
    rm -rf $RPM_BUILD_ROOT
fi

%files
%defattr(-,root,root)
/usr/src/%{module}-%{version}

%post
for POSTINST in /usr/lib/dkms/common.postinst; do
    if [ -f $POSTINST ]; then
        $POSTINST %{module} %{version}
        exit $?
    fi
    echo "WARNING: $POSTINST does not exist."
done
echo -e "ERROR: DKMS version is too old and %{module} was not"
echo -e "built with legacy DKMS support."
echo -e "You must either rebuild %{module} with legacy postinst"
echo -e "support or upgrade DKMS to a more current version."
exit 1

%preun
# Are we doing an upgrade?
if [ "$1" = "1" -o "$1" = "upgrade" ] ; then
	# Yes we are.  Are we upgrading to a new ZFS version?
	NEWEST_VER=$(dkms status zfs | tr -d , | sort -r -V | awk '/installed/{print $2; exit}')
	if [ "$NEWEST_VER" != "%{version}" ] ; then
		# Yes, it's a new ZFS version.  We'll uninstall the old module
		# later on in this script.
		true
	else
		# No, it's probably an upgrade of the same ZFS version
		# to a new distro (zfs-dkms-0.7.12.fc28->zfs-dkms-0.7.12.fc29).
		# Don't remove our modules, since the rebuild for the new
		# distro will automatically delete the old modules.
		exit 0
	fi
fi

# If we're here then we're doing an uninstall (not upgrade).
CONFIG_H="/var/lib/dkms/%{module}/%{version}/*/*/%{module}_config.h"
SPEC_META_ALIAS="@PACKAGE@-@VERSION@-@RELEASE@"
DKMS_META_ALIAS=`cat $CONFIG_H 2>/dev/null |
    awk -F'"' '/META_ALIAS\s+"/ { print $2; exit 0 }'`
if [ "$SPEC_META_ALIAS" = "$DKMS_META_ALIAS" ]; then
    echo -e
    echo -e "Uninstall of %{module} module ($SPEC_META_ALIAS) beginning:"
    dkms remove -m %{module} -v %{version} --all %{!?not_rpm:--rpm_safe_upgrade}
fi
exit 0
