# -*- 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 114317 2013-12-05 07:12:27Z ryandesign@macports.org $

PortSystem          1.0

name                berkeleygw
version             1.0.6
categories          science
platforms           darwin
license             BSD
maintainers         gmail.com:dstrubbe

description         GW/Bethe-Salpeter equation
long_description    BerkeleyGW is a set of computer codes that calculate the quasiparticle properties \
                    and the optical responses of a large variety of materials from bulk periodic crystals \
                    to nanostructures such as slabs, wires and molecules, using many-body perturbation theory.
homepage            http://www.berkeleygw.org
master_sites        http://www.berkeleygw.org/releases

checksums           rmd160  7d4861abdc7592a0768420df2fd9014918de8bd4 \
                    sha256  8912a5654d2b343689ec6c0ccd6b355ad336d04e46592684a1125573a6544ea7

depends_lib         port:atlas \
                    port:fftw

# fftw is not universal
universal_variant   no

distfiles           BGW-${version}.tar.gz

patchfiles          patch-testsuite-Silane-PARATEC-Silane.test.diff

configure {
    system -W ${worksrcpath} "sed 's|/opt/local|${prefix}|' < config/generic.serial.macos.mk > arch.mk"
}

build.target	    all-flavors
use_parallel_build  yes

destroot.post_args  INSTDIR=${destroot}${prefix}

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

# more options that could be added: threads, openmpi, mpich

# This is from the Fortran recipe, but with some customization
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 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 clang/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}

        build.args VERSION=-mp-${ver}
    }
}

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

    build.args COMPFLAG="-DG95" FCPP="${configure.cc} -P -E -ansi" LINK="${prefix}/bin/g95" \
        F90free="${prefix}/bin/g95 -ffree-form -ffree-line-length-huge -fno-second-underscore" \
        MOD_OPT="-fmod=" CC_COMP=${configure.cxx} C_COMP=${configure.cc} C_LINK=${configure.cxx} \
        LAPACKLIB="-L${prefix}/lib/ -lsatlas"
    # test, destroot args needed just to avoid trying to build in the test/openmp directory unnecessarily
    test.args COMPFLAG="-DG95"
    destroot.args COMPFLAG="-DG95"
    patchfiles-append patch-Common-common-rules.mk.diff
}

livecheck.type      regex
livecheck.url       ${master_sites}
livecheck.regex     BGW-(\[0-9.\]+).tar
