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

name                wireshark4
categories          net
license             {GPL-2 GPL-3}
maintainers         {darkart.com:opendarwin.org @ghosthound}
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.

##
## error out if we're on macOS < 10.12 (darwin 16)
## while not strictly true (we can probably build with an older qt5),
##  this seems like a reasonable cutoff to keep this port simpler.
## If someone wants to make wireshark 4.x work on older macOS patches/PRs welcome.
##
if { ${os.platform} eq "darwin" && ${os.major} < 16 } {
    known_fail  yes
    pre-fetch {
        ui_error "${name} requires macOS 10.12 or later"
        return -code error "incompatible OS version"
    }
}

version         4.6.0
revision        0

checksums       sha256  ab016463062bb635285b9678dd45ddd84c65938911fd40b3cca9a903a08ad8d9 \
                sha1    9440b807951ecc9b46f261a64b33e3cd4d0b17fa \
                size    50726944

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

master_sites        https://www.wireshark.org/download/src/ \
                    https://www.wireshark.org/download/src/all-versions/

use_xz              yes

distfiles           wireshark-${version}${extract.suffix}

worksrcdir          wireshark-${version}

conflicts           wireshark2 wireshark22 wireshark3 wireshark30

depends_build-append \
                    port:pkgconfig

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

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

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 no_gui conflicts qt5 qt6 description {do not build the wireshark GUI} {
    ## initial settings (above) handle this
}

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

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

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

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

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

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 libmaxminddb description {Build with libmaxminddb support} {
    configure.args-replace  -DBUILD_mmdbresolve=OFF -DBUILD_mmdbresolve=ON
    depends_lib-append      port:libmaxminddb
}


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

set pythons_suffixes {39 310 311 312 313}

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


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

if {![variant_isset qt5] && ![variant_isset qt6] && ![variant_isset no_gui]} {
    if {${os.major} >=20} {
        default_variants-append +qt6
    } else {
        default_variants-append +qt5
    }
}

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

