# -*- 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
PortGroup           github 1.0

github.setup        vergoh vnstat 2.14 v
github.tarball_from releases
revision            0
categories          net
maintainers         {i0ntempest @i0ntempest} openmaintainer
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/${name}/

checksums           rmd160  7b7533e53941c674efd75d26dfdaff1825ce279b \
                    sha256  8ba2119b3c6f780775135f9c9fa7236867da67e2f93649f1313319b9cf48f87c \
                    size    384609

depends_lib         port:gd2 \
                    port:sqlite3

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

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 system configuration paths, preserving the /usr/local/etc fallback
    reinplace "s| /etc| ${prefix}/etc|g" \
                                        ${worksrcpath}/man/vnstat.conf.5
    reinplace "s|\"/etc|\"${prefix}/etc|g" \
                                        ${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
    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

post-activate {
    if {![file exists ${prefix}/etc/vnstat.conf]} {
        copy ${prefix}/etc/vnstat.conf.sample \
            ${prefix}/etc/vnstat.conf
        notes-append "
            A configuration file has been installed to ${prefix}/etc/vnstat.conf
        "
    }
}
