%bcond_with systemd
%bcond_with jemalloc
%bcond_without hyperscan

%define rspamd_user      _rspamd
%define rspamd_group     %{rspamd_user}
%define rspamd_home      %{_localstatedir}/lib/rspamd
%define rspamd_logdir    %{_localstatedir}/log/rspamd
%define rspamd_confdir   %{_sysconfdir}/rspamd
%define rspamd_pluginsdir   %{_datadir}/rspamd/plugins
%define rspamd_rulesdir   %{_datadir}/rspamd/rules
%define rspamd_wwwdir   %{_datadir}/rspamd/www

Summary:        Rapid spam filtering system
Name:           rspamd
Version:        3.9.1
Release:        1%{?_dist_release}%{?with_systemd:.systemd}
Group:          servers
Vendor:         Project Vine
Distribution:   Vine Linux

License:        ASL 2.0
URL:            https://rspamd.com
Source0:        https://github.com/rspamd/rspamd/archive/%{version}.tar.gz#/%{name}-%{version}.tar.xz
Source1000:     rspamd.init
Source1001:     rspamd.logrotate
Patch1:         rspamd-3.1-32bit-noasm.patch

BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}

BuildRequires:  cmake
BuildRequires:  file-devel
BuildRequires:  fmt-devel
BuildRequires:  glib2-devel
%ifarch x86_64
BuildRequires:  hyperscan-devel
%endif
%if %{with jemalloc}
BuildRequires:  jemalloc-devel
%endif
BuildRequires:  libarchive-devel
BuildRequires:  libicu-devel
BuildRequires:  libsodium-devel
BuildRequires:  libunwind-devel
BuildRequires:  lua-devel
BuildRequires:  openssl-devel
BuildRequires:  pcre2-devel
BuildRequires:	perl
BuildRequires:  ragel
BuildRequires:  sqlite3-devel
BuildRequires:  xxhash-devel
%if %{with systemd}
BuildRequires:  systemd
%endif
BuildRequires:  libzstd-devel

Requires:       logrotate
Requires(pre):  shadow-utils
%if %{with systemd}
Requires(pre):  systemd
Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd
%else
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.


%debug_package


%prep
%autosetup -p1


%build
%if %{with hyperscan}
%define opt_hyperscan -DENABLE_HYPERSCAN=ON
%else
%define opt_hyperscan -DENABLE_HYPERSCAN=OFF
%endif

%cmake \
        -DCMAKE_INSTALL_PREFIX=%{_prefix} \
        -DCONFDIR=%{_sysconfdir}/rspamd \
        -DINCLUDEDIR=%{_includedir} \
        -DLIBDIR=%{_libdir}/rspamd/ \
        -DMANDIR=%{_mandir} \
        -DSHAREDIR=%{_datadir}/rspamd \
        -DDBDIR=%{_localstatedir}/lib/rspamd \
        -DRUNDIR=%{_rundir}/rspamd \
        -DLOGDIR=%{_localstatedir}/log/rspamd \
        -DEXAMPLESDIR=%{_datadir}/examples/rspamd \
        -DNO_SHARED=ON \
        -DDEBIAN_BUILD=0 \
        -DRSPAMD_GROUP=%{rspamd_group} \
        -DRSPAMD_USER=%{rspamd_user} \
%if %{with systemd}
        -DWANT_SYSTEMD_UNITS=ON \
        -DSYSTEMDDIR=%{_unitdir} \
%else
        -DWANT_SYSTEMD_UNITS=OFF \
        -DDISABLE_PTHREAD_MUTEX=1 \
%endif
%ifarch x86_64
        %{opt_hyperscan} \
%endif
        -DSYSTEM_FMT=ON \
        -DSYSTEM_ZSTD=ON \
        -DSYSTEM_XXHASH=ON \
        -DENABLE_LIBUNWIND=ON \
        -DENABLE_LUAJIT=OFF \
        -DENABLE_PCRE2=ON \
%if %{with jemalloc}
        -DENABLE_JEMALLOC=ON \
%endif
        %nil

%cmake_build


%install
%cmake_install

pushd ./rpm/

