# -*- 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                   mpi 1.0
PortGroup                   github 1.0
PortGroup                   cmake 1.0
PortGroup                   muniversal 1.0
PortGroup                   legacysupport 1.1
PortGroup                   compiler_blacklist_versions 1.0

# strnlen
legacysupport.newest_darwin_requires_legacy 10

epoch                       3
github.setup                Unidata netcdf-c 4.9.3 v
github.tarball_from         archive
revision                    0
name                        netcdf
maintainers                 {takeshi @tenomoto} openmaintainer
categories                  science
license                     Permissive

description                 libraries for array-oriented scientific data
long_description \
    NetCDF (network Common Data Form) is a set of software libraries \
    and machine-independent data formats that support the creation,  \
    access, and sharing of array-oriented scientific data.

homepage                    http://www.unidata.ucar.edu/software/netcdf/

checksums           rmd160  0d5048ac645b4373372d538b8b3c92588da8ea3a \
                    sha256  990f46d49525d6ab5dc4249f8684c6deeaf54de6fec63a187e9fb382cc0ffdff \
                    size    25619216

compilers.choose            cc cpp
mpi.setup

cmake.out_of_source         yes

# m4 1.4.2 which comes with Mac OS X 10.4 is too old:
# https://trac.macports.org/ticket/47545
# And m4 wasn't included in the Xcode 15.3 version of the command line tools:
# https://trac.macports.org/ticket/69467#comment:6
# https://trac.macports.org/ticket/69639#comment:1
depends_build-append        port:m4
configure.args-append       -DENABLE_NETCDF_4=OFF \
                            -DENABLE_DAP=OFF \
                            -DENABLE_CDF5=OFF \
                            -DNC_M4=${prefix}/bin/gm4 \
                            -DCMAKE_PREFIX_PATH=${worksrcpath}/cmake/modules

# https://trac.macports.org/ticket/65672
compiler.blacklist-append   *gcc-4.* {clang < 400}

test.run                    yes
test.target                 test

default_variants +netcdf4 +dap
if {(!${universal_possible} || ![variant_isset universal]) && (${build_arch} == "x86_64" || ${build_arch} == "ppc64")} {
    default_variants-append +cdf5
}

if {[variant_isset netcdf4]} {
    mpi.enforce_variant         hdf5
}

variant netcdf4 description {enable support for netcdf-4 API} {
    depends_lib-append      port:hdf5 \
                            port:blosc \
                            port:bzip2 \
                            port:zlib \
                            port:zstd
    configure.args-delete   -DENABLE_NETCDF_4=OFF
    configure.args-append   -DENABLE_NETCDF_4=ON
}

variant hdf4 description {enable support for hdf4} {
    depends_lib-append      port:hdf4 \
                            path:include/turbojpeg.h:libjpeg-turbo
    patchfiles-append       patch-liblib-CMakeLists.txt.diff
    configure.args-append   -DENABLE_HDF4=ON
}

variant dap description {enable dap} {
    depends_lib-append      port:curl \
                            port:libxml2
    configure.args-delete   -DENABLE_DAP=OFF
    configure.args-append   -DENABLE_DAP=ON \
                            -DENABLE_DAP_REMOTE_TESTS=OFF
}

variant cdf5 description {enable support for cdf5} {
    configure.args-delete   -DENABLE_CDF5=OFF
    configure.args-append   -DENABLE_CDF5=ON
}

if {${universal_possible} && [variant_isset universal]} {

    pre-configure {
        # record configure date (as cmake does) but ensure date will be the same for achitectures
        # helps ensure libnetcdf.settings is the same for all architectures
        set date [exec "/bin/date"]
        reinplace "s|@CONFIG_DATE@|${date}|g" ${worksrcpath}/libnetcdf.settings.in
    }

    merger-post-destroot {
        foreach arch ${universal_archs_to_use} {
            reinplace "s|${build.dir}-${arch}|${build.dir}|g" ${destroot}-${arch}${prefix}/lib/libnetcdf.settings
        }
    }
}

if {[mpi_variant_isset]} {
    configure.cflags-append -DHDF5_PARALLEL
    configure.args-append   -DENABLE_PARALLEL_TESTS=ON
}

github.livecheck.regex  {([^"rba]+)}
