# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           cmake 1.1
PortGroup           compiler_blacklist_versions 1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.1

github.setup        rspamd rspamd 3.7.5
revision            0
categories          mail
license             BSD
maintainers         nomaintainer
license             Apache-2
homepage            https://rspamd.com
github.tarball_from archive

description         Rapid spam filtering system.
long_description    Rspamd is an advanced spam filtering system and \
                    email processing framework that allows evaluation \
                    of messages by a number of rules including regular \
                    expressions, statistical analysis and custom \
                    services such as URL black lists. Each message is \
                    analysed by Rspamd and given a verdict that might \
                    be used by MTA for further processing (e.g. to \
                    reject a message, or add a special header \
                    indicating spam) along with other information, \
                    such as possible DKIM signature or modifications \
                    suggested for a message. Rspamd can act as a \
                    Milter allowing direct interaction with popular \
                    MTA systems, such as Postfix or Sendmail. Rspamd \
                    is designed to process hundreds of messages per \
                    second simultaneously, and provides a number of \
                    useful features including a comprehensive Lua API \
                    that allows access to messages processing in \
                    various aspects as well as asynchronous network \
                    API to access external resources, such as DNS, \
                    HTTP or even generic TCP/UDP services.

checksums           rmd160  3ad5711028976602d1a2e5337b00806170f32596 \
                    sha256  3fba8b0086fe68ac16146cfc25cfcff352bff52fa5cef7d277007ed008170330 \
                    size    5903689

# legacysupport needed for open_memstream and fmemopen, on 10.12 and earlier
legacysupport.newest_darwin_requires_legacy 16
legacysupport.use_mp_libcxx                 yes

depends_build-append \
                    port:pkgconfig

depends_lib-append  port:fann \
                    port:gd2 \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:gmime \
                    port:hyperscan \
                    path:lib/pkgconfig/icu-uc.pc:icu \
                    port:libevent \
                    port:libmagic \
                    port:libsodium \
                    port:libstemmer \
                    port:libunwind \
                    port:lua \
                    path:lib/libluajit-5.1.2.dylib:luajit \
                    path:lib/libopenblas.dylib:OpenBLAS \
                    path:lib/libssl.dylib:openssl \
                    port:pcre2 \
                    port:perl5 \
                    port:ragel \
                    port:sqlite3 \
                    port:xxhashlib \
                    port:zlib \
                    port:zstd

depends_run-append  port:hiredis \
                    port:redis

# Match requirements of dependency hyperscan.
supported_archs     x86_64

compiler.cxx_standard 2020
# MacPorts base 2.8.1 doesn't yet know about C++20.
compiler.blacklist  {clang < 1103}

# Previous issue note; see https://github.com/rspamd/rspamd/issues/2884
# For issues with -DENABLE_PCRE2=ON, enable pcre jit debugging

set rspamd_user     _rspamd
set rspamd_group    ${rspamd_user}
add_users           ${rspamd_user} \
                    group=${rspamd_group} \
                    realname=Rspamd

libpath             ${prefix}/var/lib/${name}

# -DCMAKE_OSX_ARCHITECTURES="x86_64" causes the "Symbol not found: _blake2b_blocks_x86" error
# See https://github.com/rspamd/rspamd/issues/2883
cmake.set_osx_architectures no

# Remove after this issue addressed:
# https://github.com/rspamd/rspamd/issues/4752
## diff -NaurdwB ./rspamd-orig ./rspamd-new | sed -E -e 's/\.\/.+-(orig|new)/\./' | sed -E -e 's|/opt/local|@@PREFIX@@|g' > patch-BackwardConfig_cmake.diff
patchfiles-append   patch-BackwardConfig_cmake.diff


# Reference: https://github.com/rspamd/rspamd/blob/master/CMakeLists.txt
# Also see https://github.com/rspamd/rspamd/issues/2884
configure.args-append \
                    -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
                    -DCMAKE_INSTALL_PREFIX=${prefix} \
                    -DCONFDIR=${prefix}/etc/${name} \
                    -DDBDIR=${prefix}/var/lib/${name} \
                    -DENABLE_FANN=ON \
                    -DENABLE_GD=ON \
                    -DENABLE_HYPERSCAN=ON \
                    -DENABLE_LIBUNWIND=ON \
                    -DENABLE_LUAJIT=ON \
                    -DENABLE_PCRE2=ON \
                    -DENABLE_SNOWBALL=ON \
                    -DENABLE_TORCH=ON \
                    -DINSTALL_EXAMPLES=ON \
                    -DLIBDIR=${prefix}/lib \
                    -DLOGDIR=${prefix}/var/log/${name} \
                    -DMANDIR=${prefix}/share/man \
                    -DNO_SHARED=ON \
                    -DRSPAMD_USER=${rspamd_user} \
                    -DRUNDIR=${prefix}/var/run/${name} \
                    -DSYSTEM_XXHASH=ON \
                    -DSYSTEM_ZSTD=ON

# Build with -DENABLE_FULL_DEBUG=ON
# configure.args-append \
#     -DENABLE_FULL_DEBUG=ON

test.run            yes
test.target         run-test

post-destroot {
    # create default directories
    xinstall -o ${rspamd_user} -g ${rspamd_group} -m 0755 -d \
                    ${destroot}${prefix}/var/lib/${name}
    xinstall -d     ${destroot}${prefix}/share/doc/${name}
    xinstall -o ${rspamd_user} -g ${rspamd_group} -m 0755 -d \
                    ${destroot}${prefix}/etc/${name}
    xinstall -o ${rspamd_user} -g ${rspamd_group} -m 0755 -d \
                    ${destroot}${prefix}/var/run/${name}
    xinstall -o ${rspamd_user} -g ${rspamd_group} -m 0755 -d \
                    ${destroot}${prefix}/var/log/${name}

    set docdir ${prefix}/share/doc/${name}
    xinstall -m 0755 -d ${destroot}${docdir}
    xinstall -m 0444 -W ${worksrcpath} ChangeLog LICENSE.md README.md \
                    ${destroot}${docdir}

    # install the man pages
    xinstall -m 0644 -W ${worksrcpath}/doc rspamadm.1 rspamc.1 \
                    ${destroot}${prefix}/share/man/man1
    xinstall -m 0644 -W ${worksrcpath}/doc rspamd.8 \
                    ${destroot}${prefix}/share/man/man8
}

startupitem.create  yes
startupitem.executable \
                    ${prefix}/bin/rspamd \
                    -u ${rspamd_user} \
                    -g ${rspamd_group} \
                    -f \
                    -c ${prefix}/etc/rspamd/rspamd.conf

destroot.keepdirs   ${destroot}${prefix}/etc/${name} \
                    ${destroot}${prefix}/var/run/${name} \
                    ${destroot}${prefix}/var/lib/${name} \
                    ${destroot}${prefix}/var/log/${name} \
                    ${destroot}${prefix}/share/doc/${name}