%if %{with systemd}
%{__install} -p -D -m 0644 80-rspamd.preset %{buildroot}%{_presetdir}/80-rspamd.preset
%{__install} -p -D -m 0644 %{name}.logrotate %{buildroot}%{_sysconfdir}/logrotate.d/%{name}
%{__install} -d -p -m 0755 %{buildroot}%{rspamd_logdir}
%else
%{__install} -p -D -m 0755 %{SOURCE1000} %{buildroot}%{_initrddir}/%{name}
%{__install} -p -D -m 0644 %{SOURCE1001} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}
%{__install} -d -p -m 0755 %{buildroot}%{_localstatedir}/run/rspamd
%{__install} -d -p -m 0755 %{buildroot}%{rspamd_logdir}
%endif

%{__install} -d -p -m 0755 %{buildroot}%{rspamd_home}
%{__install} -p -D -d -m 0755 %{buildroot}%{_sysconfdir}/%{name}/local.d/
%{__install} -p -D -d -m 0755 %{buildroot}%{_sysconfdir}/%{name}/override.d/

popd


%clean
rm -rf %{buildroot}


%pre
%{_sbindir}/groupadd -r %{rspamd_group} 2>/dev/null || :
%{_sbindir}/useradd -g %{rspamd_group} -c "Rspamd user" -s /bin/false -r -d %{rspamd_home} %{rspamd_user} 2>/dev/null || :

%post
#to allow easy upgrade from 0.8.1
%{__chown} -R %{rspamd_user}:%{rspamd_group} %{rspamd_home}

if [ -d /var/lib/rspamd/ ]; then
	find /var/lib/rspamd/ -name "*.unser" -delete
fi

%if %{with systemd}
#Macro is not used as we want to do this on upgrade
#%systemd_post %{name}.service
systemctl --no-reload preset %{name}.service >/dev/null 2>&1 || :
%{__chown} %{rspamd_user}:%{rspamd_group} %{rspamd_logdir}
%else
/sbin/chkconfig --add %{name}
%endif

%preun
%if %{with systemd}
%systemd_preun %{name}.service
%else
if [ $1 = 0 -o -x /bin/systemctl ]; then
    /sbin/service %{name} stop >/dev/null 2>&1
    /sbin/chkconfig --del %{name}
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart %{name}.service
%else
if [ $1 -ge 1 ]; then
    /sbin/service %{name} condrestart > /dev/null 2>&1 || :
fi
%endif


