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

name                nds2-client-swig
version             0.16.12
revision            0

categories          science
platforms           darwin
license             GPL-2
maintainers         nomaintainer

description         Network Data Server Client
long_description    Client tool for accessing streamed LIGO data using the Network \
                    Data Server version 2.

homepage            https://wiki.ligo.org/Computing/NDSClient
master_sites        https://software.igwn.org/lscsoft/source/
use_bzip2           yes

checksums           rmd160  bc8be8b5a8920bc02167e26a4a5e0a7046f74ada \
                    sha256  d356296f4a137d5e95401b1279e8dbce8b4b7dc8d87d319b09d1a57331caca06 \
                    size    160575

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:nds2-client \
                    port:swig3

regsub -all -- "-swig$" ${name} {} basename

configure.args-append \
                    -DPYTHON=false \
                    -DPYTHON_EXECUTABLE=false \
                    -DSWIG_EXECUTABLE:FILEPATH=${prefix}/bin/swig3 \
                    -DENABLE_SWIG_JAVA=no \
                    -DENABLE_SWIG_MATLAB=no \
                    -DENABLE_SWIG_OCTAVE=no \
                    -DENABLE_SWIG_PYTHON2=no \
                    -DENABLE_SWIG_PYTHON3=no \
                    -DCMAKE_INSTALL_SYSCONFDIR=${prefix}/etc \
                    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
                    -DCMAKE_EXPORT_COMPILE_COMMANDS=1

test.run            yes
test.cmd            ${prefix}/bin/ctest
test.target         -R '.*'

livecheck.type      regex
livecheck.url       ${master_sites}
livecheck.regex     {nds2-client-swig-(\d+(?:\.\d+)*).tar.bz2}

#========================================================================
# Create subports for Java
#========================================================================

subport ${basename}-java {
    PortGroup               java 1.0

    revision                0

    categories-append       java
    description             Java bindings for ${description}
    long_description        ${long_description} This package provides Java \
                            bindings, modules, and scripts.

    java.version            1.7+
    # Set fallback to an LTSava version
    java.fallback           openjdk8

    depends_build-append    port:swig3-java

    configure.javac         /usr/bin/javac -source 1.7 -target 1.7
    configure.args-replace  -DENABLE_SWIG_JAVA=no -DENABLE_SWIG_JAVA=yes

    destroot.target         install
    destroot.args-append    -C ${worksrcpath}/../build/java

    livecheck.type          none
}

#========================================================================
# Create subports for MATLAB
#========================================================================

subport ${basename}-matlab {
    PortGroup               java 1.0

    revision                0

    categories-append       matlab
    description             MATLAB bindings for ${description}
    long_description        ${long_description}. This package provides MATLAB \
                            bindings, modules, and scripts.

    java.version            1.7+
    # Set fallback to an LTS Java version
    java.fallback           openjdk8

    depends_build-append    port:swig3-java

    configure.javac         /usr/bin/javac -source 1.7 -target 1.7
    configure.args-replace  -DENABLE_SWIG_JAVA=no -DENABLE_SWIG_JAVA=yes

    depends_lib-append      port:${basename}-java

    destroot.target         install

    configure.args-replace  -DENABLE_SWIG_MATLAB=no -DENABLE_SWIG_MATLAB=yes

    destroot.args-append    -C ${worksrcpath}/../build/matlab

    livecheck.type          none
}

#========================================================================
# Create subports for Octave
#========================================================================

subport ${basename}-octave {
    revision                0
    categories-append       octave
    description             Octave bindings for ${description}
    long_description        ${long_description}. This package provides Octave \
                            bindings, modules, and scripts.

    depends_build-append    port:swig3-octave

    depends_lib-append      path:bin/octave:octave

    destroot.target         install
    destroot.args-append    pkgoctexecdir="${prefix}/share/octave/site/m"

    # At least as of Octave 3.2.4, the Octave C++ API does not work with clang.
    # compiler.blacklist-append *clang*
    configure.args-replace  -DENABLE_SWIG_OCTAVE=no -DENABLE_SWIG_OCTAVE=yes

    destroot.args-append    -C ${worksrcpath}/../build/octave

    livecheck.type          none
}

#========================================================================
# Create subports for each supported Python version
#========================================================================
foreach v {313 314} {
    set python.version      ${v}
    set python.branch       [string index ${v} 0].[string range ${v} 1 99]
    set python.bin          ${prefix}/bin/python${python.branch}
    set python.prefix       ${frameworks_dir}/Python.framework/Versions/${python.branch}
    set python.site_packages \
                            "${python.prefix}/lib/python${python.branch}/site-packages"
    set python.pkgname      ""

    subport py${v}-${basename} {
        revision            0
        categories-append   python
        description         Python ${python.version} bindings for ${description}
        long_description    ${long_description}. This package provides Python \
                            ${python.version} bindings, modules, and scripts.

        depends_build-append \
                            port:swig3-python \
                            port:py${python.version}-setuptools

        depends_lib-append  port:python${python.version} \
                            port:py${python.version}-numpy

        configure.python    ${python.bin}
        configure.args-replace  \
                            -DPYTHON=false -DPYTHON=${python.bin} \
                            -DPYTHON_EXECUTABLE=false -DPYTHON_EXECUTABLE=${python.bin} \
                            -DENABLE_SWIG_PYTHON3=no -DENABLE_SWIG_PYTHON3=yes

        configure.args-append \
                            -DPYTHON3_VERSION=${python.branch} \
                            -DPYTHON${python.version}_MODULE_INSTALL_DIR="${python.site_packages}" \
                            -DPYTHON${python.version}_EXTMODULE_INSTALL_DIR="${python.site_packages}" \
                            -DSWIG_CPPFLAGS="-I${python.prefix}/include"

        destroot.target     install
        destroot.args-append \
                            -C ${worksrcpath}/../build/python

    livecheck.type          none
  }
}
