# -*- 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               github          1.0
PortGroup               legacysupport   1.1

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

distname                ${name}-v${version}

maintainers             {@gpanders gpanders.com:greg} \
                        {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer
categories              sysutils
license                 GPL-3

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:autoconf \
                        port:automake \
                        port:pkgconfig

depends_lib-append      lib:libuuid:libuuid \
                        path:lib/pkgconfig/libuv.pc:libuv \
                        port:json-c \
                        port:judy \
                        port:libyaml \
                        port:lz4 \
                        port:openssl

checksums               rmd160  f00653c4366164522bacca5efba6a2fe646e8cbe \
                        sha256  77e94acf7085c23ab20b2d0d2d4d2bc5a289f121fc7aac5d0daffb960041fd95 \
                        size    47466484

configure.args          --disable-dependency-tracking \
                        --disable-silent-rules \
                        --prefix=${prefix} \
                        --sysconfdir=${prefix}/etc \
                        --localstatedir=${prefix}/var \
                        --libexecdir=${prefix}/libexec \
                        --libdir=${prefix}/lib \
                        --with-math \
                        --with-zlib \
                        --enable-dbengine \
                        --with-user=netdata

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

add_users ${netdata_user} group=${netdata_group}

if { ${name} eq ${subport} } {
    use_autoreconf yes
    depends_run-append      port:${name}-dashboard

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

        # Delete any files provided by netdata-dashboard to avoid conflicts. We
        # can't just delete the whole directory because this directory contains
        # some files that are not present in netdata/dashboard. This will
        # hopefully change someday, see
        # https://github.com/netdata/dashboard/issues/83
        foreach f [glob -dir ${netdata_web_dir} * **/*] {
            delete ${destroot}${f}
        }

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

}

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

    github.setup        netdata dashboard 3.0.4 v
    revision            0

    platforms           any
    supported_archs     noarch

    github.tarball_from releases
    distname            dashboard

    checksums           rmd160  1c0c4b221bb64a651f5f3a2bec7f80ab1535959d \
                        sha256  5b791a3a5a5beee411877e687b0f1a852b6ed87178952402714d44c4659abdae \
                        size    6315714

    extract.only

    use_configure       no

    build {}

    destroot {
        xinstall -d -o ${netdata_user} -g ${netdata_group} ${destroot}${netdata_web_dir}
        system "tar -C ${destroot}${netdata_web_dir} -xf ${distpath}/${distname}.tar.gz --strip-components 1"
        system "chown -R ${netdata_user}:${netdata_group} ${destroot}${netdata_web_dir}"
    }
}
