# -*- 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           perl5 1.0
PortGroup           java 1.0

name                munin
version             2.0.61
revision            1
license             GPL-2
categories          net
maintainers         nomaintainer
description         Extensible system monitoring tool

long_description    Munin the monitoring tool surveys all your computers \
                    and remembers what it saw. It presents all the \
                    information in graphs through a web interface. Its \
                    emphasis is on plug and play capabilities. Using Munin \
                    you can easily monitor the performance of your \
                    computers, networks, SANs, applications, weather \
                    measurements and whatever comes to mind. \
                    \
                    Note: By default only Munin node is installed. To \
                    install the Munin server that gathers data from \
                    existing nodes and generates graphs please enable the \
                    server variant.

homepage            http://munin-monitoring.org/
master_sites        sourceforge:project/munin/stable/${version}

checksums           rmd160  4b5a9ede74ace4823417538c1b97c8b36745105f \
                    sha256  0118256a28579665c9770051feae8aff4ca1f27bcfc2d92d576776a2262dea2b \
                    size    2247668

# add perl variants
perl5.require_variant   yes
perl5.conflict_variants yes
perl5.branches          5.28 5.30 5.32 5.34
perl5.default_branch    5.34
perl5.create_variants   ${perl5.branches}

java.version        1.7+
java.fallback       openjdk11

depends_lib-append \
                    port:p${perl5.major}-crypt-des \
                    port:p${perl5.major}-digest-sha1 \
                    port:p${perl5.major}-digest-hmac \
                    port:p${perl5.major}-html-template \
                    port:p${perl5.major}-log-log4perl \
                    port:p${perl5.major}-module-build \
                    port:p${perl5.major}-net-server \
                    port:p${perl5.major}-net-snmp \
                    port:p${perl5.major}-net-ssleay \
                    port:p${perl5.major}-time-hires \
                    port:python310 \
                    port:ruby26

# munindoc needs at least groff version 1.20.1 for good Unicode support.

depends_run         port:groff

use_configure       no
use_parallel_build  no
patchfiles          patch-munin-node.conf.in.diff \
                    patch-fix-getversion-invocation.diff

post-patch {
    reinplace -W ${worksrcpath} "s|/usr/bin/perl|${perl5.bin}|" \
        contrib/dump_db \
        contrib/dump_storable \
        contrib/epoch2localtime \
        contrib/import_db \
        contrib/munin-every-services-all-boxes-build-html.in \
        contrib/munin-mergedb.pl \
        contrib/munin-node-debug \
        contrib/perf/db_files.pl \
        contrib/perf/rrd_perf.pl \
        contrib/perf/test_weak.pl \
        contrib/rrdcopy \
        contrib/rtbtrace.pl \
        node/bin/munindoc \
        node/extras/munin-node-simple.in \
        node/sbin/munin-node \
        node/sbin/munin-node-configure \
        node/sbin/munin-run \
        plugins/node.d.debug/sincos \
        plugins/node.d.debug/testplugin

    reinplace -W ${worksrcpath} "s|/usr/bin/env python.*|${prefix}/bin/python3.10|" \
        Makefile.config \
        doc/conf.py

    reinplace "s|root:root|root:wheel|" ${worksrcpath}/Makefile
}

# sed fails in Mountain Lion, see #36527
build.env           LANG=C LC_ALL=C

build.target        build
build.args          PREFIX=${prefix} \
                    CONFDIR=${prefix}/etc/munin \
                    DBDIR=${prefix}/var/munin \
                    LOGDIR=${prefix}/var/log/munin \
                    STATEDIR=${prefix}/var/run/munin \
                    MANDIR=${prefix}/share/man \
                    DOCDIR=${prefix}/share/doc/munin \
                    LIBDIR=${prefix}/lib/munin \
                    HTMLDIR=${prefix}/www/munin \
                    CGIDIR=${prefix}/www/cgi-bin \
                    PERL=${perl5.bin} \
                    PYTHON=${prefix}/bin/python3.10 \
                    RUBY=${prefix}/bin/ruby2.6
