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

name                arpwatch
version             3.8
revision            0
categories          net
license             BSD
maintainers         {wohner.eu:normen @Gminfly} openmaintainer

description         Monitor ARP & RARP requests
long_description    Arpwatch monitors ethernet activity and maintains a \
                    database of ethernet/ip address pairings. It also \
                    reports certain changes via email.

homepage            https://ee.lbl.gov
master_sites        ${homepage}/downloads/arpwatch/

checksums           rmd160  0b03057d1d2f9c3b04e4bd5f1782dddee6835d8b \
                    sha256  c763409f3534b8b3f119173cd92a4b9d42378b6c666cc00b255cc036d318b29c \
                    size    117299

livecheck.url       "http://www.freebsd.org/cgi/ports.cgi?query=${name}&stype=all"
livecheck.regex     ">${name}-(.*?)<"

set python_branch   3.13
set python_version  [string map {. ""} ${python_branch}]

# Update config.guess and config.sub to be able to build on arm64.
depends_build-append \
                    port:automake \
                    port:wget

depends_run-append  port:net-snmp

# Python is need for helper scripts only but we need it in post-destroot
depends_lib-append  port:libpcap \
                    port:python${python_version}

post-extract {
    # See ${worksrcpath}/update-ethercodes.sh.in
    system -W ${worksrcpath} "wget https://standards-oui.ieee.org/oui/oui.csv"
}

patchfiles          _getshort.patch

set sharedir ${prefix}/share/${name}-${version}
set ethercodes ${sharedir}/ethercodes.dat
post-patch {
    reinplace "s|-DARPDIR|-DETHERCODES=\\\\\"${ethercodes}\\\\\" -DARPDIR|" ${worksrcpath}/Makefile.in
    reinplace "s|INCLS = -I.|INCLS = -I. -I${prefix}/include|" ${worksrcpath}/Makefile.in
    reinplace "s|LIBS = @LIBS@|LIBS = -L${prefix}/lib @LIBS@|" ${worksrcpath}/Makefile.in
    reinplace "s|ARPDIR ?= .*|ARPDIR ?= ${prefix}/var/arpwatch|" ${worksrcpath}/Makefile.in
    reinplace "s|/usr/local/arpwatch|${prefix}/var/arpwatch|" {*}[glob -directory ${worksrcpath} *.8.in]
    reinplace "/.*\\/etc\\/rc.d.*/D" ${worksrcpath}/Makefile.in
    reinplace "s|/usr/local|${prefix}|" ${worksrcpath}/arpfetch ${worksrcpath}/bihourly.sh
    reinplace "s|sort +2rn arp.dat|(\[ -f arp.dat \] \\&\\& sort +2rn arp.dat \\|\\| sort +2rn \$1)|" ${worksrcpath}/arp2ethers
    reinplace -E "s%(.\\.awk)%${sharedir}/\\1%g" ${worksrcpath}/arp2ethers
    set automake_dirs [glob -directory ${prefix}/share automake-*]
    set automake_dir [lindex [lsort -command vercmp $automake_dirs] end]
    copy -force ${automake_dir}/config.guess ${automake_dir}/config.sub \
        ${worksrcpath}
}

configure.cflags-append \
                    "-DBIND_8_COMPAT=1"
configure.args-append \
                    PYTHON=${prefix}/bin/python${python_branch}

post-configure {
    reinplace "/#define ETHERCODES .*/D" ${worksrcpath}/arpwatch.h
}

post-destroot {
    set destsharedir ${destroot}${sharedir}
    file mkdir ${destsharedir}
    xinstall -m 755 -W ${build.dir} massagevendor-old massagevendor.py ${destroot}${prefix}/bin
    xinstall -m 755 -W ${build.dir} arp2ethers arpfetch bihourly.sh ${destroot}${prefix}/sbin
    xinstall -m 644 -W ${build.dir} CHANGES FILES INSTALL README d.awk duplicates.awk \
                                        e.awk euppertolower.awk p.awk \
                                        ${destsharedir}
    system -W ${worksrcpath} "${prefix}/bin/python${python_branch} ${destroot}${prefix}/bin/massagevendor.py \
                                ./oui.csv > ${destroot}${ethercodes}"
}
