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

# strnlen
legacysupport.newest_darwin_requires_legacy 10

name                wireshark4
version             4.7.2
revision            0
categories          net
license             {GPL-2 GPL-3}
maintainers         {darkart.com:opendarwin.org @ghosthound} \
                    {@barracuda156 macos-powerpc.org:barracuda}
description         Graphical network analyzer and capture tool
homepage            https://www.wireshark.org
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.
master_sites        https://www.wireshark.org/download/src/ \
                    https://www.wireshark.org/download/src/all-versions/
use_xz              yes
distfiles           wireshark-${version}${extract.suffix}
checksums           rmd160  6a32447e3f1caeb4b5dc4166cd1b935f4cf29d48 \
                    sha256  f94df4731d9fcb86240950fde44b3db52ffdd04e918b558e613891d0b578dadc \
                    size    58615112

conflicts           wireshark2 wireshark22 wireshark3 wireshark30

worksrcdir          wireshark-${version}

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

depends_lib-append  port:c-ares \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:libgcrypt \
                    port:libpcap \
                    port:libssh \
                    port:libxml2 \
                    port:lz4 \
                    port:nghttp2 \
                    port:nghttp3 \
                    port:pcre2 \
                    port:speexdsp \
                    port:zstd

# Target "cmTC_82ab0" requires the language dialect "C11" (with compiler extensions).
compiler.c_standard 2011

configure.args-append \
                    -DBUILD_mmdbresolve=OFF \
                    -DBUILD_wireshark=OFF \
                    -DCMAKE_INSTALL_NAME_DIR:STRING="${prefix}/lib" \
                    -DCMAKE_INSTALL_RPATH:STRING="${prefix}/lib" \
                    -DENABLE_APPLICATION_BUNDLE=OFF \
                    -DENABLE_BROTLI=OFF \
                    -DENABLE_GNUTLS=OFF \
                    -DENABLE_KERBEROS=OFF \
                    -DENABLE_LIBXML2=ON \
                    -DENABLE_LUA=OFF \
                    -DENABLE_LZ4=ON \
                    -DENABLE_MINIZIP=OFF \
                    -DENABLE_NETLINK=OFF \
                    -DENABLE_NGHTTP2=ON \
                    -DENABLE_NGHTTP3=ON \
                    -DENABLE_OPUS=OFF \
                    -DENABLE_SMI=OFF \
                    -DENABLE_SNAPPY=OFF \
                    -DENABLE_SPARKLE=OFF \
                    -DENABLE_ZLIB=OFF \
                    -DENABLE_ZLIBNG=OFF

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

variant qt5 conflicts qt6 no_gui description {Build wireshark GUI with qt5} {
    PortGroup               qt5 1.0

    configure.args-replace  -DENABLE_APPLICATION_BUNDLE=OFF -DENABLE_APPLICATION_BUNDLE=ON
    configure.args-replace  -DBUILD_wireshark=OFF -DBUILD_wireshark=ON
    configure.args-append   -DUSE_qt6=OFF
    qt5.depends_build_component \
                            qttools
    qt5.depends_component   qtbase \
                            qtmacextras \
                            qtmultimedia \
                            qtsvg \
                            qttranslations
}

variant qt6 conflicts qt5 no_gui description {Build wireshark GUI with qt6} {
    PortGroup               qt6 1.0

    configure.args-replace  -DENABLE_APPLICATION_BUNDLE=OFF -DENABLE_APPLICATION_BUNDLE=ON
    configure.args-replace  -DBUILD_wireshark=OFF -DBUILD_wireshark=ON
    configure.args-append   -DUSE_qt6=ON
    qt6.depends_build       qttools
    qt6.depends_lib         qtbase \
                            qtmultimedia \
                            qtsvg \
                            qttranslations \
                            qt5compat
}

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

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

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 lua description {Build with Lua dissector support} {
    configure.args-replace  -DENABLE_LUA=OFF -DENABLE_LUA=ON
    depends_lib-append      port:lua52
}

variant libmaxminddb description {Build with libmaxminddb support} {
    configure.args-replace  -DBUILD_mmdbresolve=OFF -DBUILD_mmdbresolve=ON
    depends_lib-append      port:libmaxminddb
}

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

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

variant no_gui conflicts qt5 qt6 description {Do not build the wireshark GUI} {
    ## initial settings (above) handle this
}

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

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

set pythons_suffixes {310 311 312 313 314}

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 +python314
# XYZZY: it would be better to detect which python3* is already installed and use that...
if {!${python_isset}} {
    default_variants-append +python314
}

default_variants +brotli +chmodbpf +gnutls +kerberos5 +libmaxminddb +libsmi +snappy +zlib

if {![variant_isset qt5] && ![variant_isset qt6] && ![variant_isset no_gui]} {
    if {(${os.platform} ne "darwin") || ${os.major} >= 20} {
        default_variants-append +qt6
    } elseif {${os.major} >= 16} {
        default_variants-append +qt5
    } else {
        default_variants-append +no_gui
    }
}

if {[variant_isset qt5] || [variant_isset qt6]} {
    post-destroot {
        move ${destroot}${prefix}/bin/Wireshark.app ${destroot}${applications_dir}/Wireshark.app
        # It appears that the binaries in the Wireshark.app/Contents/MacOS directory need
        # an rpath entry to "${prefix}/lib" in order for various plugins to work.
        # cribbed from lang/gcc10
        set mp_extra_rpath ${prefix}/lib
        foreach binfile [ exec find ${destroot}${applications_dir}/Wireshark.app/Contents/MacOS -type f ] {
            ui_debug "Ensuring binary '${binfile}' has RPATH '${mp_extra_rpath}'"
            # Note install_name_tool returns a failure if the dylib already has the entry
            # We don't want that here so force final status to be true...
            system "install_name_tool -add_rpath ${mp_extra_rpath} ${binfile} > /dev/null 2>&1 ; true"
        }
    }
}

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 {*}[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/

    # Grab some headers from the .../work/build/ directory:
    xinstall -m 644 -W ${workpath}/build config.h vcs_version.h ws_version.h ${destroot}${prefix}/include/wireshark/
    xinstall -m 644 {*}[glob ${workpath}/build/epan/*.h] ${destroot}${prefix}/include/wireshark/epan/
    xinstall -m 644 {*}[glob ${workpath}/build/epan/dfilter/*.h] ${destroot}${prefix}/include/wireshark/epan/dfilter/
    xinstall -m 644 {*}[glob ${workpath}/build/wiretap/*.h] ${destroot}${prefix}/include/wireshark/wiretap/
}

livecheck.type      regex
livecheck.url       ${homepage}download/src/
livecheck.regex     "wireshark-(\\d+(?:\\.\\d+)*)\\.*"
