# -*- 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     compiler_blacklist_versions 1.0
PortGroup     cmake 1.0

name          @PROJECT_NAME@
version       @PROJECT_VERSION@
categories    devel
license       GPL-2+
platforms     darwin
maintainers   {@emaros ligo.org:ed.maros} openmainer

description      @PROJECT_DESCRIPTION@
long_description @PROJECT_DESCRIPTION_LONG@

homepage      @PACKAGE_URL@
master_sites  @LDAS_TOOLS_SOURCE_URL@
distname      @PROJECT_NAME@-${version}

checksums \
    rmd160 @RMD160@ \
    sha256 @SHA256@ \
    size   @SIZE@

depends_build \
    port:cmake \
    port:pkgconfig \
    port:swig \
    port:ldas-tools-cmake \
    port:ldas-tools-al \


cmake.out_of_source yes

configure.args -DENABLE_SWIG_PYTHON2=no \
               -DENABLE_SWIG_PYTHON3=no

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

if {${configure.cxx_stdlib} eq "libstdc++" } {
    configure.args-append --disable-cxx11
}

# requires clang from Xcode5 or higher to build
compiler.blacklist-append {clang < 500.2.75} llvm-gcc-4.2 gcc-4.2

use_parallel_build yes

pre-fetch {
    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        ui_error "${name} only runs on Mac OS X 10.7 or greater."
        return -code error "incompatible Mac OS X version"
    }
}

livecheck.type   regex
livecheck.url    ${master_sites}
livecheck.regex  {${tarname}-(\d+(?:\.\d+)*).tar.gz}

#------------------------------------------------------------------------

set python.versions {27 35 36 37 38}
set python.default_version 35
set default_python_ py-${basename}

configure.args-append -DCMAKE_INSTALL_DOCDIR=${prefix}/share/doc/${subport}

#========================================================================
# Create subports for each supported Python version
#========================================================================
foreach v ${python.versions} {
    set python.version       ${v}
    set python.branch        [string range ${python.version} 0 end-1].[string index ${python.version} end]
    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       LDAStools

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

        depends_build-append  port:swig-python
        depends_lib-append    port:python${python.version} \
                              port:ldas-tools-al

        configure.python          ${python.bin}
        switch -glob -- ${v} {
            "2*" { configure.args-replace    -DENABLE_SWIG_PYTHON2=no "-DENABLE_SWIG_PYTHON2=yes -DPYTHON2_EXECUTABLE=${python.bin}" }
            "3*" -
            default { configure.args-replace    -DENABLE_SWIG_PYTHON3=no "-DENABLE_SWIG_PYTHON3=yes -DPYTHON3_EXECUTABLE=${python.bin}" }
        }

        configure.args-append \
            -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.args-append      -C ${worksrcpath}/../build/python

        post-destroot {
            if {${subport} eq "py27-${basename}"} {
                foreach script [glob -tails -nocomplain -directory ${destroot}${python.prefix}/bin *] {
                    file link -symbolic ${destroot}${prefix}/bin/${script} ../Library/Frameworks/Python.framework/Versions/${python.version}/bin/${script}
                }
            }
        }
    }
}
