# $Id: Portfile 51545 2009-05-27 18:21:55Z markd@macports.org $

PortSystem	1.0

name				nrg
version				0.99.27
categories			net
maintainers			markd
platforms			darwin

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.

homepage			http://nrg.hep.wisc.edu/
master_sites		        ftp://noc.hep.wisc.edu/src/nrg/ \
				ftp://noc.hep.wisc.edu/pub/src/nrg/
checksums			md5 c38e6b27bc99f8f7fd825bc6e1a44f68
depends_lib			port:rrdtool port:p5-time-hires
patchfiles			patch-nrg-discover-tcp.in.diff

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

configure.args               	--bindir=${prefix}/bin \
				--sysconfdir=${prefix}/etc/${name} \
				--datadir=${prefix}/share \
				--build=powerpc-unknown-freebsd \
				CFLAGS="-D__FreeBSD__"

platform i386 {
	configure.args-delete	--build=powerpc-unknown-freebsd
        configure.args-append   --build=i386-unknown-freebsd
}

variant pingd_server  {
	startupitem.create	yes
	startupitem.name	pingd
	startupitem.executable	"${prefix}/bin/pingd"
}

build.target                    pingd
destroot.target                 install.pingd install

# Maintain empty directories
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

        eval reinplace "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 = \${prefix}/etc|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 755 ${destroot}${prefix}/var/${name}/web/icons
	eval xinstall -m 755 [glob ${worksrcpath}/images/*.*] ${destroot}${prefix}/var/${name}/web/icons

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

post-activate {

ui_msg "\n#### 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 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.\n"

}

