%bcond_without luajit
%bcond_without fasttext

%if 0%{?rhel} < 8
%global dts devtoolset-11
%else
%global dts gcc-toolset-13
%endif

%global _vpath_builddir %{_target_platform}
%if 0%{?rhel} == 7
%global _cmake %cmake3 -B %{_vpath_builddir} -S .
%global _cmake_cmd %__cmake3
%global _cmake_build %__cmake3 --build "%{_vpath_builddir}" %{?_smp_mflags} --verbose
%global _cmake_install DESTDIR="%{buildroot}" %__cmake3  --install %{_vpath_builddir}
%else
%global _cmake %cmake
%global _cmake_cmd %__cmake
%global _cmake_build %cmake_build
%global _cmake_install %cmake_install
%undefine __cmake_in_source_build
%endif


Name:             rspamd
Conflicts:        rspamd-asan

Provides:         rspamd
Version:          3.8.4
Release:          1%{?dist}
Summary:          Rapid spam filtering system
Group:            System Environment/Daemons
License:          Apache-2.0
URL:              https://rspamd.com
Source0:          https://github.com/rspamd/rspamd/archive/%{version}/rspamd-%{version}.tar.gz
Source1:          %{name}.logrotate
Source2:          80-rspamd.preset
Source3:          %{name}.init
%if %{with luajit}
Source4:          LuaJIT-2.1.ROLLING.tar.gz
%endif
# https://github.com/rspamd/fastText.git
Source5:          fasttext.tar.gz

%if 0%{?rhel} == 6
# revert usage of g_abort() back to abort()
Patch0:           rspamd-get-rid-of-g_abort.patch
# backport from glibc-headers-2.28
Patch1:           rspamd-define-static_assert.patch
%endif


%if 0%{?rhel} == 7
BuildRequires:    cmake3 >= 3.12
%else
BuildRequires:    cmake >= 3.12
%endif
BuildRequires:    make

%if 0%{?rhel} >= 7
BuildRequires:    %{dts}-gcc %{dts}-gcc-c++ %{dts}-annobin-plugin-gcc %{dts}-runtime
%else
BuildRequires:    gcc-c++ >= 11.0
%endif
BuildRequires:    libunwind-devel
BuildRequires:    binutils-devel
BuildRequires:    file-devel
BuildRequires:    lapack-devel
BuildRequires:    pkgconfig(icu-i18n)
BuildRequires:    pkgconfig(libsodium)


%ifarch x86_64
BuildRequires:    hyperscan-devel
BuildRequires:    jemalloc-devel
%endif
%if %{without luajit}
BuildRequires:    lua-devel
%endif

%if 0%{?rhel} == 6
BuildRequires:    rx-glib2-devel
%else
BuildRequires:    glib2-devel
%endif

BuildRequires:    openblas-devel
BuildRequires:    pkgconfig(openssl) >= 3.0
BuildRequires:    pcre2-devel
BuildRequires:    ragel
BuildRequires:    sqlite-devel
Requires(pre):    shadow-utils
%if 0%{?rhel} >= 7
BuildRequires:    systemd
Requires(post):   systemd
Requires(preun):  systemd
Requires(postun): systemd
%else
Requires:         rx-glib2
Requires(post):   chkconfig
Requires(preun):  chkconfig, initscripts
Requires(postun): initscripts
%endif

%description
Rspamd is a rapid, modular and lightweight spam filter. It is designed to work
with big amount of mail and can be easily extended with own filters written in
lua.

%prep
%autosetup -p1 -n %{name}-%{version}

%if %{with luajit}
pushd %{_builddir}
  tar -xf %{SOURCE4}
popd
%endif

%if %{with fasttext}
pushd %{_builddir}
  tar -xf %{SOURCE5}
popd
%endif


%build
%if 0%{?rhel} >= 7
source /opt/rh/%{dts}/enable
%endif

%if %{with luajit}
pushd %{_builddir}/LuaJIT-2.1.ROLLING
  make clean
  make %{?_smp_mflags} CC="gcc -fPIC" PREFIX=%{_builddir}/luajit-build
  make install PREFIX=%{_builddir}/luajit-build
