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

name                        cdo
version                     2.5.3
revision                    2
maintainers                 {takeshi @tenomoto} \
                            {me.com:remko.scharroo @remkos} \
                            openmaintainer
license                     GPL-2
categories                  science
description                 Climate Data Operators
homepage                    https://code.mpimet.mpg.de/projects/cdo
master_sites                https://code.mpimet.mpg.de/attachments/download/30045

# Temporarily added because of stealth update of v2.5.3
dist_subdir                 ${name}/${version}_1

checksums           rmd160  149cf2014684bc75f834265d6d05a79e3c66024c \
                    sha256  470fee8f4d2b4eddf9ec82d0adccf1f6b4821ddf34b33bfe6b7069b6b6457b40 \
                    size    13973520

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.

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.thread_local_storage yes
compilers.choose            cc cxx
mpi.setup
mpi.enforce_variant         hdf5

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

patchfiles                  patch-cdo-mach-task-10.8.diff \
                            patch-cdo-missing-strings-h.diff

configure.args              --with-netcdf=${prefix} \
                            --disable-dependency-tracking \
                            --disable-openmp \
                            --with-proj=${prefix}/lib/proj9 \
                            --with-hdf5=${prefix} \
                            --with-udunits2=${prefix} \
                            --with-fftw3 \
                            --with-curl=${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.

depends_build-append        port:python313
configure.env-append        PYTHON=${prefix}/bin/python3.13

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}
}

default_variants    +eccodes

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

variant grib2 description {obsoleted by grib_api variant} {
    default_variants    +eccodes
}

variant grib_api description {obsoleted by eccodes variant} {
    default_variants    +eccodes
}

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]*)}
