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

github.setup            netdata netdata 2.2.6 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.

compiler.thread_local_storage \
                        yes

depends_build-append    port:pkgconfig

depends_lib-append      bin:curl:curl \
                        bin:go:go \
                        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  7e7d6ce35a93901114cd73a8f1f77c03d0302eba \
                        sha256  bd98c146aa6d0c25f80cb50b1447b8aca8a17f0995b28a11a23e843b8f210f42 \
                        size    50395963

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   -DWEB_DIR=${netdata_web_subpath}

add_users ${netdata_user} group=${netdata_group}

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}

    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
    }

    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
}