popd
rm -f %{_builddir}/luajit-build/lib/*.so || true
%endif

%if %{with fasttext}
mkdir -p  %{_builddir}/fasttext/%{_vpath_builddir}
pushd %{_builddir}/fasttext/%{_vpath_builddir}
%if 0%{?rhel} > 7
sed 's/-pthread/-fPIC -pthread/' -i ../CMakeLists.txt
%endif

%_cmake_cmd \
    -G "Unix Makefiles" \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DCMAKE_INSTALL_PREFIX=%{_builddir}/fasttext-bin \
    ..

%make_build
make install PREFIX=%{_builddir}/fasttext-bin

popd
%endif

%if 0%{?rhel} == 6
export LDFLAGS="$LDFLAGS -L/opt/rx/lib -Wl,-rpath=/opt/rx/lib"
%endif
%if %{with fasttext}
export LDFLAGS="$LDFLAGS -Wl,-rpath=%{_libdir}/rspamd/fasttext"
%endif

%_cmake \
        -G "Unix Makefiles" \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DENABLE_LTO=ON \
        -DCMAKE_INSTALL_PREFIX=%{_prefix} \
        -DCONFDIR=%{_sysconfdir}/rspamd \
        -DMANDIR=%{_mandir} \
        -DDBDIR=%{_localstatedir}/lib/rspamd \
        -DRUNDIR=%{_localstatedir}/run/rspamd \
        -DLOGDIR=%{_localstatedir}/log/rspamd \
        -DEXAMPLESDIR=%{_datadir}/examples/rspamd \
        -DSHAREDIR=%{_datadir}/rspamd \
        -DLIBDIR=%{_libdir}/rspamd/ \
        -DINCLUDEDIR=%{_includedir} \
        -DRSPAMD_GROUP=_rspamd \
        -DRSPAMD_USER=_rspamd \
        -DENABLE_PCRE2=ON \
%if 0%{?rhel} < 7
        -DWANT_SYSTEMD_UNITS=OFF \
        -DDISABLE_PTHREAD_MUTEX=1 \
%else
        -DWANT_SYSTEMD_UNITS=ON \
        -DSYSTEMDDIR=%{_unitdir} \
%endif
        -DNO_SHARED=ON \
        -DDEBIAN_BUILD=1 \
        -DENABLE_LIBUNWIND=ON \
        -DENABLE_LIBCXX=AUTO \
%ifarch x86_64 aarch64
        -DENABLE_HYPERSCAN=ON \
%endif
%ifarch x86_64
        -DENABLE_JEMALLOC=ON \
%endif
%if %{with luajit}
        -DENABLE_LUAJIT=ON \
        -DLUA_ROOT=%{_builddir}/luajit-build \
%else
        -DENABLE_LUAJIT=OFF \
%endif
%if %{with fasttext}
        -DENABLE_FASTTEXT=ON \
        -DFASTTEXT_ROOT_DIR=%{_builddir}/fasttext-bin \
%endif
        -DENABLE_BLAS=ON

%_cmake_build

%install
%_cmake_install

%if %{with fasttext}
mkdir -p %{buildroot}%{_libdir}/rspamd/fasttext
cp %{_builddir}/fasttext-bin/lib/libfasttext.so.0 %{buildroot}%{_libdir}/rspamd/fasttext/
%endif

%{__install} -p -D -m 0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/rspamd
%if 0%{?rhel} >= 7
%{__install} -p -D -m 0644 %{SOURCE2} %{buildroot}%{_presetdir}/80-rspamd.preset
%endif
%{__install} -d -p -m 0755 %{buildroot}%{_localstatedir}/log/rspamd
%{__install} -d -p -m 0755 %{buildroot}%{_localstatedir}/lib/rspamd
%{__install} -p -D -d -m 0755 %{buildroot}%{_sysconfdir}/rspamd/local.d/
%{__install} -p -D -d -m 0755 %{buildroot}%{_sysconfdir}/rspamd/override.d/
%if 0%{?rhel} < 7
%{__install} -p -D -m 0755 %{SOURCE3} %{buildroot}%{_initrddir}/%{name}
%{__install} -d -p -m 0755 %{buildroot}%{_localstatedir}/run/rspamd
%endif

%clean
rm -rf %{buildroot}

%pre
%{_sbindir}/groupadd -r _rspamd 2>/dev/null || :
%{_sbindir}/useradd -g _rspamd -c "Rspamd user" -s /bin/false -r -d %{_localstatedir}/lib/rspamd _rspamd 2>/dev/null || :

%post
%{__chown} -R _rspamd:_rspamd %{_localstatedir}/lib/rspamd
%{__chown} _rspamd:_rspamd %{_localstatedir}/log/rspamd
%if 0%{?rhel} < 7
/sbin/chkconfig --add %{name}
%else
systemctl --no-reload preset rspamd.service >/dev/null 2>&1 || :
%endif

%preun
%if 0%{?rhel} < 7
if [ $1 = 0 ]; then
    /sbin/service %{name} stop >/dev/null 2>&1
    /sbin/chkconfig --del %{name}
fi
%else
%systemd_preun rspamd.service
%endif

%postun
%if 0%{?rhel} < 7
if [ $1 -ge 1 ]; then
    /sbin/service %{name} condrestart > /dev/null 2>&1 || :
fi

%else
%systemd_postun_with_restart rspamd.service
%endif

%files
%defattr(-,root,root,-)
%{!?_licensedir:%global license %%doc}
%license LICENSE.md
%doc AUTHORS.md README.md CONTRIBUTING.md 

%dir %{_sysconfdir}/rspamd
%config(noreplace) %{_sysconfdir}/rspamd/*

%{_bindir}/rspamd
%{_bindir}/rspamd_stats
%{_bindir}/rspamc
%{_bindir}/rspamadm

%if 0%{?rhel} < 7
%{_initrddir}/%{name}
%{_localstatedir}/run/%{name}
%else
%{_unitdir}/rspamd.service
%{_presetdir}/80-rspamd.preset
%endif

%dir %{_libdir}/rspamd
%{_libdir}/rspamd/*

%{_mandir}/man8/rspamd.*
%{_mandir}/man1/rspamc.*
%{_mandir}/man1/rspamadm.*

%dir %{_datadir}/rspamd
%{_datadir}/rspamd/*

%config(noreplace) %{_sysconfdir}/logrotate.d/rspamd

%attr(-, _rspamd, _rspamd) %dir %{_localstatedir}/lib/rspamd
%dir %{_localstatedir}/log/rspamd

%changelog
* Tue Feb 27 2024 Raven <raven@sysadmins.ws> - 3.8.4-1
- update to 3.8.4

* Tue Feb 20 2024 Raven <raven@sysadmins.ws> - 3.8.1-1
- update to 3.8.1

* Thu Dec 21 2023 Raven <raven@sysadmins.ws> - 3.7.5-1
- update to 3.7.5

* Mon Nov 20 2023 Raven <raven@sysadmins.ws> - 3.7.4-1
- update to 3.7.4

* Tue Oct  3 2023 Raven <raven@sysadmins.ws> - 3.6-1
- initial build