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

name                zabbix-agent
revision            0
categories          net
maintainers         {eborisch @eborisch} openmaintainer

license             AGPL-3

description         An open source application and network monitor

long_description    Zabbix is the ultimate open source availability and \
                    performance monitoring solution. Zabbix offers advanced \
                    monitoring, alerting, and visualization features today \
                    which are missing in other monitoring systems, even some \
                    of the best commercial ones.

# Point to the latest LTS version
set LTS 7

array set VERSIONS {
    ""  0
    4   4.0.99
    5   5.0.99
    7   7.0.26
    74  7.4.10
    #8   8.0.0
}

set zver            [regsub -all {[^\d]} ${subport} {}]
version             $VERSIONS(${zver})
set zbranch         [regsub {\.[\drcalphbet]+$} ${version} {}]

distname            zabbix-${version}

livecheck.url \
    https://www.zabbix.com/download_sources
homepage            http://www.zabbix.com/
master_sites \
    https://cdn.zabbix.com/zabbix/sources/stable/${zbranch}

dist_subdir         zabbix${zver}

switch $zver {
    7 {
        livecheck.regex     "zabbix-(7\\.0\\.\[0-9\]+).tar.gz"
        checksums \
            rmd160  a66b7574cd6651b4a88e75d473c66f564e9552b1 \
            sha256  3c27a97b52c75e2eccfa43dec5e3fd7d52876f5fdd94172495b025ec9f2a13a8 \
            size    46875037
    }
    74 {
        livecheck.regex     "zabbix-(7\\.4\\.\[0-9\]+).tar.gz"
        checksums \
            rmd160  56d88cfbe9f5f3ebdc2fae83691d736bca1e178f \
            sha256  8557600b93666f2f89b108e9998977740e08ec35f2a025da30b760822e966bfa \
            size    43527563
    }
    8 {
        livecheck.regex     "zabbix-(8\\.0\\.\[0-9\]+).tar.gz"
        checksums \
            rmd160  f137f75b30adc1c2ba3aa93c53896c67f8778970 \
            sha256  d4828f04526f7f8a3110c812d90574a9457ce3520e2350516c6a6b6c788be5b3 \
            size    60892700
    }
}

subport             zabbix4-agent {replaced_by zabbix${LTS}-agent}
subport             zabbix5-agent {replaced_by zabbix${LTS}-agent}
subport             zabbix7-agent {conflicts zabbix74-agent zabbix8-agent}
subport             zabbix74-agent {conflicts zabbix7-agent zabbix8-agent}
#subport             zabbix8-agent {conflicts zabbix74-agent zabbix7-agent}

# Alert any users just in case. Remove 05/2027
subport             zabbix4 {PortGroup obsolete 1.0}
subport             zabbix4-frontend {PortGroup obsolete 1.0}
subport             zabbix5 {PortGroup obsolete 1.0}
subport             zabbix5-frontend {PortGroup obsolete 1.0}

if {${name} eq ${subport}} {
    # Install the latest LTS version if requested.
    replaced_by zabbix${LTS}-agent
    distfiles
    supported_archs noarch
    # Make sure this port doesn't do anything else
    build {}
    destroot {}
} else {
    patchfiles          log_and_pid_locations.patch

    depends_lib-append  port:libiconv \
                        port:libevent
    universal_variant   no

    use_autoconf        yes

    configure.args \
        --bindir=${prefix}/bin/zabbix \
        --sbindir=${prefix}/sbin/zabbix \
        --datadir=${prefix}/share/zabbix \
        --libexecdir=${prefix}/libexec/zabbix \
        --localstatedir=${prefix}/var/zabbix \
        --sysconfdir=${prefix}/etc/zabbix${zver} \
        --enable-agent \
        --enable-ipv6 \
        --with-libevent=${prefix} \
        --without-openssl

    variant ssl description "Enable SSL" {
        depends_lib-append  port:openssl
        configure.args-replace --without-openssl --with-openssl=${prefix}
    }

    if {$zver == 7} {
        configure.args-append   --with-libpcre=${prefix}
        depends_lib-append      port:pcre
    } else {
        configure.args-append   --with-libpcre2=${prefix}
        depends_lib-append      port:pcre2
    }

    #configure.ldflags-append    -lresolv

    long_description-append "\n * ${subport} provides a local monitoring agent."
    startupitem.create  yes
    startupitem.name        zabbix${zver}-agentd
    startupitem.executable  \
        ${prefix}/sbin/zabbix/zabbix_agentd \
        -c ${prefix}/etc/zabbix${zver}/zabbix_agentd.conf
    startupitem.pidfile     auto ${prefix}/var/run/zabbix/zabbix_agentd.pid
    startupitem.logfile     ${prefix}/var/log/zabbix/zabbix_agentd.launch

    # Revisit when in better shape on OSX; currently missing many items.
    #if {$zver >= 6} {
    #    depends_build-append    port:go
    #    configure.args-append   --enable-agent2
    #    build.env-append        GOPATH=${worksrcpath}/src/zabbix
    #}

    add_users zabbix group=zabbix

    post-patch {
        reinplace "s|%%PREFIX%%|${prefix}|" \
            conf/zabbix_agentd.conf
        reinplace "s|/usr/local/etc|${prefix}/etc/zabbix${zver}|" \
            conf/zabbix_agentd.conf
    }

    destroot.keepdirs \
        ${destroot}${prefix}/etc/zabbix${zver}/zabbix_agentd.conf.d \
        ${destroot}${prefix}/var/run/zabbix \
        ${destroot}${prefix}/var/log/zabbix

    post-destroot {
        # Copy sample agent .conf files
        xinstall -m 755 -d \
            ${destroot}${prefix}/etc/zabbix${zver}/zabbix_agentd.conf.d
        xinstall -m 755  ${worksrcpath}/conf/zabbix_agentd.conf \
            ${destroot}${prefix}/etc/zabbix${zver}/zabbix_agentd.conf.sample

        # Don't overwrite user settings on each install
        delete ${destroot}${prefix}/etc/zabbix${zver}/zabbix_agentd.conf
        foreach dname {run log} {
            xinstall -d -m 755 ${destroot}${prefix}/var/${dname}/zabbix
        }
    }

    post-activate {
        foreach dname {run log} {
            system "chown -R zabbix:zabbix ${prefix}/var/${dname}/zabbix"
        }
    }

    notes "
#### ZABBIX${zver} agent installation:

1) Edit the sample .conf file ${prefix}/etc/zabbix${zver}/zabbix_agentd.conf.sample
   (rename & omit .sample)

   Set the following variable to the IP address of your ZABBIX server to\
   allow it access to the agent. (Alternatively ServerActive for active checks.)

    Server=x.x.x.x


2) Set zabbix_agentd to run at system boot

     sudo port load zabbix${zver}-agent

####
"
}
