# -*- 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 111806 2013-10-02 16:13:35Z ryandesign@macports.org $

PortSystem          1.0
PortGroup           active_variants 1.1

name                etsf_io
version             1.0.4
categories          science
platforms           darwin
license             LGPL-2.1
maintainers         gmail.com:dstrubbe
description         A library of F90 routines to read/write the ETSF_IO file format.
long_description    A library of F90 routines to read/write the common ETSF_IO file format developed by \
                    the European Theoretical Spectroscopy Facility (ETSF) for electronic-structure codes.
homepage            http://www.etsf.eu/resources/software/libraries_and_tools
master_sites        http://www.etsf.eu/system/files \
                    https://launchpad.net/etsf-io/+milestone/${version}

checksums           rmd160  25d311b2952714639f315ae26dd1c25c84b9c0f0 \
                    sha1    8b7c7af083946e03f76b68af69c046d3b127494a

depends_lib         port:netcdf-fortran

# https://trac.macports.org/ticket/39319
require_active_variants netcdf-fortran {} universal

configure.optflags  -O3

configure.args      --with-netcdf-incs=-I${prefix}/include \
                    --with-netcdf-libs="-L${prefix}/lib -lnetcdf -lnetcdff"

use_parallel_build  yes

# it is not clear how to set the netcdf-module-path appropriately for +universal
# it could be ${prefix}/mods32/include or ${prefix}/mods64/include instead of above
universal_variant   no

test.run            yes
test.cmd            make
test.target         check

# Fortran recipe
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              gfortran-mp-${ver}
        require_active_variants   netcdf-fortran gcc${ver_no_dot}
    }
}

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
    require_active_variants   netcdf-fortran g95
    # Warning: a few tests will fail with g95.
}

livecheck.url       ${homepage}
livecheck.type      regex
livecheck.regex     ${name}-(\[0-9.\]+)${extract.suffix}
