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

name                wireshark3
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/
platforms           darwin

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.

# Include patch for CLOCK_REALTIME build errors, on macOS releases prior to 10.12.
# While a fix has been included in upstream 3.4.2, that release also requires
# QT59... and is a no-go for these older macOS releases.
#
# So for now, we're stuck with 3.4.1, for users running macOS 10.11 and earlier.
#
# MacPorts issue, with all of the juicy details:
#   https://trac.macports.org/ticket/61511
# GitHub issue:
#   https://gitlab.com/wireshark/wireshark/-/issues/17101
# Upstream fix committed:
#   https://gitlab.com/wireshark/wireshark/-/merge_requests/1404

if {${os.major} >= 16} {
    version         3.6.8
    revision        0

    checksums       sha256  67ab23ee09a646ab8cc0ddcc985209f24f253337461e3d9644c6cfe1c097260c \
                    sha1    e4b2cb5703f9d6cf0934469a2e8a2426cb308dad \
                    size    39942580

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

    checksums       sha256  f8165211f5b4a4f6708df73ef9be51df917927f2da78348b32d3a6eb5fc458a3 \
                    rmd160  1b5e1fee340c149b70dbe8e8cf935518b06656e8 \
                    sha1    3c9a24b8954d712a189f997131e283fbd0b606bc \
                    size    32470004

    patchfiles-append \
                    patch-wireshark3-3.4.1-fix-clock_realtime.diff

    livecheck.type  none
}

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 wireshark30 wireshark4

depends_build-append \
                    port:pkgconfig

depends_lib         path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    path:lib/libssl.dylib:openssl \
                    port:libgcrypt \
                    port:libpcap \
                    port:libssh \
                    port:lz4

configure.args-append \
                    -DCMAKE_INSTALL_RPATH:STRING="${prefix}/lib" \
                    -DCMAKE_INSTALL_NAME_DIR:STRING="${prefix}/lib" \
                    -DENABLE_APPLICATION_BUNDLE=OFF \
                    -DENABLE_ADNS=OFF \
                    -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_SMI=OFF \
                    -DENABLE_SNAPPY=OFF \
                    -DENABLE_ZLIB=OFF \
                    -DBUILD_wireshark=OFF

variant qt5 conflicts no_gui description {Build wireshark with a qt5 GUI} {
    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   -DENABLE_QT5=ON
    qt5.depends_build_component \
                            qttools
    qt5.depends_component   qtbase \
                            qtmacextras \
                            qtmultimedia \
                            qtsvg \
                            qttranslations

    post-destroot {
        move ${destroot}${prefix}/bin/Wireshark.app ${destroot}${applications_dir}/Wireshark.app
    }
}

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

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

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

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


default_variants +zlib +libsmi +gnutls +geoip +kerberos5 +chmodbpf

if {![variant_isset qt5] && ![variant_isset no_gui]} {
    default_variants-append +qt5
}

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

