# -*- 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           cmake 1.0
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0

# strnlen
legacysupport.newest_darwin_requires_legacy 10

name                wireshark3
version             3.6.24
revision            0
categories          net
license             {GPL-2 GPL-3}
maintainers         {darkart.com:opendarwin.org @ghosthound}
description         Graphical network analyzer and capture tool
long_description    A network analyzer that lets you capture \
                    and interactively browse Ethernet frames. \
                    Packet data can be read from a file, or live \
                    from a local network interface.
homepage            https://www.wireshark.org
master_sites        ${homepage}/download/src/ \
                    ${homepage}/download/src/all-versions/
use_xz              yes
distfiles           wireshark-${version}${extract.suffix}
checksums           sha256  94e0642bfa0678fc98905785679697f1bd777fa0534081d432fbe95b7c80c42b \
                    rmd160  993ad5ce7c68b0701b89b340a43337f2d14f5610 \
                    size    40303900

conflicts           wireshark2 wireshark22 wireshark30 wireshark4

worksrcdir          wireshark-${version}

depends_build-append \
                    port:flex \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:brotli \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:libgcrypt \
                    port:libpcap \
                    port:libssh \
                    port:lz4 \
                    port:speexdsp \
                    port:zlib \
                    port:zstd

# Qt5+ are broken at the moment, so we build a CLI app.
configure.args-append \
                    -DBUILD_wireshark=OFF \
                    -DCMAKE_INSTALL_NAME_DIR:STRING="${prefix}/lib" \
                    -DCMAKE_INSTALL_RPATH:STRING="${prefix}/lib" \
                    -DENABLE_ADNS=OFF \
                    -DENABLE_APPLICATION_BUNDLE=OFF \
                    -DENABLE_BROTLI=ON \
                    -DENABLE_CARES=OFF \
                    -DENABLE_GCRYPT=ON \
                    -DENABLE_GEOIP=OFF \
                    -DENABLE_GNUTLS=OFF \
                    -DENABLE_KERBEROS=OFF \
                    -DENABLE_LIBXML2=OFF \
                    -DENABLE_LUA=OFF \
                    -DENABLE_LZ4=ON \
                    -DENABLE_MINIZIP=OFF \
                    -DENABLE_NETLINK=OFF \
                    -DENABLE_NGHTTP2=OFF \
                    -DENABLE_OPUS=OFF \
                    -DENABLE_SMI=OFF \
                    -DENABLE_SNAPPY=OFF \
                    -DENABLE_ZLIB=ON \
                    -DENABLE_ZSTD=ON

# epan/dissectors/packet-usb-hid.c:3693:25: error: invalid suffix "b11110000" on integer constant
compiler.blacklist-append \
                    *gcc-4.0 *gcc-4.2

# Someone just can't live happily without breaking a build...
patchfiles-append           patch-do-no-break-this-for-no-good-reason.diff

if {${os.platform} eq "darwin" && [string match macports-gcc* ${configure.compiler}]} {
    patchfiles-append       patch-fix-crash_info.c.diff
}

variant adns conflicts cares description "Use adns library for async. dns resolution" {
    configure.args-replace  -DENABLE_ADNS=OFF -DENABLE_ADNS=ON
    depends_lib-append      port:adns
}

variant cares conflicts adns description description "Build with c-ares support" {
    configure.args-replace  -DENABLE_CARES=OFF -DENABLE_CARES=ON
    depends_lib-append      port:c-ares
}

variant chmodbpf description "Enable Wireshark to access macOS capture devices" {
    depends_run             port:wireshark-chmodbpf
}

variant geoip description "Build with GeoIP support" {
    configure.args-replace  -DENABLE_GEOIP=OFF -DENABLE_GEOIP=ON
    depends_lib-append      port:libgeoip \
                            port:libmaxminddb
}

variant gnutls description "Build with GNU TLS support" {
    configure.args-replace  -DENABLE_GNUTLS=OFF -DENABLE_GNUTLS=ON
    depends_lib-append      path:lib/pkgconfig/gnutls.pc:gnutls
}

variant kerberos5 description "Build with Kerberos support" {
    configure.args-replace  -DENABLE_KERBEROS=OFF -DENABLE_KERBEROS=ON \
    configure.args-append   -DCMAKE_SHARED_LINKER_FLAGS="-lk5crypto"
    depends_lib-append      port:kerberos5
}

