# -*- 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
# $Id: Portfile 118976 2014-04-14 12:19:40Z michaelld@macports.org $

PortSystem          1.0
PortGroup           qmake 1.0
PortGroup           active_variants 1.1
PortGroup           github 1.0

name                gqrx
maintainers         michaelld openmaintainer

description         Gqrx is a software defined radio (SDR) receiver using GNU Radio, OSMOSDR, and Qt4.

categories          science comms
license             GPL-3 BSD
platforms           darwin macosx

set description_common {Gqrx is a software defined radio receiver for Funcube Dongle (FCD), RTL2832U-based DVB-T devices (RTL-SDR), Universal Software Radio Peripherals (USRP) and Osmo SDR devices.  Gqrx is powered by GNU Radio and the Qt GUI toolkit.  Gqrx is free and open source software and anyone is invited to hack the source code to suit their needs.}

if {${subport} ne "gqrx-devel"} {

    # patch to fix usage of real and imag to be compatible with both
    # libstdc++ and libc++ runtimes; this patch is included with the
    # devel from upstream.

    # configure.cxx_stdlib works with MacPorts SVN trunk, not release;
    # do something else until both work.

    # if {[info exists configure.cxx_stdlib] && [string match libc++ ${configure.cxx_stdlib}]}

    patchfiles-append patch-fix-real-imag.diff

}

post-patch {

    # set install location

    reinplace "s|@APPSDIR@|${qt_apps_dir}|g" ${worksrcpath}/gqrx.pro

    # set version

    reinplace "s|@VERSION@|${version}|g" ${worksrcpath}/gqrx.pro

    # set arch type(s)

    reinplace "s|@ARCHES@|${qt_arch_types}|g" ${worksrcpath}/gqrx.pro

}

configure.post_args gqrx.pro

post-destroot {

    # link the executable back to $qt_bins_dir

    ln -s ${qt_apps_dir}/Gqrx.app/Contents/MacOS/Gqrx \
        ${destroot}${qt_bins_dir}

}

if {${subport} eq ${name}} {

    long_description    ${description}  \
        This port is kept up with the latest Gqrx tagged release, is typically updated every few months, and provides compatibility with the GNU Radio release 3.7 API: the gnuradio and gnuradio-devel ports.  ${description_common}

    github.setup        csete gqrx 2.2.0 v
    revision            2

    checksums           rmd160 f8fc0f253734ad5dbe15155fe4e3de8b0c9bac72 \
                        sha256 6f3114a7476cd1deaaaa37d08669ae44bdc387ce49f24a1db6ca2cf054467c45

    conflicts           gqrx-legacy gqrx-devel

    patchfiles-append   patch-gqrx.pro.diff

}

subport gqrx-devel {

    long_description    ${description}  \
        This port is kept up with the Gqrx GIT 'master' branch, is typically updated weekly to monthly, and provides compatibility with the GNU Radio release 3.7 API: the gnuradio and gnuradio-devel ports.  ${description_common}

    github.setup        csete gqrx eca0ea7163ff16abd698737fc8ac1baab21be668
    version             2.2.1_20140411
    revision            1

    checksums           rmd160 4ab864be49882463d2787d1baa5a0cd24b28f8cf \
                        sha256 61aebf11b424378c93a196b5ba1ad7c7ca60969004a0ae4712a71b20537126a0

    # fix port name set by github PortGroup

    name                gqrx-devel

    conflicts           gqrx-legacy gqrx

    patchfiles-append   patch-gqrx.pro-devel.diff

}

subport gqrx-legacy {

    long_description    ${description}  \
        This port is for the last Gqrx commit providing compatibility with GNU Radio release 3.6 API: the gnuradio-legacy port.  ${description_common}

    github.setup        csete gqrx 90ea4cfd685dffeac21cf1d860228f6eead1e4a5

    # fix port name set by github PortGroup

    name                gqrx-legacy

    version             2.0_20130703

    checksums           rmd160 eba005dfad221997a7d80a8d8cd132cfea209667 \
                        sha256 83403f9d2c05d10a8d8df76a4282c41b9d3547ceccaebecc92c1d3f0835a6f59

    conflicts           gqrx gqrx-devel

    # gqrx-legacy works with *-legacy only

    depends_lib-append  port:gnuradio-legacy \
                        port:gr-osmosdr-legacy

    patchfiles-append   patch-gqrx.pro-legacy.diff

    # no version checking for now

    livecheck.type      none

}

# override githib PortGroup homepage setting

homepage            http://gqrx.dk/

if {${subport} ne "gqrx-legacy"} {

    # allow gqrx to work with both gnuradio and gnuradio-devel ...

    depends_lib-append  port:gr-osmosdr \
        path:lib/libgnuradio-audio.dylib:gnuradio

    # ... but not with gnuradio-legacy or gnuradio-next

    pre-fetch {
        if {![catch {set installed [lindex [registry_active gnuradio-legacy] 0]}]} {
            # gnuradio-legacy is installed; this version of gr-osmosdr does not work with gnuradio-legacy
            ui_msg "\nError: ${name} requires the gnuradio or gnuradio-devel port, and will not work with the gnuradio-legacy port.  deactivate gnuradio-legacy, and then install or activate gnuradio or gnuradio-devel.\n"
            return -code error "Invalid port dependency: gnuradio-legacy"
        }
        if {![catch {set installed [lindex [registry_active gnuradio-next] 0]}]} {
            # gnuradio-next is installed; this version of gr-osmosdr does not work with gnuradio-next
            ui_msg "\nError: ${name} requires the gnuradio or gnuradio-devel port, and will not work with the gnuradio-next port.  deactivate gnuradio-next, and then install or activate gnuradio or gnuradio-devel.\n"
            return -code error "Invalid port dependency: gnuradio-next"
        }
    }

    variant portaudio description "Use PortAudio 2.0 for the audio interface" {
        depends_lib-append port:portaudio
        configure.args-append AUDIO_BACKEND=portaudio
    }
}