destroot.args       PREFIX=${destroot}${prefix} \
                    CONFDIR=${destroot}${prefix}/etc/munin \
                    DBDIR=${destroot}${prefix}/var/munin \
                    LOGDIR=${destroot}${prefix}/var/log/munin \
                    STATEDIR=${destroot}${prefix}/var/run \
                    MANDIR=${destroot}${prefix}/share/man \
                    DOCDIR=${destroot}${prefix}/share/doc/munin \
                    LIBDIR=${destroot}${prefix}/lib/munin \
                    HTMLDIR=${destroot}${prefix}/www/munin \
                    CGIDIR=${destroot}${prefix}/www/cgi-bin \
                    PERL=${perl5.bin} \
                    PYTHON=${prefix}/bin/python3.10 \
                    RUBY=${prefix}/bin/ruby2.6
destroot.keepdirs   ${destroot}${prefix}/var/log/munin \
                    ${destroot}${prefix}/var/run/munin \
                    ${destroot}${prefix}/etc/munin/plugins \
                    ${destroot}${prefix}/etc/munin/plugin-conf.d \
                    ${destroot}${prefix}/var/munin \
                    ${destroot}${prefix}/www/munin
destroot.target     install-common-prime \
                    install-node-prime \
                    install-plugins-prime \
                    install-plugins-java \
                    install-man

startupitem.create  yes
startupitem.name    munin-node
startupitem.start   "${prefix}/sbin/munin-node"
startupitem.stop    "kill `cat ${prefix}/var/run/munin/munin-node.pid`"

variant server description {Install Munin server} {
    depends_lib-append      port:rrdtool \
                            port:p${perl5.major}-cgi-fast \
                            port:p${perl5.major}-digest-md5 \
                            port:p${perl5.major}-file-copy-recursive \
                            port:p${perl5.major}-getopt-long \
                            port:p${perl5.major}-io-socket-inet6 \
                            port:p${perl5.major}-params-validate \
                            port:p${perl5.major}-text-balanced \
                            port:p${perl5.major}-timedate
    destroot.target-append  install-master-prime
}

add_users           munin group=munin

post-destroot {
    # Rename config files so port upgrades don't overwrite installed versions of these
    file rename ${destroot}${prefix}/etc/munin/munin-node.conf ${destroot}${prefix}/etc/munin/munin-node.conf.sample

    # State directory should be owned by munin
    xinstall -d -o munin ${destroot}${prefix}/var/run/munin

    if { [variant_isset server] } {
        file rename ${destroot}${prefix}/etc/munin/munin.conf ${destroot}${prefix}/etc/munin/munin.conf.sample
        # Create and symlink launchd item for munin-cron
        xinstall -d ${destroot}${prefix}/etc/LaunchDaemons/org.macports.munin-cron
        file copy ${filespath}/org.macports.munin-cron.plist.in \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.munin-cron/org.macports.munin-cron.plist
        reinplace s|@PREFIX@|${prefix}|g ${destroot}${prefix}/etc/LaunchDaemons/org.macports.munin-cron/org.macports.munin-cron.plist

        xinstall -d ${destroot}/Library/LaunchDaemons
        ln -s ${prefix}/etc/LaunchDaemons/org.macports.munin-cron/org.macports.munin-cron.plist \
        ${destroot}/Library/LaunchDaemons/org.macports.munin-cron.plist

        ui_msg "###########################################################"
        ui_msg "# A startup item has been generated that will cause the"
        ui_msg "# Munin server to fetch data from all nodes every 5 mins."
        ui_msg "# It is disabled by default, please execute the following"
        ui_msg "# command to enable:"
        ui_msg "#"
        ui_msg "# sudo launchctl load -w /Library/LaunchDaemons/org.macports.munin-cron.plist"
        ui_msg "###########################################################"
    }
}

post-activate {
    # Copy sample versions of config files into place
    if {![file exists ${prefix}/etc/munin/munin-node.conf]} {
        file copy ${prefix}/etc/munin/munin-node.conf.sample ${prefix}/etc/munin/munin-node.conf
    }
    if { [variant_isset server] } {
        if {![file exists ${prefix}/etc/munin/munin.conf]} {
            file copy ${prefix}/etc/munin/munin.conf.sample ${prefix}/etc/munin/munin.conf
        }
    }
}

notes "
To detect supported Munin plugins please use the following command:

sudo -u munin munin-node-configure --suggest --shell | sudo sh
"

livecheck.regex ${name}-(\[0-9\]+\\.\[0-9\]*\[02468\](\\.\[0-9\]+)*)${extract.suffix}
