# -*- 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
# $Id: Portfile 108360 2013-07-21 02:06:46Z michaelld@macports.org $

PortSystem          1.0
PortGroup           cmake 1.0

name                uhd
categories          science comms
maintainers         michaelld openmaintainer
description         USRP Hardware Driver for Ettus Research Products
license             GPL-3+

homepage            https://ettus-apps.sourcerepo.com/redmine/ettus/projects/uhd/wiki
platforms           darwin

dist_subdir         uhd
set base_version 003_005_003

if {${subport} == ${name}} {

    long_description ${description}: \
Provides the release version, which is typically updated every month or so.

    version         ${base_version}
    distname        release_${version}
    worksrcdir      UHD-Mirror-${distname}
    master_sites    https://github.com/EttusResearch/UHD-Mirror/archive
    conflicts       uhd-devel
    checksums       sha1   0442e499e2bf582c38cc92dca478fe6934a4b8f8 \
                    rmd160 0708c1d2445b76df736d1e1ce10f663647f15fb8

    livecheck.url	http://ettus-apps.sourcerepo.com/redmine/ettus/projects/uhd/repository/changes?rev=master
    livecheck.regex >release_(\[^<\]*)</option></select>

}

subport uhd-devel {

    long_description ${description}: \
Provides the GIT master version, which is typically updated every few days to week.

    version         ${base_version}_20130719
    fetch.type      git
    git.url         https://github.com/EttusResearch/UHD-Mirror.git
    git.branch      ea418a8507a363633541aa7554a030b56a476d85
    worksrcdir      UHD-Mirror-release-${base_version}_git
    conflicts       uhd

    livecheck.url     http://ettus-apps.sourcerepo.com/redmine/ettus/projects/uhd/repository/changes?rev=master
    livecheck.regex   "cb-1. name.*value=.(\[0-9a-z\]*)"
    livecheck.version ${git.branch}

}

depends_lib-append	port:boost

# do VPATH build

post-extract        { file mkdir ${workpath}/build }
configure.dir       ${workpath}/build
build.dir           ${workpath}/build

# set last configure argument to the reletive path
# to the top-level cmake source

configure.post_args ../${worksrcdir}/host

# explicitly set configure args -D cmake flags to get MacPorts'
# curses; these are used by some variants, but they don't hurt.

configure.args-append \
    -DCURSES_CURSES_H_PATH=${prefix}/include \
    -DCURSES_CURSES_LIBRARY=${prefix}/lib/libcurses.dylib \
    -DCURSES_FORM_LIBRARY=${prefix}/lib/libform.dylib \
    -DCURSES_HAVE_CURSES_H=${prefix}/include/curses.h \
    -DCURSES_INCLUDE_PATH=${prefix}/include \
    -DCURSES_LIBRARY=${prefix}/lib/libcurses.dylib \
    -DCURSES_NCURSES_LIBRARY=${prefix}/lib/libncurses.dylib \
    -DPKG_LIB_DIR=${prefix}/share/uhd

# after destroot, have uhd fetch its images into the destroot area,
# so that they are also tracked by MacPorts

post-destroot {
    system "${destroot}${prefix}/share/uhd/utils/uhd_images_downloader.py --install-location=${destroot}${prefix}/share/uhd/images"
}

# set Python variants

set pythons_suffixes {26 27}
global chosen_python_suffix
set chosen_python_suffix ""

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [join [split ${s} ""] "."]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    eval [subst {
        variant ${p} description "Build UHD for Python ${v}" \
            conflicts ${c} {

            set chosen_python_suffix ${s}
            depends_lib-append \
                port:${p} \
                port:py${s}-cheetah

            # specify that Python version to use
            configure.args-append \
                -DPYTHON_EXECUTABLE=${prefix}/bin/python${v}
        }
    }]
}

# if no python variant is set, default to 2.7
if {![variant_isset python26] &&
    ![variant_isset python27]} {
    default_variants +python27
}

# make sure -python27 was not used alone
if {![variant_isset python26] &&
    ![variant_isset python27]} {
    return -code error "Exactly one Python variant must be selected."
}

variant test description {enable tests} {
    configure.args-append -DENABLE_TESTS=ON
    test.run yes
}

if {![variant_isset test]} {
    configure.args-append -DENABLE_TESTS=OFF
}

variant examples description {enable examples} {
    configure.args-append -DENABLE_EXAMPLES=ON
}

if {![variant_isset examples]} {
    configure.args-append -DENABLE_EXAMPLES=OFF
}

variant libusb description {enable USB support via libusb version 1.0} {
    depends_lib-append port:libusb
    configure.args-append -DENABLE_USB=ON
}

if {![variant_isset libusb]} {
    configure.args-append -DENABLE_USB=OFF
}

variant docs description {build documentation} {
    depends_lib-append port:doxygen
    configure.args-append -DENABLE_DOXYGEN=ON
}

if {![variant_isset docs]} {
    configure.args-append -DENABLE_DOXYGEN=OFF
}

variant manual description {build manual} {
    depends_lib-append \
        port:py${chosen_python_suffix}-docutils
    pre-configure {
        set v [join [split ${chosen_python_suffix} ""] "."]
        configure.args-append -DENABLE_MANUAL=ON \
            -DRST2HTML_EXECUTABLE=${prefix}/bin/rst2html-${v}.py
    }
}

if {![variant_isset manual]} {
    configure.args-append -DENABLE_MANUAL=OFF
}

variant full requires docs examples libusb manual test \
    description {Enable all variants except +debug and +universal} {}
