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

# Mavericks and older versions don't include fdopendir function
legacysupport.newest_darwin_requires_legacy 13

name                vnstat
version             2.13
revision            0
categories          net
maintainers         nomaintainer
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            https://humdi.net/vnstat/
master_sites        https://humdi.net/${name}

checksums           rmd160  3e128a840fbc8af7469d5ddc296d184b137aec9f \
                    sha256  c9fe19312d1ec3ddfbc4672aa951cf9e61ca98dc14cad3d3565f7d9803a6b187 \
                    size    336647

depends_lib         port:gd2

post-patch {
    # 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.8 \
                                        ${worksrcpath}/man/vnstati.1
    reinplace "s|/var/lib|/var/db|g"    ${worksrcpath}/man/vnstat.1 \
                                        ${worksrcpath}/man/vnstatd.8 \
                                        ${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.8 \
                                        ${worksrcpath}/man/vnstati.1 \
                                        ${worksrcpath}/src/cfg.c
    # fix occurrences of /usr/bin/vnstat references in example files
    reinplace "s|/usr/bin/vnstat|${prefix}/bin/vnstat|g" \
                                        ${worksrcpath}/examples/vnstat-json.cgi \
                                        ${worksrcpath}/examples/vnstat-json.php \
                                        ${worksrcpath}/examples/vnstat-metrics.cgi \
                                        ${worksrcpath}/examples/vnstat.cgi
    reinplace "s|/usr/bin/vnstati|${prefix}/bin/vnstati|g" \
                                        ${worksrcpath}/examples/vnstat.cgi
}

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}

    file copy {*}[glob ${worksrcpath}/examples/*] \
        ${destroot}${prefix}/share/examples/${name}
    file delete -force ${destroot}${prefix}/share/examples/${name}/init.d
    file delete -force ${destroot}${prefix}/share/examples/${name}/systemd
    file delete -force ${destroot}${prefix}/share/examples/${name}/upstart

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

startupitem.create      yes
startupitem.name        vnstatd
startupitem.start       "${prefix}/sbin/vnstatd -n"
startupitem.stop        "/bin/kill \$(cat ${prefix}/var/run/vnstatd.pid)"
startupitem.logfile     ${prefix}/var/log/vnstatd.log
startupitem.pidfile     auto ${prefix}/var/run/vnstatd.pid
startupitem.logevents   yes
startupitem.netchange   yes

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
    }
}