variant libsmi description "Build with libsmi snmp support" {
    configure.args-replace  -DENABLE_SMI=OFF -DENABLE_SMI=ON
    depends_lib-append      port:libsmi
}

variant lua description "Build with Lua dissector support" {
    configure.args-replace  -DENABLE_LUA=OFF -DENABLE_LUA=ON
    depends_lib-append      port:lua52
}

variant minizip description "Build with minizip support" {
    configure.args-replace -DENABLE_MINIZIP=OFF -DENABLE_MINIZIP=ON
    depends_lib-append      port:minizip
}

variant nghttp2 description "Build with nghttp2 support" {
    configure.args-replace -DENABLE_NGHTTP2=OFF -DENABLE_NGHTTP2=ON
    depends_lib-append      port:nghttp2
}

variant snappy description "Build with snappy compression support" {
    configure.args-replace  -DENABLE_SNAPPY=OFF -DENABLE_SNAPPY=ON
    depends_lib-append      port:snappy
}

set pythons_suffixes {39 310 311}

set pythons_ports {}
foreach py_ver_nodot ${pythons_suffixes} {
    lappend pythons_ports python${py_ver_nodot}
}

foreach py_ver_nodot ${pythons_suffixes} {
    set py_port python${py_ver_nodot}
    set py_ver [string index ${py_ver_nodot} 0].[string range ${py_ver_nodot} 1 end]
    set i [lsearch -exact ${pythons_ports} ${py_port}]
    set py_conflicts [lreplace ${pythons_ports} ${i} ${i}]
    variant ${py_port} description "Use python ${py_ver} during build" conflicts {*}${py_conflicts} "
        depends_build-append      port:${py_port}
        configure.args-append     -DPython3_ROOT_DIR=\"${frameworks_dir}/Python.framework/Versions/${py_ver}/bin\"
        configure.args-append     -DPython3_FIND_STRATEGY=LOCATION
    "
}

# make sure one of the +python* variants is set
set python_isset false
foreach py_ver_nodot ${pythons_suffixes} {
    if { [variant_isset python${py_ver_nodot}] } {
        set python_isset true
        break
    }
}

# if no python3* variant is specified, add +python311
# XYZZY: it would be better to detect which python3* is already installed and use that...
if {!${python_isset}} {
    default_variants-append +python311
}

default_variants +chmodbpf +geoip +gnutls +kerberos5 +libsmi +nghttp2

if {![variant_isset adns] && ![variant_isset cares]} {
    default_variants-append +cares
}

post-destroot {
    xinstall -d ${destroot}${prefix}/include/wireshark/epan/crypt/
    xinstall -d ${destroot}${prefix}/include/wireshark/epan/dfilter/
    xinstall -d ${destroot}${prefix}/include/wireshark/epan/dissectors/
    xinstall -d ${destroot}${prefix}/include/wireshark/epan/ftypes/
    xinstall -d ${destroot}${prefix}/include/wireshark/wiretap/
    xinstall -m 644 -W ${worksrcpath}/ config.h ${destroot}${prefix}/include/wireshark/
    xinstall -m 644 {*}[glob ${worksrcpath}/epan/*.h] ${destroot}${prefix}/include/wireshark/epan/
    xinstall -m 644 {*}[glob ${worksrcpath}/epan/crypt/*.h] ${destroot}${prefix}/include/wireshark/epan/crypt/
    xinstall -m 644 {*}[glob ${worksrcpath}/epan/dfilter/*.h] ${destroot}${prefix}/include/wireshark/epan/dfilter/
    xinstall -m 644 {*}[glob ${worksrcpath}/epan/dissectors/*.h] ${destroot}${prefix}/include/wireshark/epan/dissectors/
    xinstall -m 644 {*}[glob ${worksrcpath}/epan/ftypes/*.h] ${destroot}${prefix}/include/wireshark/epan/ftypes/
    xinstall -m 644 {*}[glob ${worksrcpath}/wiretap/*.h] ${destroot}${prefix}/include/wireshark/wiretap/
}

livecheck.type  regex
livecheck.url   ${homepage}download.html
livecheck.regex "Stable Release \\((\\d+(?:\\.\\d+)*)"
