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

name                stimfit
version             0.17.1
checksums           rmd160  c07ad51c7dddb533aaa1edb544386c2c8875b4b5 \
                    sha256  c6a5761644d9fb6504a097b9232197a0e65cc6f19afb9162c2e20a39e2efcf84 \
                    size    20174648

categories          science
license             GPL-2
maintainers         {gmx.de:christsc @neurodroid}
description         Electrophysiology analysis suite
long_description    A program for viewing and analyzing electrophysiological data
homepage            http://www.stimfit.org
master_sites        ${homepage}
worksrcdir          Stimfit-${version}-Source

wxWidgets.use       wxWidgets-3.2

depends_build-append \
                    port:gawk

depends_lib         port:fftw-3 \
                    port:hdf5 \
                    port:${wxWidgets.port}

compiler.cxx_standard \
                    2017

configure.args      -DCMAKE_INSTALL_PREFIX=${applications_dir} \
                    -DSTF_ENABLE_PYTHON=OFF \
                    -DSTF_WITH_BIOSIG=ON \
                    -DSTF_BIOSIG_PROVIDER=SUBMODULE \
                    -DSTF_LAPACK_PROVIDER=AUTO \
                    -DSTF_BUILD_MODULE=OFF \
                    -DSTF_BUILD_TESTS=OFF \
                    -DSTF_MACOS_APP_BUNDLE=ON \
                    -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig}

configure.pre_args  -S ${worksrcpath} -B ${workpath}/build

set python_branches {3.10 3.11 3.12 3.13 3.14}

set none_selected 1
foreach python_branch ${python_branches} {
    set python_version [join [lrange [split ${python_branch} .] 0 1] ""]
    if {[variant_isset python${python_version}]} {
        set none_selected 0
        break
    }
}

if {$none_selected} {
    default_variants +python314
}

set lapack_none_selected 1
foreach lapack_variant {accelerate openblas atlas} {
    if {[variant_isset ${lapack_variant}]} {
        set lapack_none_selected 0
        break
    }
}

if {$lapack_none_selected} {
    default_variants +accelerate
}

foreach python_branch ${python_branches} {
    set python_version [join [lrange [split ${python_branch} .] 0 1] ""]

    set variant_line {variant python${python_version} description "Enable Python ${python_branch} shell"}

    foreach over ${python_branches} {
        if {${python_branch} == ${over}} {
            continue
        }

        set over_no_dot [join [lrange [split ${over} .] 0 1] ""]
        append variant_line " conflicts python${over_no_dot}"
    }
    append variant_line { {}}

    eval $variant_line
}

foreach python_branch ${python_branches} {
    set python_version [join [lrange [split ${python_branch} .] 0 1] ""]
    if {[variant_isset python${python_version}]} {
        depends_build-append \
            port:swig-python \
            port:py${python_version}-setuptools

        depends_lib-replace \
            port:${wxWidgets.port} \
            port:py${python_version}-wxpython-4.0

        depends_lib-append \
            port:python${python_version} \
            port:py${python_version}-numpy \
            port:py${python_version}-matplotlib

        configure.args-replace \
            -DSTF_ENABLE_PYTHON=OFF \
            -DSTF_ENABLE_PYTHON=ON

        configure.args-replace \
            -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig} \
            -DwxWidgets_CONFIG_EXECUTABLE=${frameworks_dir}/Python.framework/Versions/${python_branch}/bin/wx-config

        configure.args-append \
            -DPython3_EXECUTABLE=${prefix}/bin/python${python_branch}
    }
}

variant accelerate description {Use Apple Accelerate framework for BLAS/LAPACK} conflicts openblas atlas {
    configure.args-append \
        -DSTF_LAPACK_PROVIDER=ACCELERATE
}

variant openblas description {Use MacPorts OpenBLAS libraries} conflicts accelerate atlas {
    depends_lib-append \
        port:openblas

    configure.args-append \
        -DSTF_LAPACK_PROVIDER=OPENBLAS
}

variant atlas description {Use MacPorts ATLAS libraries (legacy provider)} conflicts accelerate openblas {
    depends_lib-append \
        port:atlas

    configure.args-append \
        -DSTF_LAPACK_PROVIDER=LEGACY \
        -DBLA_VENDOR=ATLAS
}

configure.ldflags-append \
                    -headerpad_max_install_names

post-destroot {
    set docdir ${destroot}${prefix}/share/doc/${name}
    xinstall -d ${docdir}
    xinstall -m 644 ${worksrcpath}/dist/macosx/copyright \
        ${worksrcpath}/COPYING ${docdir}
}
