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

# strnlen
legacysupport.newest_darwin_requires_legacy 10

name                wgrib2
version             3.1.1
revision            7
maintainers         {takeshi @tenomoto}
license             public-domain
categories          science
description         program to read atmospheric data files in GRIB2
homepage            http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/
master_sites        ftp://ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/ \
                    http://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/
distname            ${name}.tgz.v${version}
extract.suffix
set version_g2clib  1.4.0
checksums           rmd160  86f2374fc52164c6432a9ef44db25485fa46a80f \
                    sha256  9236f6afddad76d868c2cfdf5c4227f5bdda5e85ae40c18bafb37218e49bc04a \
                    size    30034766
long_description   \
    wgrib2 is a swiss army knife for grib2 files. \
    GRIB-2 has arrived and has begun replacing the the older standard GRIB-1. \
    GRIB-2 can reduce the file size by up to 80% (NAM, JPEG2000 compression).

compilers.choose    cc fc f77 f90
compilers.setup     require_fortran -clang33 -clang34
compilers.allow_arguments_mismatch  yes

depends_extract     bin:gnutar:gnutar
depends_lib         port:jasper2 \
                    port:libpng \
                    port:netcdf \
                    port:hdf5   \
                    port:proj8  \
                    port:libaec

worksrcdir          grib2

extract.post_args   "| gnutar -xf -"
patchfiles          patch-makefile.diff  \
                    patch-makefile.gctpc.diff \
                    patch-ip2lib_d-Makefile.diff \
                    patch-g2clib-makefile.diff \
                    patch-g2clib-1.4.0-dec_jpeg2000.c.diff \
                    patch-g2clib-1.4.0-enc_jpeg2000.c.diff \
                    patch-sp_v2.0.2_d-Makefile.diff \
                    patch-wgrib2-unpk.c.diff \
                    patch-wgrib2-makefile.diff

use_configure       no
use_parallel_build  no

if {[gcc_variant_isset] || [clang_variant_isset]} {
    default_variants    +openmp
}

set ip ${worksrcpath}/ip2lib_d
set lipolate ${ip}/libip2_d.a
set g2c ${worksrcpath}/g2clib-${version_g2clib}
set lg2c ${g2c}/libgrib2c.a

configure.cppflags-prepend  -I${prefix}/lib/libaec/include -I${prefix}/libexec/jasper2/include -DJAS_VERSION_MAJOR=2
configure.ldflags-prepend   -L${prefix}/lib/proj8/lib -L${prefix}/lib/libaec/lib -L${prefix}/libexec/jasper2/lib
variant universal {}
set cflags "${configure.cflags} [get_canonical_archflags cc]"
set ldflags "${configure.ldflags} [get_canonical_archflags ld]"

pre-build {
    set ompflg {}
    if {[variant_isset openmp]} {
        set ompflg -fopenmp
    }
    set fsymbol ""
    if {[fortran_variant_isset]} {
        if {[variant_isset g95]} {
            set fsymbol -DG95
            configure.fflags-append -r8
        } else {
            set fsymbol -DGFORTRAN
            build.args-append   USE_OPENMP=1
            configure.fflags-append -fdefault-real-8 -fdefault-double-8
        }
        build.env-append    FC=${configure.fc} \
                            F90=${configure.fc} \
                            LD=${configure.fc} \
                            "FFLAGS=${configure.fflags} ${ompflg}"
    }
    build.env-append    "LDFLAGS=${ldflags} ${ompflg}" \
                        CC=${configure.cc} \
                        "CPPFLAGS=${fsymbol} ${configure.cppflags}" \
                        "CFLAGS=${cflags} ${ompflg}"
}

destroot {
    xinstall -m 644 ${g2c}/grib2.h ${destroot}${prefix}/include
    xinstall -m 644 ${lg2c} ${destroot}${prefix}/lib
    xinstall -m 755 ${worksrcpath}/${name}/${name} ${destroot}${prefix}/bin
    foreach b {gmerge smallest_4 smallest_grib2} {
        xinstall -m 755 ${worksrcpath}/aux_progs/${b} ${destroot}${prefix}/bin
    }
    if {[file exist ${lipolate}]} {
        system "ranlib ${lipolate}"
        xinstall -m 644 ${lipolate} ${destroot}${prefix}/lib
        xinstall -d -m 755 ${destroot}${prefix}/share/doc/${name}
        xinstall -m 644 ${worksrcpath}/README.ipolates ${destroot}${prefix}/share/doc/${name}
    }
# install gctpc
    xinstall -m 755 -d ${destroot}${prefix}/include/gctpc
    foreach f {cproj.h proj.h} {
        xinstall -m 644 ${worksrcpath}/gctpc/source/${f} ${destroot}${prefix}/include/gctpc
    }
    xinstall -m 755 -d ${destroot}${prefix}/lib/gctpc
    xinstall -m 644 ${worksrcpath}/lib/libgeo.a ${destroot}${prefix}/lib/gctpc/libgctpc.a
    xinstall -m 755 -d ${destroot}${prefix}/share/gctpc
    file copy ${worksrcpath}/gctpc/test ${destroot}${prefix}/share/gctpc
    xinstall -m 755 -d ${destroot}${prefix}/share/doc
    file copy ${worksrcpath}/gctpc/doc ${destroot}${prefix}/share/doc/gctpc
}

variant openmp description {Add support for OpenMP} {
    compiler.openmp_version 2.5
}

livecheck.url       http://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/
livecheck.regex     ${name}\\.tgz\\.v(\\d+(?:\\.\\d+)*)
