# -*- 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.1
PortGroup           github 1.0
PortGroup           legacysupport 1.0

# Only enable legacy support on OSX10.6 or older
legacysupport.newest_darwin_requires_legacy 10

compiler.cxx_standard 2011

github.setup        Nuand bladeRF 5a146b2a0f52e8ec17969579cee62b8ed16e429c
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
version             20211028-[string range ${github.version} 0 7]
revision            0

# no-OS is a submodule, but because we're not cloning into a GIT
# module in the first place, we can't use the git submodule
# feature. Have to download separately and install into the correct
# location to get this port to work! Which means we need a no-OS
# commit and so forth ... basically replicate what the github PG does
# but without using it since it is not designed to be used for
# multiple downloads.

set no_OS_github_author  analogdevicesinc
set no_OS_github_project no-OS

# this commit comes from the patchfiles inside the bladeRF tarball, in
# "thirdparty/analogdevicesinc/no-OS_local/patches"
set no_OS_commit    0bba46e6f6f75785a65d425ece37d0a04daf6157

set bladeRF_distname ${distname}
set bladeRF_distfile ${distfiles}
set no_OS_distname  ${no_OS_github_project}-${no_OS_commit}
set no_OS_distfile  ${no_OS_distname}${extract.suffix}

distfiles           ${bladeRF_distfile}:tagone \
                    ${no_OS_distfile}:tagtwo

master_sites        ${github.homepage}/tarball/${git.branch}:tagone \
                    https://github.com/${no_OS_github_author}/${no_OS_github_project}/tarball/${no_OS_commit}:tagtwo

checksums           ${bladeRF_distfile} \
                    rmd160  d5d021706445e8c18408fa77e1dffc567b7297e6 \
                    sha256  94a83ae0ab0a73142dc9ade22957e94a1765bf2eaad1cd248700029cf471a544 \
                    size    2266660 \
                    ${no_OS_distfile} \
                    rmd160  dbe43395fc0b22919ac7e6a5e2f355987e4dd2b7 \
                    sha256  53670c8f0a9feaa774d8c3067ba201b31c0197294bdd00a1795c72a0368aac40 \
                    size    44184797

# github portgroup defaults to extract.rename yes
extract.rename      no
post-extract {

    # roughly replicate what's going on in the github PG post-extract
    # for the main tarball extract directory name. It doesn't work
    # here because we're using tags.
    if {[file exists [glob -nocomplain ${workpath}/${github.author}-${github.project}-*]] && \
            [file isdirectory [glob -nocomplain ${workpath}/${github.author}-${github.project}-*]]} {
        ui_debug "move [glob ${workpath}/${github.author}-${github.project}-*] ${workpath}/${distname}"
        move [glob ${workpath}/${github.author}-${github.project}-*] ${workpath}/${distname}
    } else {
        # tarball is not "${github.author}-${github.project}-*"
        ui_error "\n\NError: \${worksrcpath} for 'bladeRF' does not exist after extracting distfiles. This might indicate that the author or project is different than set in the Portfile due to a rename at GitHub. Please examine the extracted directory in ${workpath} and try to correct the Portfile by either changing the author or project or adding the worksrcdir option with the correct directory name.\n"
        return -code error "Unexpected github tarball extract."
    }

    if {[file exists [glob -nocomplain ${workpath}/${no_OS_github_author}-${no_OS_github_project}-*]] && \
            [file isdirectory [glob -nocomplain ${workpath}/${no_OS_github_author}-${no_OS_github_project}-*]]} {
        ui_debug "move [glob ${workpath}/${no_OS_github_author}-${no_OS_github_project}-*] ${workpath}/${no_OS_distname}"
        move [glob ${workpath}/${no_OS_github_author}-${no_OS_github_project}-*] ${workpath}/${no_OS_distname}
    } else {
        # tarball is not "${no_OS_github_author}-${no_OS_github_project}-*"
        ui_error "\n\NError: \${worksrcpath} no 'no-OS' does not exist after extracting distfiles. This might indicate that the author or project is different than set in the Portfile due to a rename at GitHub. Please examine the extracted directory in ${workpath} and try to correct the Portfile by either changing the author or project or adding the worksrcdir option with the correct directory name.\n"
        return -code error "Unexpected github tarball extract."
    }

    # put the no-OS directory into position

    # remove the original directory
    ui_debug "delete ${worksrcpath}/thirdparty/analogdevicesinc/no-OS"
    delete ${worksrcpath}/thirdparty/analogdevicesinc/no-OS
    ui_debug "move ${workpath}/${no_OS_distname} ${worksrcpath}/thirdparty/analogdevicesinc/no-OS"
    move ${workpath}/${no_OS_distname} ${worksrcpath}/thirdparty/analogdevicesinc/no-OS

}

categories          science comms
maintainers         {michaelld @michaelld} openmaintainer
description         bladeRF is an open-source hardware and software software-defined radio (SDR) platform.
long_description    {*}${description} This port provides all the source code required to program and interact with a bladeRF platform, including firmware for the Cypress FX3 USB controller, HDL for the Altera Cyclone IV FPGA, and C code for the host side libraries, drivers, and utilities.
license             MIT LGPL-2 GPL-2+

depends_build-append \
    port:pkgconfig \

depends_lib-append \
    path:lib/pkgconfig/libusb-1.0.pc:libusb \
    port:libedit \
    port:tecla

# temporary patch to fix CMake MATCHES "Clang"

patchfiles-append   patch-clang-matches.diff

# remove top-level library and include paths, such that internal
# libraries & headers are used instead of any already-installed ones.

configure.ldflags-delete -L${prefix}/lib
configure.cppflags-delete -I${prefix}/include

configure.args-append \
    -DVERSION_INFO_OVERRIDE="MacPorts-${version}" \
    -DTREAT_WARNINGS_AS_ERRORS=OFF \
    -DENABLE_LIBTECLA=ON \
    -DENABLE_LIBEDIT=ON

# BladeRF cmake doesn't understand build type "MacPorts";
# use release unless +debug is set

if {[variant_isset debug]} {
    cmake.build_type Debug
} else {
    cmake.build_type Release
}

variant docs description "Install BladeRF documentation" {}

# Docs use Pandoc, which is presently broken on powerpc.
if {${configure.build_arch} ni [list ppc ppc64]} {
    default_variants +docs
}

if {[variant_isset docs]} {

    depends_build-append \
        path:bin/doxygen:doxygen \
        port:help2man \
        port:pandoc

    configure.args-append \ \
        -DDOXYGEN_EXECUTABLE=${prefix}/bin/doxygen \
        -DPANDOC_EXECUTABLE=${prefix}/bin/pandoc \
        -DBUILD_DOCUMENTATION=ON \
        -DBUILD_BLADERF_CLI_DOCUMENTATION=ON \
        -DBUILD_BLADERF_CLI_INTERACTIVE_HELP=ON

} else {

    configure.args-append \ \
        -DDOXYGEN_EXECUTABLE= \
        -DPANDOC_EXECUTABLE= \
        -DBUILD_DOCUMENTATION=NO \
        -DBUILD_BLADERF_CLI_DOCUMENTATION=NO \
        -DBUILD_BLADERF_CLI_INTERACTIVE_HELP=NO

}
