# -*- 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                   legacysupport 1.1
PortGroup                   mpi 1.0

name                        cdo
version                     2.6.3
revision                    0
maintainers                 {takeshi @tenomoto} \
                            {me.com:remko.scharroo @remkos} \
                            openmaintainer
license                     GPL-2
categories                  science
description                 Climate Data Operators
long_description            CDO is a collection of command line Operators \
                            to manipulate and analyse Climate and NWP model Data. \
                            Supported data formats are GRIB 1/2, netCDF 3/4, SERVICE, \
                            EXTRA and IEG. There are more than 600 operators available.

homepage                    https://code.mpimet.mpg.de/projects/cdo
master_sites                https://code.mpimet.mpg.de/attachments/download/30224

checksums                   rmd160  91a3379db2599625e19a59326a98892f4de60311 \
                            sha256  889ece29314b48cbf47ba14ab5f1779886f128767f6d22dfcc7fad1e62f2d017 \
                            size    13268491

fetch.ignore_sslcert        yes

# Since version 2.4.0, cdo requires C++20 capable compilers.
# https://code.mpimet.mpg.de/news/536
compiler.cxx_standard       2020
compiler.blacklist-append   {clang < 1800}
compiler.thread_local_storage yes
compilers.choose            cc cxx
mpi.setup
mpi.enforce_variant         hdf5

depends_lib                 port:curl \
                            port:fftw-3 \
                            port:hdf5 \
                            port:netcdf \
                            port:proj9 \
                            port:udunits2

patchfiles                  patch-cdo-mach-task-10.8.diff

configure.args              --disable-dependency-tracking \
                            --disable-openmp \
                            --with-curl=${prefix} \
                            --with-fftw3 \
                            --with-hdf5=${prefix} \
                            --with-netcdf=${prefix} \
                            --with-proj=${prefix}/lib/proj9 \
                            --with-udunits2=${prefix} \
                            --with-zlib=${prefix}
configure.cppflags-append   -I${prefix}/include/udunits2
configure.ldflags-append    -lhdf5

# If cpp is not forced here, configure could select /usr/bin/cpp, which may be either incompatible
# with the selected clang c-compiler or may be too old, as was found on MacOS 10.14.
# https://trac.macports.org/ticket/72869

configure.args-append        CPP="\${CC} -E"

# Since cdo 2.2.0, running tests requires the Python 3 interpreter. But since the configure always looks
# for a recent Python and does not distinguish whether test is requested, it is added here as a build dependency.
# To make sure that it works prior to Catalina, it is best to simply require the latest python3 package.

set py_ver                  3.14
set py_ver_nodot            [string map {. {}} ${py_ver}]
depends_build-append        port:python${py_ver_nodot}
configure.env-append        PYTHON=${prefix}/bin/python${py_ver}

test.run                    yes
test.args                   -j1
test.target                 check

# Setting configure.cc h5pcc has been removed because it causes error
# because -Wl,-headerpad_max_install_names does not work with -pthread.
# See hdfeos5 Portfile to set h5pcc.

post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 {*}[glob ${worksrcpath}/doc/*.pdf] ${destroot}${prefix}/share/doc/${name}
    foreach f {AUTHORS  ChangeLog NEWS OPERATORS README} {
        xinstall -m 644 {*}[glob ${worksrcpath}/${f}] ${destroot}${prefix}/share/doc/${name}
    }
    xinstall -m 755 -d ${destroot}${prefix}/share/${name}
    file copy ${worksrcpath}/contrib ${destroot}${prefix}/share/${name}
}

# ecCodes is broken on 32-bit platforms.
if {${configure.build_arch} ni [list arm i386 ppc]} {
    default_variants-append +eccodes
}

if {[clang_variant_isset]} {
    configure.args-delete   --disable-openmp
    configure.args-append   --enable-openmp
}

variant eccodes description {enable grib2 support} {
    depends_lib-append      port:ecCodes
    configure.args-append   --with-eccodes=${prefix} \
                            --disable-cgribex
    configure.ldflags-append    -lpng -lopenjpeg
}

variant szip description {compile with szip support} {
    depends_lib-append      port:szip
    configure.args-append   --with-szlib=${prefix}
}

variant magicspp description {compile with ECMWF magics support} {
    depends_lib-append      port:libxml2 \
                            port:magicspp
    configure.args-append   --with-libxml2=${prefix} \
                            --with-magics=${prefix}
}

variant cdi description {build, install and link to a CDI library} {
    configure.args-append   --enable-cdi-lib
}

livecheck.type              regex
livecheck.url               ${homepage}
livecheck.regex             {Version ([0-9]+\.[0-9]+\.[0-9]+\.*[0-9]*)}
