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

github.setup        nexusformat HDF5-External-Filter-Plugins 0.1.0 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            11
categories          science
license             BSD MIT
maintainers         {@eborisch eborisch} openmaintainer
description         Provides dynamically loadable (compression) filters for HDF5
long_description    {*}${description}. Filters for bitshuffle, bzip2, and lz4 are \
                    included. See also \
                    http://www.hdfgroup.org/services/filters.html for \
                    more information.

checksums \
    rmd160  6724d20763725fe59d04329d5069e2943eec99cb \
    sha256  3da58e714b94d2e1bffbf52a6b64952733874ef383c23a1ad08cd01cc69f28f9 \
    size    943586

variant optimized {
    # Bitshuffle code has sse and AVX implementations
    configure.cxxflags-append   -march=native
}

depends_lib-append  port:hdf5 \
                    port:lz4 \
                    port:bzip2

configure.args-append   -DENABLE_BZIP2_PLUGIN=ON \
                        -DENABLE_LZ4_PLUGIN=ON \
                        -DENABLE_BITSHUFFLE_PLUGIN=ON \
                        -DCMAKE_INSTALL_PLUGINDIR=${prefix}/lib/hdf5

post-patch {
    if {${os.platform} eq "darwin" && ${os.major} > 13} {
        reinplace -E "s^#define (hton|ntoh)ll.*^^" LZ4/src/H5Zlz4.c
    }
    reinplace -E "s^(set.*CMAKE_INSTALL_PLUGINDIR.*)plugins^\\1hdf5^" \
        CMakeLists.txt
    foreach x {LZ4 bitshuffle BZIP2} {
        reinplace "s/MODULE/SHARED/" ${x}/src/CMakeLists.txt
    }
}

post-destroot {
    set DDIR ${destroot}${prefix}/share/doc/${name}/
    file mkdir ${DDIR}
    copy ${worksrcpath}/resources/LICENSE.txt ${DDIR}

    foreach libn {lz4 bshuf bz2} {
        set libpath ${prefix}/lib/hdf5/libh5${libn}.dylib
        system "install_name_tool -id ${libpath} ${destroot}${libpath}"
    }
}

notes \
"To enable, use (with H5Pset_filter()):\n filter_id = 307   for bzip2\n\
filter_id = 32004 for lz4\n\
filter_id = 32008 for bitshuffle\n\n\
To enable use from Matlab, or other tools expecting the default HDF5 plugin\
path, create the required symbolic link:\n \
 mkdir -p /usr/local/hdf5/lib\n \
 ln -s ${prefix}/lib/hdf5 /usr/local/hdf5/lib/plugin"
