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

PortSystem          1.0
PortGroup           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                syslog-ng-devel
github.setup        syslog-ng syslog-ng 9ca90a20318885017ff5761a31d2b0afd5dc33eb
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
version             2024.10.22
revision            1
set real_name       syslog-ng
set real_version    4.8.1
conflicts           ${real_name}

categories          sysutils
license             {GPL-2+ OpenSSLException}
maintainers         nomaintainer
description         Highly configurable syslog replacement
long_description    syslog-ng is the world's most flexible and scalable audit \
                    trail processing tool for organizations of any size. It \
                    provides a centralised, securely stored log of all devices \
                    on your network, whatever platform they run on. And \
                    syslog-ng also incorporates a host of powerful features, \
                    including filtering based on message content, as well as \
                    customisable data mining and analysis capabilities.

homepage            https://www.syslog-ng.com

fetch.type          git

post-fetch {
    system -W ${worksrcpath} "git submodule update --init"
}

set py_ver          3.12
set py_ver_nodot    [string map {. {}} ${py_ver}]

depends_build-append \
                    port:bison \
                    port:flex \
                    port:gettext \
                    port:gperf \
                    path:bin/pkg-config:pkgconfig \
                    port:python${py_ver_nodot}

depends_lib-append  port:curl \
                    port:gettext-runtime \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:hiredis \
                    port:ivykis \
                    port:json-c \
                    port:libdbi \
                    port:libesmtp \
                    port:libmaxminddb \
                    port:libnet \
                    port:librdkafka \
                    port:mongo-c-driver \
                    port:paho.mqtt.c \
                    port:pcre2 \
                    port:rabbitmq-c \
                    port:tcp_wrappers \
                    port:zlib

startupitem.create      yes
startupitem.executable  ${prefix}/sbin/syslog-ng
startupitem.pidfile clean ${prefix}/var/syslog-ng.pid

compiler.c_standard     1999
compiler.cxx_standard   2017

# Do not enable testing by default; at the moment it uses
# criterion framework, which is broken on some platforms.
configure.args-append \
                    -DBUILD_TESTING=OFF \
                    -DENABLE_AFAMQP=ON \
                    -DENABLE_AFSMTP=ON \
                    -DENABLE_AFSNMP=ON \
                    -DENABLE_AFUSER=ON \
                    -DENABLE_APPMODEL=ON \
                    -DENABLE_AZURE_AUTH_HEADER=ON \
                    -DENABLE_CLOUD_AUTH=ON \
                    -DENABLE_CPP=ON \
                    -DENABLE_CURL=ON \
                    -DENABLE_DARWIN_OSL=OFF \
                    -DENABLE_EBPF=OFF \
                    -DENABLE_EXAMPLE_MODULES=OFF \
                    -DENABLE_GEOIP2=ON \
                    -DENABLE_GETENT=ON \
                    -DENABLE_GRPC=OFF \
                    -DENABLE_HOOK_COMMANDS=ON \
                    -DENABLE_IPV6=ON \
                    -DENABLE_JAVA=OFF \
                    -DENABLE_JAVA_MODULES=OFF \
                    -DENABLE_JOURNALD=OFF \
                    -DENABLE_JSON=ON \
                    -DENABLE_KAFKA=ON \
                    -DENABLE_MAP_VALUE_PAIRS=ON \
                    -DENABLE_MONGODB=ON \
                    -DENABLE_MQTT=ON \
                    -DENABLE_NATIVE=ON \
                    -DENABLE_OBJC=OFF \
                    -DENABLE_OPENBSD_SYS_DRIVER=OFF \
                    -DENABLE_PACCT=OFF \
                    -DENABLE_PYTHON=ON \
                    -DENABLE_PYTHON_MODULES=OFF \
                    -DENABLE_REDIS=ON \
                    -DENABLE_RIEMANN=OFF \
                    -DENABLE_SNMP=ON \
                    -DENABLE_SQL=ON \
                    -DENABLE_STARDATE=ON \
                    -DENABLE_STOMP=ON \
                    -DENABLE_XML=ON \
                    -DIVYKIS_SOURCE=system \
                    -DJSONC_SOURCE=system \
                    -DPYTHON_EXECUTABLE=${prefix}/bin/python${py_ver} \
                    -DWITH_COMPILE_DATE=OFF \
                    -DWITH_GETTEXT=ON

variant grpc description "Enable GRPC modules" {
    depends_lib-append \
                    port:abseil \
                    port:c-ares \
                    port:grpc \
                    port:protobuf3-cpp \
                    port:re2

    configure.args-replace \
                    -DENABLE_GRPC=OFF -DENABLE_GRPC=ON
}

variant osl description "Enable support for OSLog" {
    configure.args-replace \
                    -DENABLE_DARWIN_OSL=OFF -DENABLE_DARWIN_OSL=ON \
                    -DENABLE_OBJC=OFF -DENABLE_OBJC=ON
}

# Clangs fail to build these, at least with Apple libc++.
# https://github.com/syslog-ng/syslog-ng/issues/5104
if {[string match *clang* ${configure.compiler}]} {
    configure.args-replace \
                    -DENABLE_AFSNMP=ON -DENABLE_AFSNMP=OFF
}

if {[string match *gcc* ${configure.compiler}]} {
    default_variants-append +grpc
}

# OSLog supported in 10.10+. No point in enabling this earlier.
# Also, it does not presently build with gcc.
# https://github.com/syslog-ng/syslog-ng/issues/5093
if {(${os.platform} eq "darwin" && ${os.major} > 13) \
    && ![string match *gcc* ${configure.compiler}]} {
    default_variants-append +osl
}

post-destroot {
    xinstall -m 644 ${filespath}/syslog-ng.conf-dist ${destroot}${prefix}/etc/
    reinplace  "s|%VERSION%|${real_version}|g" ${destroot}${prefix}/etc/syslog-ng.conf-dist

    # https://github.com/syslog-ng/syslog-ng/issues/5100
    foreach dylib [ exec find ${destroot}${prefix}/lib/syslog-ng -name "\*.dylib" ] {
        regsub ":$" ${dylib} "" destroot_dylib_path
        regsub ${destroot} ${destroot_dylib_path} "" dylib_path
        system "install_name_tool -id ${dylib_path} ${destroot_dylib_path}"
    }

    if {[variant_isset grpc]} {
        foreach module { libbigquery.dylib libloki.dylib libotel.dylib } {
            system "install_name_tool -change ${prefix}/lib/libgrpc-protos.dylib \
                ${prefix}/lib/syslog-ng/libgrpc-protos.dylib \
                ${destroot}${prefix}/lib/syslog-ng/${module}"
        }
    }

    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 -W ${worksrcpath} README.md AUTHORS COPYING NEWS.md \
        GPL.txt LGPL.txt ${destroot}${prefix}/share/doc/${name}
}

post-activate {
    if {![file exists ${prefix}/etc/syslog-ng.conf]} {
        copy ${prefix}/etc/syslog-ng.conf-dist ${prefix}/etc/syslog-ng.conf
    }
}

notes \
"To use syslog-ng, first unload OS X's built-in syslog daemon:

    sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.syslogd.plist

Then customize ${prefix}/etc/syslog-ng.conf,
and

    sudo load syslog-ng
"
