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

name                    apbs

categories              science
license                 BSD
maintainers             nomaintainer

description             Adaptive Poisson-Boltzmann Solver
long_description        APBS is a software package for the numerical \
                        solution of the Poisson-Boltzmann equation, \
                        a popular continuum model for describing \
                        electrostatic interactions between molecular \
                        solutes over a wide range of length scales.

version                 3.4.1
homepage                https://www.poissonboltzmann.org
revision                0

set FETK_version        1.9.3

github.setup            Electrostatics    apbs    ${version} v
github.tarball_from     releases
extract.mkdir           no
master_sites            https://github.com/Electrostatics/apbs/archive/refs/tags/v${version}:apbs \
                        https://github.com/Electrostatics/FETK/archive/refs/tags/${FETK_version}:FETK

distfiles               apbs-${version}.tar.gz:apbs FETK-${FETK_version}.tar.gz:FETK
checksums               apbs-${version}.tar.gz \
                        rmd160  17de250c3887ff68f89a873c5a0be19f6ab38813 \
                        sha256  644e6246fd37c9dd4172fc7be1763337082ab8584dbcd53e738c831b62c89bb2 \
                        size    85806314 \
                        FETK-${FETK_version}.tar.gz \
                        rmd160  25cf57754a0b817e76e19b8de93617e554302661 \
                        sha256  2ce7ab04cba4403f4208c3ecf1c81a0a18aae6a77d22da0ffa5f64c2da7c6f28 \
                        size    27757348

worksrcdir              ${workpath}/apbs-${version}

# Test support requires python3 but the
# building the apbs python module with swig
# is unnecessary for apbs's use in pymol.
platform darwin {
    if {${os.major} < 19} {
        depends_test port:python312
        set python ${prefix}/bin/python3.12
    } else {
        set python /usr/bin/python3
    }
}

compiler.cxx_standard   2017

depends_lib             port:maloc \
                        port:superlu \
                        port:SuiteSparse

configure.optflags      -O3 -ffast-math
configure.cppflags-append -I${worksrcpath}/include -Wno-error=implicit-int

if {[string match *clang* ${configure.compiler}]} {
    configure.cppflags-append \
                        -Wno-error=incompatible-function-pointer-types
} elseif {[string match *gcc* ${configure.compiler}]} {
    configure.cppflags-append \
                        -Wno-error=incompatible-pointer-types
}

# Follow Debian's approach of not building those
# features like BEM, GEOFLOW or PBAM that only
# exist in apbs's github without tagged releases yet.
configure.args-append   -DAPBS_STATIC_BUILD=ON \
                        -DBUILD_SHARED_LIBS=OFF \
                        -DBUILD_TOOLS=ON \
                        -DENABLE_OPENMP:BOOL=OFF \
                        -DENABLE_BEM=OFF \
                        -DENABLE_GEOFLOW=OFF \
                        -DENABLE_PBAM=OFF \
                        -DBUILD_MALOC=OFF \
                        -DBUILD_SUPERLU=OFF \
                        -DBUILD_SHARED_LIBS=ON \
                        -DFETCHCONTENT_SOURCE_DIR_FETK=${worksrcpath}/FETK-${FETK_version} \
                        -DSUPERLU_INCLUDES=${prefix}/include -DSUPERLU_LIBRARIES=${prefix}/lib

patchfiles              cmake-maloc.patch disable_python_cmake_check.patch patch-testfiles.patch open-U-obsolete.patch python3.patch wrong-path-for-interpreter.patch
test.run  yes
test {
    ln ${worksrcpath}/tools/manip/inputgen.py ${worksrcpath}/tests/
    ln ${worksrcpath}/tools/manip/psize.py    ${worksrcpath}/tests/

    platform darwin {
        system -W ${worksrcpath}/tests "PATH=${destroot}${prefix}/bin:/usr/bin:/bin:${prefix}/bin ${python} apbs_tester.py -c test_cases.cfg || exit 0"
    }
    system -W ${worksrcpath}/tests "cat test.log"
    system -W ${worksrcpath}/tests "if grep FAILED test.log; then echo \"FAIL\"; exit 1; fi"
}

post-extract {
    move ${worksrcdir}/../FETK-${FETK_version} ${worksrcdir}
}

post-patch {
    reinplace "s|EXACT REQUIRED|REQUIRED|" ${worksrcpath}/CMakeLists.txt ${worksrcpath}/tests/CMakeLists.txt ${worksrcpath}/tools/python/CMakeLists.txt
    platform darwin {
        if {${os.major} < 19} {
            reinplace "s|env python3|env ${prefix}/bin/python3.12|" ${worksrcpath}/tools/manip/psize.py
            reinplace "s|/usr/bin/python3|${prefix}/bin/python3.12|" ${worksrcpath}/tools/python/vgrid/mergedx.py
        }
    }
}

post-destroot {
    set tools ${destroot}${prefix}/share/${subport}/tools
    
    move ${tools}/manip/psize.py ${destroot}${prefix}/bin/apbs-psize.py
    file attributes ${destroot}${prefix}/bin/apbs-psize.py \
        -permissions 0755

    foreach {bin} [glob -tails -dir ${tools}/bin *] {
        move ${tools}/bin/${bin} ${destroot}${prefix}/bin/apbs-${bin}
        file attributes ${destroot}${prefix}/bin/apbs-${bin} \
            -permissions 0755
    }
}

github.livecheck.regex  {([0-9.]+)}
