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

name                ubertooth
categories          devel net security
description         Software, firmware and hardware designs for Ubertooth.
long_description    Project Ubertooth is an open source wireless development platform suitable \
                    for Bluetooth experimentation. Ubertooth ships with a capable BLE (Bluetooth Smart) \
                    sniffer and can sniff some data from Basic Rate (BR) Bluetooth Classic connections.
license             GPL-2
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
homepage            https://ubertooth.readthedocs.io

subport             ${name}-devel {}

if {${subport} eq "${name}"} {
    conflicts       ${name}-devel

    github.setup    greatscottgadgets ubertooth 2020-12-R1
    revision        1
    checksums       rmd160  078b6b2ae3dc7e003358d049800d804fa74ba834 \
                    sha256  24dd76995419de73dcf4f17bb341f386826d68071191e1d7d2d30a699949102d \
                    size    1373134
    github.tarball_from tarball
} elseif {${subport} eq "${name}-devel"} {
    conflicts       ${name}

    github.setup    greatscottgadgets ubertooth e0fd34d8abb8ee9edda5a8172f218454365e2ba5
    version         2023-04-04
    revision        0
    checksums       rmd160  b35d4d8cc2e1929b0805330f716a7075916f4852 \
                    sha256  47e8454f910425ed1651c1de8cfbb5d5822d24ba1ca414138c75d8881d8b6a21 \
                    size    1307422
    github.tarball_from archive
}

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

patchfiles          patch-python3.diff

set python_branch   3.11
set python_ver      [string map {. {}} ${python_branch}]
configure.python    ${prefix}/bin/python${python_branch}

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

depends_lib-append  path:lib/pkgconfig/libusb-1.0.pc:libusb

# It is advised to used paired versions:
if {${subport} eq "${name}"} {
    depends_lib-append  path:lib/pkgconfig/libbtbb.pc:libbtbb
} elseif {${subport} eq "${name}-devel"} {
    depends_lib-append  path:lib/pkgconfig/libbtbb.pc:libbtbb-devel
}

post-patch {
    fs-traverse f ${worksrcpath}/host/python {
        if {[file extension ${f}] eq ".py"} {
            reinplace -q "s|/usr/bin/env python3|${configure.python}|" ${f}
        }
    }
    reinplace "s|/usr/bin/env python3|${configure.python}|" ${worksrcpath}/host/python/ubtbr/ubertooth-btbr
    reinplace "s|/usr/bin/env python3|${configure.python}|" ${worksrcpath}/host/python/specan_ui/ubertooth-specan-ui
    reinplace "s|/usr/bin/env python3|${configure.python}|" ${worksrcpath}/host/python/specan_ui/setup.py.in
    reinplace "s|@PYTHON3@|${configure.python}|g" ${worksrcpath}/host/python/ubtbr/CMakeLists.txt
}

cmake.source_dir    ${worksrcpath}/host
cmake.module_path   ${cmake.source_dir}/cmake/modules

compiler.blacklist-append *gcc-4.*

if {${configure.build_arch} in [list ppc ppc64]} {
    # TODO: I guess Qt4-based versions will not do?
    configure.args-append \
                    -DENABLE_PYTHON=OFF
    # Needed for Rosetta
    compiler.blacklist-append clang
} else {
    depends_lib-append \
                    port:python${python_ver} \
                    port:py${python_ver}-numpy \
                    port:py${python_ver}-pyside \
                    port:py${python_ver}-qtpy
    depends_build-append \
                    port:py${python_ver}-setuptools

    configure.args-append \
                    -DPython3_EXECUTABLE=${configure.python}
}

# It is advised to used paired versions:
pre-fetch {
    if {${subport} eq "${name}-devel"} {
        if {[catch {set result [active_variants libbtbb-devel ""]}]} {
            ui_msg "Error: ${name} should be paired with libbtbb. Please install a libbtbb-devel subport."
            return -code error "Version mismatch."
        }
    } elseif {${subport} eq "${name}"} {
        if {[catch {set result [active_variants libbtbb ""]}]} {
            ui_msg "Error: ${name} should be paired with libbtbb. Please install a libbtbb subport."
            return -code error "Version mismatch."
        }
    }
}
