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

name                fldigi
version             4.2.06
revision            0
# same sources but with different version
set version_flarq   4.3.9
categories          science
platforms           darwin macosx
license             GPL-3
maintainers         {ra1nb0w @ra1nb0w} openmaintainer

description         Fast Light Digital Modem Application
long_description    Fldigi (Fast Light Digital Modem Application), is a \
    cross-platform modem program that supports most of the peer-to-peer \
    (live keyboard) digital modes used on the amateur radio bands.
homepage            http://www.w1hkj.com
master_sites        http://www.w1hkj.com/files/${name}/

checksums           rmd160  9e990f4c85343f587e4bb31772a57561862058a6 \
                    sha256  4360de225d6f8cfeb9bb6a5be6ac492e52702c8f704f87609c452d3bcb1b6c08 \
                    size    5149140

depends_build-append \
    port:pkgconfig \
    port:asciidoc

depends_lib-append \
    path:lib/libfltk.dylib:fltk \
    path:lib/libhamlib.dylib:hamlib \
    port:libpng \
    port:libsamplerate \
    port:libsndfile \
    port:portaudio

configure.args-append \
    --without-pulseaudio \
    --with-asciidoc

# build fix: remove -no-stack-check on macOS < 10.10
if {${os.platform} eq "darwin" && ${os.major} <= 13} {
    patchfiles-append \
        remove_no-stack-check.patch
}

# We put this into a pre-configure block so it can be evaluated _after_ platform selection.
pre-configure {

    # enable optimization on all Intel hardwares
    if {[variant_exists universal] && [variant_isset universal]} {
        set merger_configure_args(x86_64) configure.args-append --enable-optimizations=x86-64
        set merger_configure_args(i386)   configure.args-append --enable-optimizations=i686
    } elseif {[variant_isset native]} {
        if {${configure.build_arch} eq "arm64"} {
            configure.args-append --enable-optimizations=nativeARM
        } else {
            configure.args-append --enable-optimizations=native
        }
    } else {
        switch -- ${configure.build_arch} {
            x86_64 { configure.args-append --enable-optimizations=x86-64 }
            i386   { configure.args-append --enable-optimizations=i686 }
        }
    }

    # Fix building for older macOS versions via MACOSX_DEPLOYMENT_TARGET
    # from http://www.w1hkj.com/doku/doku.php?id=howto:building_on_os_x
    if {${os.platform} eq "darwin" && [vercmp $macosx_deployment_target 10.12] < 0} {
        configure.args-append \
            --without-clock_gettime
        configure.env-append \
            MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target}
    }
}

variant bundle description {Enable the optional macOS bundle of fldigi/flarq} {
    patchfiles-append \
        patch-mkappbundle.sh.diff
    build.target    appbundle
    post-destroot {
        xinstall -d -m 0755 ${destroot}${applications_dir}
        copy ${workpath}/${name}-${version}/src/${name}-${version}/${name}-${version}.app ${destroot}${applications_dir}/${name}.app
        copy ${workpath}/${name}-${version}/src/${name}-${version}/flarq-${version_flarq}.app ${destroot}${applications_dir}/flarq.app
    }
}

variant pulseaudio description {Build fldigi with pulseaudio support} {
    configure.args-delete --without-pulseaudio
    configure.args-append --with-pulseaudio
    depends_lib-append \
        port:pulseaudio
}

variant native conflicts universal description {Enable native cpu flags optimization} {
    # check pre-configure
}

default_variants-append +bundle

livecheck.type      regex
livecheck.url       ${master_sites}
livecheck.regex     ${name}-(\[0-9.\]+)${extract.suffix}

test.run            yes
test.cmd            ./src/fldigi --version
