# -*- 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.40.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  4af005a345fc441ed9fecde9876b415aa531005c \
                    sha256  f58d5d7390fce86c62b26d76b9bc3c4d7d9a6cf2e5f8145d1d598089195e51ff \
                    size    12610604

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_AEC=ON \
                    -DAEC_PATH=${prefix}/lib/libaec \
                    -DENABLE_TESTS=OFF \
                    -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+)*)"
