# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0

categories          science comms
platforms           darwin macosx
license             GPL-2
maintainers         {ra1nb0w @ra1nb0w} openmaintainer
description         NanoVNA-QT is a Vector Network Analyzer software for the NanoVNA V2 hardware
long_description    {*}${description}

github.setup        nanovna NanoVNA-QT 20200507
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
checksums           rmd160  c0b9eeb4f30223b5ff0542aa5a9a94a781581f3a \
                    sha256  f37ec74079842ecc8091cb353a21dfb0d813fc87c823bf38ffd0cc4fea6c3075 \
                    size    876518
revision            3
# stealth update
dist_subdir         ${name}/${version}_1

compiler.c_standard 2011
compiler.cxx_standard 2011

# use the same tree but built with
# autoconf and make
subport libxavna {
    description        C and C++ library for accessing the NanoVNA V2 hardware
    long_description   {*}${description}

    patchfiles-append \
        calibration.c.patch \
        xavna.c.patch \
        xavna.h.patch

    # remove -fno-delete-null-pointer-checks
    # because it doesn't exist on macOS < 10.10
    if { ${os.major} <= 13 } {
        patchfiles-append \
            libxavna_Makefile.am.patch
    }

    depends_lib-append \
        path:share/pkgconfig/eigen3.pc:eigen3 \
        port:fftw-3

    use_autoreconf  yes

    post-destroot {
        xinstall -m 0755 -d ${destroot}${prefix}/include/xavna
        xinstall -m 0644 {*}[glob -directory ${worksrcpath}/libxavna/include *] \
            ${destroot}${prefix}/include/xavna
    }

    livecheck.type  none
}

# build the application
if {${subport} eq ${name}} {

    PortGroup       qmake5 1.0
    PortGroup       app 1.0

    # build the project with one call
    patchfiles-append \
        nanovna.pro.patch \
        polarview.c.patch

    qt5.depends_component \
        qtcharts \
        qtsvg

    depends_lib-append \
        port:libxavna \
        path:share/pkgconfig/eigen3.pc:eigen3 \
        port:fftw-3

    # avoid conflict with autoconf files
    # and avoid renaming Makefile.am
    configure.dir ${worksrcpath}/custom
    configure.post_args nanovna.pro
    build.dir ${worksrcpath}/custom

    destroot {
        xinstall -m 0755 -d ${destroot}${prefix}/bin
        xinstall -m 0755 ${worksrcpath}/vna_qt/vna_qt.app/Contents/MacOS/vna_qt ${destroot}${prefix}/bin/NanoVNA-QT
        xinstall -m 0755 -d ${destroot}${prefix}/lib
        xinstall -m 0755 ${worksrcpath}/libxavna/xavna_mock_ui/libxavna_mock_ui.1.dylib ${destroot}${prefix}/lib

        # maybe use QMAKE_SONAME_PREFIX
        system "install_name_tool -id ${prefix}/lib/libxavna_mock_ui.1.dylib ${destroot}${prefix}/lib/libxavna_mock_ui.1.dylib"
        system "install_name_tool -change libxavna_mock_ui.1.dylib ${prefix}/lib/libxavna_mock_ui.1.dylib ${destroot}${prefix}/bin/NanoVNA-QT"
    }

    app.create yes
    app.name NanoVNA-QT
    app.executable NanoVNA-QT
    app.icon pictures/antenna.png
    app.retina yes

}
