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

name                munin
version             1.4.7
revision            7
license             GPL-2
categories          net
maintainers         nomaintainer
platforms           darwin
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/munin%20stable/${version}

checksums           rmd160  b25c0a4d7d7613592757132290b7158228ea2a67 \
                    sha256  9a87356b1f8662f444a7a2a86ff36809124ffe787c442de0ea35850194d602af

perl5.branches      5.28

depends_lib         port:perl${perl5.major} \
                    port:p${perl5.major}-module-build \
                    port:p${perl5.major}-net-server \
                    port:p${perl5.major}-net-snmp \
                    port:p${perl5.major}-net-ssleay

# 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-Makefile-javaplugin \
                    patch-Makefile.config \
                    patch-munin-node.conf.in \
                    patch-encodings.diff \
                    patch-perl5.26.diff

post-patch {
    reinplace "s|/usr/bin/perl|${perl5.bin}|" \
        ${worksrcpath}/master/_bin/munin-update.in \
        ${worksrcpath}/node/extras/munin-node-sched \
        ${worksrcpath}/node/extras/munin-node-simple.in \
        ${worksrcpath}/node/bin/munindoc \
        ${worksrcpath}/contrib/munin-every-services-all-boxes-build-html.in \
        ${worksrcpath}/node/sbin/munin-node \
        ${worksrcpath}/node/sbin/munin-node-configure \
        ${worksrcpath}/node/sbin/munin-run
}

# 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}
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}
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}-html-template \
                            port:p${perl5.major}-time-hires \
                            port:p${perl5.major}-digest-md5 \
                            port:p${perl5.major}-text-balanced \
                            port:p${perl5.major}-log-log4perl
    destroot.target-append  install-master-prime
}

pre-destroot {
    # Create munin user and group
    addgroup munin
    adduser munin gid=[existsgroup 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}