# -*- 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
# $Id: Portfile 115019 2013-12-22 09:29:46Z ryandesign@macports.org $

PortSystem          1.0

name                grib_api
version             1.11.0
revision            1
platforms           darwin
maintainers         takeshi
license             Apache-2
categories          science
description         GRIB decoder
homepage            https://software.ecmwf.int/wiki/display/GRIB/Home
master_sites        https://software.ecmwf.int/wiki/download/attachments/3473437
checksums           md5     fe1dbfa09d970054ebb75e6616f5fd69 \
                    sha1    bf7238fb5a5ce289b2ca20da72de245e8842979f \
                    rmd160  4ec71f0122e82674b7b1953078bc9dc18c394c21
long_description \
    The ECMWF GRIB API is an application program interface accessible \
    from C and FORTRAN programs developed for encoding and decoding   \
    WMO FM-92 GRIB edition 1 and edition 2 messages. A useful set of  \
    command line tools is also provided to give quick access to grib messages.

depends_lib         port:jasper \
                    port:openjpeg15 \
                    port:libpng

fetch.ignore_sslcert    yes

post-patch {
    if {${os.major} == 8} {
        reinplace "s| -Wl,-rpath \$fortranlibdir||" ${worksrcpath}/configure
    }
    reinplace "s|share\/samples|share/$name/samples|" ${worksrcpath}/configure
    reinplace "s|share\/samples|share/$name/samples|" ${worksrcpath}/configure
    reinplace "s|share\/definitions|share/$name/definitions|" ${worksrcpath}/configure
# avoid installation to non-existent directory 'none'
    reinplace "s|'none'|''|" ${worksrcpath}/configure
# avoid auto detection of g95 or gfortran that may prevent generation of shared library
    reinplace "s|g95|''|" ${worksrcpath}/configure
    reinplace "s|gfortran|''|" ${worksrcpath}/configure
# avoid duplicated symbol error
    reinplace "s|-fno-common||" ${worksrcpath}/configure
}

configure.args      --disable-dependency-tracking \
                    --disable-fortran \
                    --with-ifs-samples=${prefix}/share/${name}/ifs_samples \
                    --with-jasper=${prefix} \
                    --with-openjpeg=${prefix} \
                    --with-png-support=${prefix} \
                    --disable-python

test.run            yes
test.target         check

post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    eval xinstall -m 644 [glob ${worksrcpath}/html/*.html] \
        ${destroot}${prefix}/share/doc/${name}
    foreach b {parser points} {
        file rename ${destroot}${prefix}/bin/${b} ${destroot}${prefix}/bin/grib_${b}
    }
}

variant emos description {deprecated to remove dependency to emos} {
}
 
set gcc_versions {4.3 4.4 4.5 4.6 4.7 4.8 4.9}
set g95_conflicts {}

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    set variant_line {variant gcc${ver_no_dot} description "build Fortran interface with gfortran from gcc${ver_no_dot}" conflicts g95}

    foreach over ${gcc_versions} {
        if {${ver} == ${over}} {
            continue
        }

        set over_no_dot [join [split ${over} "."] ""]
        append variant_line " conflicts gcc${over_no_dot}"
    }
    append variant_line { {}}

    eval $variant_line

    append g95_conflicts " conflicts gcc${ver_no_dot}"
}

eval [concat {variant g95 description {build Fortran interface with g95}} $g95_conflicts {{}}]

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    if {[variant_isset gcc${ver_no_dot}]} {
        depends_lib-append path:lib/libgcc/libgcc_s.1.dylib:libgcc
        depends_build-append port:gcc${ver_no_dot}

        configure.args-delete   --disable-fortran
        configure.fc  ${prefix}/bin/gfortran-mp-${ver}
        configure.f77 ${prefix}/bin/gfortran-mp-${ver}
        configure.f90 ${prefix}/bin/gfortran-mp-${ver}
        configure.args-append   --with-fortranlibdir=${prefix}/lib/gcc${ver_no_dot} \
                                --with-fortranlibs=-lgfortran
    }
}

if {[variant_isset g95]} {
    depends_lib-append path:lib/libgcc/libgcc_s.1.dylib:libgcc
    depends_build-append port:g95

    configure.args-delete   --disable-fortran
    configure.fc ${prefix}/bin/g95
    configure.f77 ${prefix}/bin/g95
    configure.f90 ${prefix}/bin/g95
    configure.args-append   --with-fortranlibdir=${prefix}/lib \
                            --with-fortranlibs=-lf95
}

variant python25 conflicts python26 python27 description {Add support for python25} {
    depends_lib-append      port:py25-numpy
    configure.args-delete   --disable-python
    configure.args-append   --enable-python
    configure.env-append    PYTHON=${prefix}/bin/python2.5 \
                            PYTHON_CONFIG=${prefix}/bin/python2.5-config
}

variant python26 conflicts python25 python27 description {Add support for python26} {
    depends_lib-append      port:py26-numpy
    configure.args-delete   --disable-python
    configure.args-append   --enable-python
    configure.env-append    PYTHON=${prefix}/bin/python2.6 \
                            PYTHON_CONFIG=${prefix}/bin/python2.6-config
}

variant python27 conflicts python25 python26 description {Add support for python27} {
    depends_lib-append      port:py27-numpy
    configure.args-delete   --disable-python
    configure.args-append   --enable-python
    configure.env-append    PYTHON=${prefix}/bin/python2.7 \
                            PYTHON_CONFIG=${prefix}/bin/python2.7-config
}
