# -*- 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                nrg
version             0.99.27
revision            8
categories          net
maintainers         nomaintainer
# The source includes the GPL-2 LICENSE file but does not specify
# whether it is GPL-2.0-only or GPL-2.0-or-later.
license             GPL-2

description         Network Resource Grapher

long_description    NRG is an RRDtool front-end tool to monitor traffic and load \
                    on network links or any other resource utilization source.  It \
                    automatically creates and maintains web pages and RRDtool databases, \
                    while providing short and long-term graph web pages just like MRTG.

# website no longer exists
homepage            https://web.archive.org/web/20090307054951/http://nrg.hep.wisc.edu/
master_sites        macports_distfiles:${name}

checksums           rmd160  1192ce02d14447abdcff994671693eeb4ca67e28 \
                    sha256  ef851705f9ebd2bf55892823f258c7cffc079ae0276df4f40860c520262971c2 \
                    size    213776

perl5.branches      5.34

depends_lib         port:perl${perl5.major} \
                    port:p${perl5.major}-time-hires \
                    port:rrdtool

patchfiles          patch-nrg-discover-tcp.in.diff \
                    CFLAGS.patch \
                    implicit-functions-args-and-return-types.patch

configure.perl      ${perl5.bin}

configure.pre_args  --prefix=${prefix}/var/${name}

configure.args      --bindir=${prefix}/bin \
                    --sysconfdir=${prefix}/etc/${name} \
                    --datadir=${prefix}/share \
                    --build=${os.arch}-unknown-${os.platform}

platform darwin {
    configure.args-replace \
                    --build=${os.arch}-unknown-${os.platform} \
                    --build=${os.arch}-unknown-freebsd
}

variant pingd_server description {pingd daemon to graph network latency with NRG} {
    startupitem.create      yes
    startupitem.name        pingd
    startupitem.executable  "${prefix}/bin/pingd"
}

build.target                pingd

destroot.target             install.pingd install
destroot.keepdirs           ${destroot}${prefix}/var/${name}/archive

post-patch {
### Path fixes ###
    reinplace "s|@prefix@/bin|@bindir@|g" \
        ${worksrcpath}/examples/NRG.mconf.in \
        ${worksrcpath}/src/nrg-spiketrimmer.in

    reinplace -q "s|@prefix@/bin|@bindir@|g" \
        {*}[glob ${worksrcpath}/templates/*.in]

#### Make NRG page title the way I want it ####
    reinplace "s|Somesite's NRG Home Page|NRG Home Page|g" \
        "${worksrcpath}/examples/Makefile.ops.in"

#### Fix paths for proper destrooting ####
    reinplace "s|exec_prefix = @prefix@|exec_prefix = ${destroot}@prefix@|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|prefix = @prefix@|prefix = ${destroot}@prefix@|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|BIN_DIR = @bindir@|BIN_DIR = ${destroot}@bindir@|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|ETC_DIR = @sysconfdir@|ETC_DIR = ${destroot}@sysconfdir@|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|TEMPLATE_DIR = \${prefix}/templates|TEMPLATE_DIR = ${destroot}@prefix@/templates|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|EXAMPLE_DIR = \${prefix}/examples|EXAMPLE_DIR = ${destroot}@datadir@/doc/nrg/examples|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|HTML_DIR = \${prefix}/html|HTML_DIR = ${destroot}@datadir@/doc/nrg/html|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|ARCHIVE_DIR = \${prefix}/archive|ARCHIVE_DIR = ${destroot}@prefix@/archive|g" \
        ${worksrcpath}/Makefile.in
    reinplace "s|CONTRIB_DIR = \${prefix}/contrib|CONTRIB_DIR = ${destroot}@datadir@/doc/nrg/contrib|g" \
        ${worksrcpath}/Makefile.in
}

post-destroot {
# Install images to \${prefix}/var/nrg/web/icons
    xinstall -d -m 0755 ${destroot}${prefix}/var/${name}/web/icons
    xinstall -m 0755 {*}[glob ${worksrcpath}/images/*.*] ${destroot}${prefix}/var/${name}/web/icons

# Rename Site.mconf so graphed devices won't get rewritten on port upgrades
    move ${destroot}${prefix}/var/${name}/Site.mconf \
        ${destroot}${prefix}/var/${name}/Site.mconf.sample
}

livecheck.type      none

notes \
"#### To complete the NRG installation ####

1) Make an /nrg symlink in your Apache document root pointing to the ../nrg/web NRG data directory:

      sudo ln -s ${prefix}/var/${name}/web /my-Apache-document-root/nrg

2) Modify your httpd.conf file for NRG graph pages:

Uncomment lines:

           AddHandler cgi-script .cgi
           LoadModule expires_module   libexec/httpd/mod_expires.so
           AddModule mod_expires.c

Add directives:

           <Directory /my-Apache-document-root/nrg>
              Options ExecCGI
           </Directory>

           <Files \"*.gif\">
              ExpiresActive On
              ExpiresDefault M5
           </Files>

Change the Apache user and group to the NRG user and group so Apache will
have permissions to create graphs when you click an RRD file link.

           User <nrg-user>
           Group <nrg-group>

3) Change the web root variables at the top of files Makefile and Site.mconf
   in directory ${prefix}/var/${name} to your Apache document root location.

4) Make NRG files and data owned by the user you'll use to run NRG

   su  (must be root for this operation)
   find ${prefix}/var/nrg -print | xargs chown <nrg-user>:<nrg-group>


#### To use NRG ####

1) Set meta-configuration definitions in the Site.mconf file for each
   resource you want to graph by following the example meta-configuration
   definitions in ${prefix}/share/doc/nrg/examples.

2) Have NRG generate rrd files and html graph pages (and modify them after
   Site.mconf changes):

      cd ${prefix}/var/${name}
      make rediscover
      make notify

3) Schedule the script ${prefix}/var/${name}/run-nrg to run via launchd or cron
   every five minutes to poll your data sources and update the rrd files.

Be sure to read the documentation in ${prefix}/share/doc/${name}/html for
creating custom graphs or for more information."