%files
%defattr(-,root,root,-)
%license LICENSE.md
%doc AUTHORS.md ChangeLog README.md
%if %{with systemd}
%{_unitdir}/%{name}.service
%{_presetdir}/80-rspamd.preset
%else
%{_initrddir}/%{name}
%dir %{_localstatedir}/run/rspamd
%endif
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%dir %{rspamd_logdir}
%{_mandir}/man8/%{name}.*
%{_mandir}/man1/rspamc.*
%{_mandir}/man1/rspamadm.*
%{_bindir}/rspamd
%{_bindir}/rspamd-*
%{_bindir}/rspamd_stats
%{_bindir}/rspamc
%{_bindir}/rspamc-*
%{_bindir}/rspamadm
%{_bindir}/rspamadm-*
%attr(-, %{rspamd_user}, %{rspamd_group}) %dir %{rspamd_home}
%dir %{rspamd_confdir}
%dir %{rspamd_confdir}/modules.d
%dir %{rspamd_confdir}/local.d
%dir %{rspamd_confdir}/maps.d
%dir %{rspamd_confdir}/override.d
%dir %{rspamd_confdir}/scores.d
%dir %{rspamd_wwwdir}
%dir %{_libdir}/rspamd
%config(noreplace) %{rspamd_confdir}/*.conf
%config(noreplace) %{rspamd_confdir}/*.inc
%config(noreplace) %{rspamd_confdir}/maps.d/*
%config(noreplace) %{rspamd_confdir}/modules.d/*
%config(noreplace) %{rspamd_confdir}/scores.d/*
%dir %{rspamd_pluginsdir}
%{rspamd_pluginsdir}/*.lua
%dir %{_datadir}/rspamd/lualib
%dir %{_datadir}/rspamd/lualib/lua_content
%dir %{_datadir}/rspamd/lualib/lua_ffi
%dir %{_datadir}/rspamd/lualib/lua_magic
%dir %{_datadir}/rspamd/lualib/lua_selectors
%dir %{_datadir}/rspamd/lualib/lua_scanners
%dir %{_datadir}/rspamd/lualib/plugins
%dir %{_datadir}/rspamd/lualib/redis_scripts
%dir %{_datadir}/rspamd/lualib/rspamadm
%{_datadir}/rspamd/lualib/*.lua
%{_datadir}/rspamd/lualib/lua_content/*.lua
%{_datadir}/rspamd/lualib/lua_ffi/*.lua
%{_datadir}/rspamd/lualib/lua_magic/*.lua
%{_datadir}/rspamd/lualib/lua_selectors/*.lua
%{_datadir}/rspamd/lualib/lua_scanners/*.lua
%{_datadir}/rspamd/lualib/plugins/*.lua
%{_datadir}/rspamd/lualib/redis_scripts/*.lua
%{_datadir}/rspamd/lualib/rspamadm/*.lua
%dir %{rspamd_rulesdir}
%dir %{rspamd_rulesdir}/regexp
%{rspamd_rulesdir}/regexp/*.lua
%dir %{rspamd_rulesdir}/controller
%{rspamd_rulesdir}/controller/*.lua
%{rspamd_rulesdir}/*.lua
%{rspamd_wwwdir}/*
%{_libdir}/rspamd/*
%{_datadir}/rspamd/effective_tld_names.dat
%dir %{_datadir}/rspamd/languages
%{_datadir}/rspamd/languages/*
%dir %{_datadir}/rspamd/elastic
%{_datadir}/rspamd/elastic/*


%changelog
* Tue Jul 23 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.9.1-1
- new upstream release.

* Sat Jul 13 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.9.0-1
- new upstream release.

* Mon Feb 26 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.8.4-1
- new upstream release.

* Sun Feb 25 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.8.3-1
- new upstream release.

* Wed Feb 21 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.8.2-1
- new upstream release.

* Sat Jan 27 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.8.1-1
- new upstream release.

* Sat Dec 16 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.7.5-1
- new upstream release.

* Wed Nov 15 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.7.4-1
- new upstream release.

* Wed Nov 01 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.7.3-2
- rebuilt with icu-74.

* Fri Oct 27 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.7.3-1
- new upstream release.

* Fri Oct 27 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.7.2-1
- new upstream release.

* Sun Oct 08 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.7.1-1
- new upstream release.

* Fri Aug 04 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.6-1
- new upstream release.

* Sat Mar 18 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.5-1
- new upstream release.
- dropped Patch2-4: fixed in upstream.

* Sun Nov 20 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.4-3
- imported Patch2-4 from upstream.
- enabled hyperscan.

* Wed Nov 02 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.4-2
- disabled hyperscan.

* Wed Nov 02 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.4-1
- new upstream release.

* Wed Oct 19 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.3-2
- rebuilt with libicu72.

* Sun Oct 02 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.3-1
- new upstream release.

* Sun Mar 27 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.2-1
- new upstream release.

* Tue Nov 02 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.1-2
- made to use system fmt and zstd.

* Tue Nov 02 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.1-1
- new upstream release.
- dropped Patch0 and 2: merged into upstream.
- dropped Patch1: fixed in upstream.

* Fri Oct 29 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.0-4
- rebuilt with icu-70.1.

* Sat Oct 16 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.0-3
- disabled jemalloc as default: SIGSEGV was occured.

* Sat Oct 16 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.0-2
- added Patch2 to build with openssl-3.0.0.
- built with jemalloc.

* Thu Aug 19 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.0-1
- new upstream release.
- updated Patch0.
- imported Patch1 from upstream of doctest to build with glibc-2.34+.

* Thu Apr 08 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.7-2
- rebuilt with icu69.

* Sat Jan 09 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.7-1
- new upstream release.

* Tue Jan 05 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.6-3
- updated Patch0.

* Thu Oct 08 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.6-2
- updated Patch0.

* Fri Oct 02 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.6-1
- new upstream release.
- added Patch0 to build with lua-5.4.

* Tue Aug 18 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.5-3
- rebuilt with current envirionment.

* Fri Apr 24 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.5-2
- rebuilt with icu-67.

* Thu Apr 02 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.5-1
- new upstream release.

* Thu Mar 19 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.4-2
- rebuilt with libicu66.

* Sat Mar 07 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.4-1
- initial build for Vine Linux.
