# -*- 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

# clock_gettime, utimensat
legacysupport.newest_darwin_requires_legacy 16

github.setup            netdata netdata 2.8.1 v
github.tarball_from     releases
revision                0

categories              sysutils
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.

# defined in CMakeLists
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 \
                        port:pkgconfig

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

distname                ${name}-v${version}

checksums               rmd160  109a6322a541c629ad822eb44f05d40b6f88a3bd \
                        sha256  373b61241d5cc80addeb0cc6c9f434abf363333081056a7d7ba6ea1f46ba6f06 \
                        size    16478236

set netdata_user        netdata
set netdata_group       netdata

set netdata_web_subpath share/${name}/web
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}

configure.args-append   -DENABLE_PLUGIN_OTEL=NO \
                        -DENABLE_PLUGIN_GO=NO \
                        -DWEB_DIR=${netdata_web_subpath}

add_users ${netdata_user} group=${netdata_group}

variant otel_plugin description {Enable OpenTelemetry plugin} {
    configure.args-append   -DENABLE_PLUGIN_OTEL=YES
    depends_build-append    port:rust \
                            port:cargo
}

variant go_plugin description {Enable Go plugin} {
    configure.args-append   -DENABLE_PLUGIN_GO=YES
    depends_build-append    port:go
}

notes "
    Go plugin is now a variant. To install, use \"+go\":

    Ie.,

    $ sudo port install netdata +go
"

if { ${name} eq ${subport} } {
    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/sbin|sbin|g}        ${worksrcpath}/CMakeLists.txt
        reinplace -E {s|usr/share|share|g}      ${worksrcpath}/CMakeLists.txt
    }

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

        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
}
