# -*- 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 111704 2013-09-29 01:21:03Z takeshi@macports.org $

PortSystem          1.0

name                emos
version             000392
revision            1
platforms           darwin
maintainers         takeshi
license             LGPL
categories          science
description         Interpolation library
homepage            https://software.ecmwf.int/emoslib
master_sites        https://software.ecmwf.int/wiki/download/attachments/3473472
distname            ${name}_${version}
checksums           md5     165b9d7918fd67fd849c59287fc54bb6 \
                    sha1    a8eecd6fa48a38e503373e6735e5bac861a10405 \
                    rmd160  270308f45c8d1d5b47da7018cba98a96d00f8139
long_description \
    The Interpolation library (EMOSLIB) includes Interpolation software \
    and GRIB, BUFR, CREX encoding/decoding routines.

depends_build       port:grib_api

post-patch {
    reinplace "s:^void \\*memcpy://void *memcpy:" ${worksrcpath}/gribex/gdecode.h
    reinplace "s: | O_LARGEFILE::" ${worksrcpath}/interpolation/sharedlib.c
    reinplace "s:`cat .list/bufrtables`:bufrtables/*.TXT:" ${worksrcpath}/install
    reinplace "s:xf:xpf:" ${worksrcpath}/install
}

if {${os.endian}=="little"} {
    set fendian -DLITTLE_ENDIAN
} else {
    set fendian ""
}
configure {
    set f [open ${worksrcpath}/config/config.site w]
    puts $f "#
AR      = ar
ARFLAGS = rv
CC      = ${configure.cc}
CFLAGS  = ${configure.cflags} ${fendian} ${configure.cc_archflags}
FCASTFLAGS = \$(CFLAGS)
FC      = ${configure.fc}
FFLAGS  = ${configure.fflags} ${fendian}
VECTFFLAGS =\$(FFLAGS)
RANLIB  = /usr/bin/ranlib"
    close $f
    file copy -force ${worksrcpath}/Makefile.in ${worksrcpath}/Makefile
    reinplace "s:reals:R64:" ${worksrcpath}/Makefile
    reinplace "s:glue:grib_api_merging:" ${worksrcpath}/Makefile
    foreach d {gribex pbio bufrdc bufrtables crexdc interpolation fft grib_api_merging} {
        file copy -force ${worksrcpath}/${d}/Makefile.in ${worksrcpath}/${d}/Makefile
        reinplace "s:reals:R64:" ${worksrcpath}/${d}/Makefile
        reinplace "s:arch:linux:" ${worksrcpath}/${d}/Makefile
        reinplace "s:depl:${name}:" ${worksrcpath}/${d}/Makefile
        reinplace "s:\$(ARCH)\$(CNAME)\$(R64)\$(A64):site:" ${worksrcpath}/${d}/Makefile
    }
    reinplace "s:I\$(GRIB_API_PATH):I${prefix}:" ${worksrcpath}/grib_api_merging/Makefile
    reinplace "s:`cat -s .r64`:R64:" ${worksrcpath}/install
    reinplace "s:`cat -s .emos`:${destroot}${prefix}/lib/${name}:" ${worksrcpath}/install
}

post-build {
    reinplace "s:R64 = R64:R64 =:" ${worksrcpath}/Makefile
    foreach d {gribex pbio bufrdc bufrtables crexdc interpolation fft} {
        reinplace "s:R64 = R64:R64 =:" ${worksrcpath}/${d}/Makefile
    }
    if {[variant_isset gcc43] || [variant_isset gcc44] || [variant_isset gcc45]} {
        reinplace "s:-fdefault-real-8 -fdefault-double-8 ::" ${worksrcpath}/config/config.site
    } else {
        reinplace "s:-r8 ::" ${worksrcpath}/config/config.site
    }
    system "cd ${worksrcpath}; make clean; make"
}

destroot {
    xinstall -m 755 -d ${destroot}${prefix}/lib/${name}
    xinstall -m 755 -d ${destroot}${prefix}/lib/${name}/bin
    system "cd ${worksrcpath}; ./install"
    xinstall -m 644 ${worksrcpath}/lib${name}.a ${destroot}${prefix}/lib/${name}
    foreach f {bufr2txt_tables bufr_split_tables txt2bufr_tables} {
      xinstall -m 755 ${worksrcpath}/bufrtables/${f} ${destroot}${prefix}/lib/${name}/bin
    }
    system "ln -s ${name}/lib${name}R64.a ${destroot}${prefix}/lib/lib${name}R64.a"
    system "ln -s ${name}/lib${name}.a ${destroot}${prefix}/lib/lib${name}.a"
    system "cd ${destroot}${prefix}/lib/${name}; \
        for d in bufrtables crextables gribtables gribtemplates land_sea_mask; do \
            chmod 755 \$d; \
            find \$d -type f -print | xargs chmod 644; \
        done; \
        find gribtables -type d -print | xargs chmod 755"
}

use_parallel_build  no
universal_variant   no

set gcc_versions {4.3 4.4 4.5 4.6 4.7 4.8 4.9}
set default_fortran_variant +gcc48
set g95_conflicts {}

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

    set variant_line {variant gcc${ver_no_dot} description "build 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}"

    if {[variant_isset gcc${ver_no_dot}]} {
        if {${default_fortran_variant} != "+gcc${ver_no_dot}"} {
            set default_fortran_variant ""
        }
    }
}

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

if {[variant_isset g95]} {
    if {${default_fortran_variant} != "+g95"} {
        set default_fortran_variant ""
    }
}

if {${default_fortran_variant} != ""} {
    default_variants-append "${default_fortran_variant}"
}

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.fc  ${prefix}/bin/gfortran-mp-${ver}
        configure.f77 ${prefix}/bin/gfortran-mp-${ver}
        configure.f90 ${prefix}/bin/gfortran-mp-${ver}
        configure.fflags-append -fdefault-real-8 -fdefault-double-8 -fcray-pointer -fno-second-underscore -DPOINTER_64 -DINTEGER_IS_INT -Dlinux -Dgfortran
        configure.cflags-append -DPOINTER_64 -DINTEGER_IS_INT
    }
}

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

    configure.fc ${prefix}/bin/g95
    configure.f77 ${prefix}/bin/g95
    configure.f90 ${prefix}/bin/g95
    configure.fflags-append -r8 -fsloppy-char -fno-second-underscore -DUSE_NO_POINTERS -DINTEGER_IS_INT -Dlinux
    configure.cflags-append -DPOINTER_64 -DINTEGER_IS_INT
}

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     {emos_([0-9][0-9][0-9][0-9][0-9][0-9])}
