# -*- 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, utimensat
legacysupport.newest_darwin_requires_legacy 16

github.setup            netdata netdata 2.10.4 v
revision                0
categories              sysutils net
license                 GPL-3
maintainers             {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer

description             Real-time performance monitoring done right
long_description        Netdata's distributed, real-time monitoring Agent \
                        collects thousands of metrics from systems, hardware, \
                        containers, and applications with zero configuration. \
                        It runs permanently on all your physical/virtual servers, \
                        containers, cloud deployments, and edge/IoT devices, and \
                        is perfectly safe to install on your systems mid-incident \
                        without any preparation.

distname                ${name}-v${version}

checksums               rmd160  d74ff5d2c8d8821abba48207e87045c4d5542f24 \
                        sha256  d1ae949863925d626b1d42a53000bcca2c239718e0b0ddc6a3f3f5029b21cdfe \
                        size    18158997
github.tarball_from     releases

patchfiles-append       0001-patch-stream-receiver-connection.c.patch

patchfiles-append       0002-common.h-define-__STDC_FORMAT_MACROS-prior-to-includ.patch

# Fix for a massive recent breakage after 2.10.3:
patchfiles-append       0003-unbreak-macos.plugin.patch

compiler.cxx_standard   2014
compiler.thread_local_storage \
                        yes
# fails to build on <10.12 with Xcode Clang
compiler.blacklist-append \
                        {clang < 900}

depends_build-append    path:bin/git:git \
                        path:bin/pkg-config:pkgconfig

depends_lib-append      port:brotli \
                        port:curl \
                        port:json-c \
                        port:judy \
                        port:libuuid \
                        path:lib/pkgconfig/libuv.pc:libuv \
                        port:libyaml \
                        port:lz4 \
                        port:pcre2 \
                        port:protobuf-c \
                        port:snappy \
                        port:zstd

# These are installed into ${prefix}/sbin.
# netdata(75885) malloc: *** error for object 0xa00a6754: pointer being freed was not allocated
legacysupport.redirect_sbins log2journal netdata netdatacli

set netdata_web_subpath share/${name}/web

# Disable plug-ins that need Go and Rust.
configure.args-append   -DBUILD_SHARED_LIBS=ON \
                        -DENABLE_BUNDLED_JSONC=OFF \
                        -DENABLE_BUNDLED_PROTOBUF=OFF \
                        -DENABLE_BUNDLED_YAML=OFF \
                        -DENABLE_JEMALLOC=OFF \
                        -DENABLE_LIBUNWIND=OFF \
                        -DENABLE_MIMALLOC=OFF \
                        -DENABLE_ML=ON \
                        -DENABLE_NETDATA_JOURNAL_FILE_READER=OFF \
                        -DENABLE_PLUGIN_GO=OFF \
                        -DENABLE_PLUGIN_IBM=OFF \
                        -DENABLE_PLUGIN_OTEL=OFF \
                        -DENABLE_PLUGIN_OTEL_SIGNAL_VIEWER=OFF \
                        -DENABLE_PLUGIN_SCRIPTS=OFF \
                        -DENABLE_WEBRTC=OFF \
                        -DWEB_DIR=${netdata_web_subpath}

variant jemalloc description "Enable jemalloc" {
    configure.args-replace \
                        -DENABLE_JEMALLOC=OFF -DENABLE_JEMALLOC=ON
    depends_lib-append  port:jemalloc
}

# This should be fixed, uses incorrect set-up in CMakeLists:
variant mimalloc description "Enable mimalloc" {
    configure.args-replace \
                        -DENABLE_MIMALLOC=OFF -DENABLE_MIMALLOC=ON
    depends_lib-append  port:mimalloc
}

set netdata_user        netdata
set netdata_group       netdata

set netdata_conf_dir    ${prefix}/etc/${name}
set netdata_cache_dir   ${prefix}/var/cache/${name}
set netdata_log_dir     ${prefix}/var/log/${name}
set netdata_lib_dir     ${prefix}/var/lib/${name}
set netdata_web_dir     ${prefix}/${netdata_web_subpath}

add_users ${netdata_user} group=${netdata_group}

if {${subport} eq ${name}} {
    startupitem.create      yes
    startupitem.executable  ${prefix}/sbin/netdata -D

    destroot.keepdirs-append \
                            ${destroot}${netdata_cache_dir} \
                            ${destroot}${netdata_log_dir} \
                            ${destroot}${netdata_lib_dir}

    post-patch {
        reinplace -E {s|usr/bin|bin|g}          ${worksrcpath}/CMakeLists.txt
        reinplace -E {s|usr/lib|lib|g}          ${worksrcpath}/CMakeLists.txt
        reinplace -E {s|usr/libexec|libexec|g}  ${worksrcpath}/CMakeLists.txt
        reinplace -E {s|usr/sbin|sbin|g}        ${worksrcpath}/CMakeLists.txt
        reinplace -E {s|usr/share|share|g}      ${worksrcpath}/CMakeLists.txt
    }

    if {[string match *gcc* ${configure.compiler}] && (${configure.build_arch} in [list arm i386 ppc])} {
        # matrix_la.h:225:32: warning: iteration 1 invokes undefined behavior [-Waggressive-loop-optimizations]
        configure.optflags-replace \
                            -Os -O2
        # ___atomic_compare_exchange_8 etc.
        configure.ldflags-append \
                            -latomic
    }

    post-destroot {
        xinstall -m 0644 ${worksrcpath}/system/netdata.conf ${destroot}${netdata_conf_dir}

        reinplace "s|web files owner = .*|web files owner = netdata|" ${destroot}${netdata_conf_dir}/netdata.conf
        reinplace "s|NETDATA_USER_CONFIG_DIR=\"/|NETDATA_USER_CONFIG_DIR=\"${prefix}/|" ${destroot}${netdata_conf_dir}/edit-config
        reinplace "s|NETDATA_STOCK_CONFIG_DIR=\"/|NETDATA_STOCK_CONFIG_DIR=\"${prefix}/|" ${destroot}${netdata_conf_dir}/edit-config

        xinstall -d -o ${netdata_user} -g ${netdata_group} ${destroot}${netdata_cache_dir}
        xinstall -d -o ${netdata_user} -g ${netdata_group} ${destroot}${netdata_log_dir}
        xinstall -d -o ${netdata_user} -g ${netdata_group} ${destroot}${netdata_lib_dir}

        touch ${destroot}${netdata_conf_dir}/.opt-out-from-anonymous-statistics

        system "chown -R ${netdata_user}:${netdata_group} ${destroot}${netdata_web_dir}"
    }
}

subport ${name}-dashboard {
    PortGroup           obsolete 1.0

    name                netdata-dashboard
    version             3.0.4
    revision            0

    description         Static assets for the Netdata Agent dashboard.
    long_description    {*}${description}

    replaced_by         netdata
}
