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

# Fix builds for macos <= 10.12.
# Fix for undefined symbols for std::bad_optional_access::~bad_optional_access
# Copied from https://trac.macports.org/ticket/69638
legacysupport.newest_darwin_requires_legacy 16
legacysupport.use_mp_libcxx yes

name                ecCodes
version             2.47.0
revision            0
maintainers         {takeshi @tenomoto} \
                    {me.com:remko.scharroo @remkos} \
                    openmaintainer
license             Apache-2
categories          science
description         API and tools for decoding and encoding GRIB, BUFR and GTS formats
homepage            https://confluence.ecmwf.int/display/ECC
master_sites        https://confluence.ecmwf.int/download/attachments/45757960
distname            eccodes-${version}-Source

checksums           rmd160  1586c655fad79f6fa60f491cde5d8903d08c53a0 \
                    sha256  82da819aa9b51831dc14b3bf2918bfee50b1cd53a05088d0c3f4493758aae094 \
                    size    12664464

patchfiles          patch-pkg-config.diff

long_description \
    ecCodes is a package developed by ECMWF which provides an application programming interface and \
    a set of tools for decoding and encoding messages in the following formats: \
        \n* WMO FM-92 GRIB edition 1 and edition 2 \
        \n* WMO FM-94 BUFR edition 3 and edition 4 \
        \n* WMO GTS abbreviated header (only decoding).

compilers.setup     -clang33 -clang34 default_fortran
compilers.allow_arguments_mismatch \
                    yes

depends_build-append \
                    bin:bison:bison \
                    bin:flex:flex \
                    port:perl5
depends_lib         port:openjpeg15 \
                    port:ld64 \
                    port:libpng \
                    port:libaec \
                    port:netcdf \
                    port:zlib

# ecCodes only supports 64 bit platforms
supported_archs     arm64 ppc64 x86_64

compiler.cxx_standard \
                    2017

# error if CMAKE_BUILD_TYPE MacPorts
if {[variant_isset debug]} {
    cmake.build_type    Debug
} else {
    cmake.build_type    Release
}

configure.args-append \
                    -DENABLE_FORTRAN=OFF \
                    -DENABLE_NETCDF=ON \
                    -DENABLE_PNG=ON \
                    -DENABLE_TESTS=OFF \
                    -Dlibaec_DIR=${prefix}/lib/libaec/lib/cmake/libaec \
                    -DNETCDF_CONFIG_EXECUTABLE=${prefix}/bin/nc-config \
                    -DENABLE_JPG_LIBJASPER=OFF \
                    -DOPENJPEG_INCLUDE_DIR=${prefix}/include/openjpeg-1.5 \
                    -DOPENJPEG_LIBRARY=${prefix}/lib/libopenjpeg.dylib \
                    -DZLIB_INCLUDE_DIR=${prefix}/include \
                    -DZLIB_LIBRARY=${prefix}/lib/libz.dylib

if {[variant_isset g95]} {
    configure.args-append   -DBUILD_SHARED_LIBS=OFF
}

post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 {*}[glob ${worksrcpath}/html/*.html] \
        ${destroot}${prefix}/share/doc/${name}
}

if {[fortran_variant_isset]} {
    configure.args-replace  -DENABLE_FORTRAN=OFF \
                            -DENABLE_FORTRAN=ON
}

variant openmp description {Add support for OpenMP} {
    configure.args-append   -DENABLE_ECCODES_OMP_THREADS=ON
    configure.cflags-append -fopenmp
}
if {[variant_isset openmp]} {
    compiler.openmp_version 2.5
}

variant memfs description {Add support for MEMFS} {
    configure.args-append   -DENABLE_MEMFS=ON
}

livecheck.type  regex
livecheck.url   ${homepage}
livecheck.regex "ecCodes version (\\d+(?:\\.\\d+)*)"
