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

PortSystem 1.0

name                vnstat
version             1.14
categories          net
platforms           darwin
maintainers         gmail.com:youvegotmoxie
license             GPL-2
description         console-based network traffic monitor
long_description    \
    vnStat is a console-based network traffic monitor for Linux and BSD that keeps \
    a log of network traffic for the selected interface(s), it uses the network \
    interface statistics provided by the kernel as information source.  This means \
    that vnStat won't actually be sniffing any traffic and also ensures light use \
    of system resources.

homepage            http://humdi.net/vnstat/
depends_lib         port:gd2
master_sites        http://humdi.net/${name}

checksums           rmd160  9cbe5d4f11d757ae19db4b38155de63a8caf6164 \
                    sha256  f8462a47d85d0890493dc9eaeafbc725ae631aa5b103fb7f8af4ddb2314e8386

post-patch {
    # fix Makefile
    reinplace "s|/usr/local|${prefix}|g"                 ${worksrcpath}/Makefile
    reinplace "s|${prefix}/man|${prefix}/share/man|g"    ${worksrcpath}/Makefile
    reinplace "s|(DESTDIR)/etc|(DESTDIR)${prefix}/etc|g" ${worksrcpath}/Makefile
    reinplace "s|(DESTDIR)/var|(DESTDIR)${prefix}/var|g" ${worksrcpath}/Makefile

    # fix occurrences of /var/db references in other files
    reinplace "s|/var|${prefix}/var|g"  ${worksrcpath}/src/common.h \
                                        ${worksrcpath}/cfg/vnstat.conf \
                                        ${worksrcpath}/man/vnstat.1 \
                                        ${worksrcpath}/man/vnstatd.1 \
                                        ${worksrcpath}/man/vnstati.1
    reinplace "s|/var/lib|/var/db|g"    ${worksrcpath}/man/vnstat.1 \
                                        ${worksrcpath}/man/vnstatd.1 \
                                        ${worksrcpath}/man/vnstati.1

    # fix occurrences of /etc/db references in other files
    reinplace "s|/etc|${prefix}/etc|g"  ${worksrcpath}/man/vnstat.1 \
                                        ${worksrcpath}/man/vnstat.conf.5 \
                                        ${worksrcpath}/man/vnstatd.1 \
                                        ${worksrcpath}/man/vnstati.1 \
                                        ${worksrcpath}/src/cfg.c
}

use_configure       no
variant universal   {}

build.args-append   CC="${configure.cc} [get_canonical_archflags]" \
                    CPP=${configure.cpp} \
                    CFLAGS="-I${prefix}/include" \
                    LDFLAGS="-L${prefix}/lib"

destroot.target     bsdinstall
destroot.keepdirs   ${destroot}${prefix}/var/db/vnstat
post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 755 -d ${destroot}${prefix}/share/examples/${name}
    
    xinstall -m 644 -W ${worksrcpath} \
        CHANGES COPYING FAQ INSTALL INSTALL_BSD README \
        ${destroot}${prefix}/share/doc/${name}

    eval file copy [glob ${worksrcpath}/examples/*] \
        ${destroot}${prefix}/share/examples/${name}

    # Rename conf file so we don't clobber a user's real conf file on upgrade.
    move ${destroot}${prefix}/etc/vnstat.conf ${destroot}${prefix}/etc/vnstat.conf.sample
}

notes "
A configuration file has been installed to ${prefix}/etc/vnstat.conf
"

pre-activate {
    if {[file exists ${prefix}/etc/vnstat.conf]} {
        # Clear the notes if there is already a conf file
        notes
    }
}

post-activate {
    if {![file exists ${prefix}/etc/vnstat.conf]} {
        copy ${prefix}/etc/vnstat.conf.sample \
            ${prefix}/etc/vnstat.conf
    }
}