--- Portfile_0	2014-10-26 21:47:53.000000000 +0100
+++ Portfile_1	2014-10-27 01:34:05.000000000 +0100
@@ -3,9 +3,11 @@
 
 PortSystem          1.0
 PortGroup           active_variants 1.1
+PortGroup           mpi 1.0
 
 name                abinit
 version             7.8.2
+revision            1
 categories          science
 platforms           darwin
 license             GPL-3
@@ -39,99 +41,95 @@
 configure.args      --with-linalg-flavor="atlas" --enable-gw-dpc
 configure.optflags  -O3
 
-# This is a temporary measure for debugging configure failure on Mountain Lion buildslave.
-
+#This is a temporary measure for debugging configure failure on Mountain Lion buildslave.
 #checking for gcc... mpicc-mpich-mp
 #checking whether the C compiler works... no
-
-configure.cmd       ./configure || true
-post-configure {
-    system -W ${worksrcpath} "cat config.log"
-}
+#configure.cmd       ./configure || true
+#post-configure {
+#    system -W ${worksrcpath} "cat config.log"
+#}
 
 build.cmd           make
 use_parallel_build  yes
-
 # We do not need to use ABINIT's own approach to parallel builds, which only is relevant if
 # the "fallbacks" are used, namely if we asked for etsf_io, libxc, wannier90, etc. support
 # but did not provide the libraries. That situation should never happen for this port.
 #build.target        multi multi_nprocs=4
 
 default_variants +etsf_io +libxc +wannier90
-if { ![variant_isset gcc46] && ![variant_isset gcc47] && ![variant_isset gcc48] } {
-    if { ![variant_isset openmpi] } {
-        default_variants    +mpich
-    } elseif { ![variant_isset mpich] } {
-        default_variants    +openmpi
-    } else {
-        default_variants    +gcc48
-    }
-}
 
-# FIXME: use compiler and MPI port groups
-
-pre-fetch {
-    set fortran unknown
-    set fortrans { gcc42 gcc43 gcc44 gcc45 gcc46 gcc47 gcc48 gcc49 g95 }
-
-    foreach fc_name ${fortrans} {
-        if { [variant_isset ${fc_name}] } {
-            set fortran ${fc_name}
-        } elseif { [variant_isset openmpi] } {
-            if { [_portnameactive openmpi-default] } {
-                if { [active_variants openmpi-default ${fc_name}] } { set fortran ${fc_name} }
-            } elseif { [_portnameactive openmpi-devel-default] } {
-                if { [active_variants openmpi-devel-default ${fc_name}] } { set fortran ${fc_name} }
-            }
-        } elseif { [variant_isset mpich] } {
-            if { [_portnameactive mpich-default] } {
-                if { [active_variants mpich-default ${fc_name}] } { set fortran ${fc_name} }
-            } elseif { [_portnameactive mpich-devel-default] } {
-                if { [active_variants mpich-devel-default ${fc_name}] } { set fortran ${fc_name} }
+#Restrict to gcc/clang because of dependencies
+compilers.choose    fc cc cxx cpp
+compilers.setup     -clang -dragonegg -llvm
+mpi.setup
+
+#Select mpi/gcc default:
+#  If a MPI variant is given, take it
+#  If not, check if there is a selected MPI; if yes, take it
+#  If there is no defaut MPI, take mpich
+#  If "sequential" or "-mpi(default)" variant, take no MPI
+set current_mpi "none"
+set mpi_default mpich
+set sequential [variant_isset sequential]
+if {!${sequential} } {
+    if { [mpi_variant_isset] } {
+        set current_mpi [mpi_variant_name]
+    } else {
+        if {![catch {set current_mpi [file link ${prefix}/etc/select/mpi/current]}]} {
+            if { ${current_mpi} eq "base" } { set current_mpi "none" }
+        }
+        if { ${current_mpi} eq "none"} { 
+            if {[info exists variations(${mpi_default})] && \
+                $variations(${mpi_default}) eq "-"} {
+                set sequential 1
+            } else {
+                set current_mpi ${mpi_default}
+                default_variants-append +${mpi_default}
             }
         }
     }
-
-    if { ${fortran} eq "unknown" } {
-        if { [variant_isset openmpi] } {
-            ui_error "Variant +openmpi requires openmpi to have been built with Fortran support."
-            return -code error "Variant +openmpi requires openmpi to have been built with Fortran support."
-        } elseif { [variant_isset mpich] } {
-            ui_error "Variant +mpich requires mpich-default/mpich-devel-default to have been built with Fortran support."
-            return -code error "Variant +mpich requires mpich-default/mpich-devel-default to have been built with Fortran support."
-        } else {
-            ui_error "Internal error: cannot determine Fortran compiler."
-            return -code error "Internal error: cannot determine Fortran compiler."
-        }
+}
+if {${sequential}} {
+    if { [gcc_variant_isset] } {
+        default_variants-append +[c_variant_name]
+    } else {
+        default_variants-append +gcc48
     }
+}
 
+pre-fetch {
     if { [variant_isset libxc] } {
-        if { ![active_variants libxc ${fortran}] } {
-            ui_error "libxc must have been built with +${fortran}."
-            return -code error "libxc must have been built with +${fortran}."
+        if { ![active_variants libxc [c_variant_name]] } {
+            ui_error "libxc must have been built with +[c_variant_name]"
+            return -code error "libxc must have been built with +[c_variant_name]"
         }
     }
-
     if { [variant_isset netcdf] } {
-        if {![active_variants netcdf-fortran ${fortran}] } {
-            ui_error "netcdf-fortran must have been built with +${fortran}."
-            return -code error "netcdf-fortran must have been built with +${fortran}."
+        if {![active_variants netcdf-fortran [c_variant_name]] } {
+            ui_error "netcdf-fortran must have been built with +[c_variant_name]"
+            return -code error "netcdf-fortran must have been built with +[c_variant_name]"
         }
     }
-
     if { [variant_isset etsf_io] } {
-        if {![active_variants etsf_io ${fortran}] } {
-            ui_error "etsf_io must have been built with +${fortran}."
-            return -code error "etsf_io must have been built with +${fortran}."
+        if {![active_variants etsf_io [fortran_variant_name]] } {
+            ui_error "etsf_io must have been built with +[fortran_variant_name]"
+            return -code error "etsf_io must have been built with +[fortran_variant_name]"
+        }
+    }
+    if { [variant_isset fftw3] } {
+        if {![active_variants fftw-3 gfortran] || \
+            ![active_variants fftw-3-single gfortran]} {
+            ui_error "fftw-3 and nfftw-3-single must have been built with +gfortran:"
+            ui_error "This can be achieved by : sudo port -f install fftw-3-single +gfortran"
+            return -code error "fftw-3/fftw-3-single must have been built with +gfortran"
         }
     }
-
-#   No need to check compiler for wannier90
-#   No need to check compiler for fftw-3
 }
 
-# fix unnecessary rejection of libxc 2.1.0
-patchfiles patch-configure.diff
+#Fix unnecessary rejection of libxc 2.1.0
+#Fix a openMP syntax error 
+patchfiles patch-configure.diff \
+           patch-src-71_bse-haydock.F90.diff
 
 pre-configure {
     if { [variant_isset etsf_io] } {
@@ -141,7 +139,6 @@
         } else {
             configure.args-append  --with-trio-flavor="none"
     }
-
     set dft-flavor "none"
     if { [variant_isset libxc] } {
         if { ${dft-flavor} eq "none" } {
@@ -157,13 +154,6 @@
             set dft-flavor ${dft-flavor}"+wannier90"
         }
     }
-    if { [variant_isset bigdft] } {
-        if { ${dft-flavor} eq "none" } {
-            set dft-flavor "bigdft"
-        } else {
-            set dft-flavor ${dft-flavor}"+bigdft"
-        }
-    }
     if { [variant_isset atompaw] } {
         if { ${dft-flavor} eq "none" } {
             set dft-flavor "atompaw"
@@ -171,7 +161,34 @@
             set dft-flavor ${dft-flavor}"+atompaw"
         }
     }
+#    if { [variant_isset bigdft] } {
+#        if { ${dft-flavor} eq "none" } {
+#            set dft-flavor "bigdft"
+#        } else {
+#            set dft-flavor ${dft-flavor}"+bigdft"
+#        }
+#    }
     configure.args-append  --with-dft-flavor="${dft-flavor}"
+
+    configure.args-append  FCCPP="${configure.cpp} -ansi"
+
+    if { ${current_mpi} ne "none"} {
+        configure.args-append  --enable-mpi="yes" --enable-mpi-io="yes"
+        if {[string match *openmpi* ${current_mpi}]} {
+            configure.args-append   --with-mpi-level="1"
+        }
+        if { ![mpi_variant_isset] } {
+            configure.fc     ${mpi.fc}
+            configure.cc     ${mpi.cc}
+            configure.cxx    ${mpi.cxx}
+        }
+        if { ${mpi.exec} ne "" } {
+            configure.args-append  MPI_RUNNER=${mpi.exec}
+            test.env-append        MPIEXEC=${mpi.exec}
+        }
+    } else {
+        configure.args-append  --enable-mpi="no" --enable-mpi-io="no"
+    }
 }
 
 destroot {
@@ -193,51 +210,17 @@
 # test.run            yes
 # test.target         check-full
 
-variant gcc46 conflicts gcc47 gcc48 openmpi mpich description {Build with GCC 4.6} {
-    configure.compiler  macports-gcc-4.6
-    configure.args-append  FCCPP="${configure.cpp} -ansi"
-}
-
-variant gcc47 conflicts gcc46 gcc48 openmpi mpich description {Build with GCC 4.7} {
-    configure.compiler  macports-gcc-4.7
-    configure.args-append  FCCPP="${configure.cpp} -ansi"
-}
-
-variant gcc48 conflicts gcc46 gcc47 openmpi mpich description {Build with GCC 4.8} {
-    configure.compiler  macports-gcc-4.8
-    configure.args-append  FCCPP="${configure.cpp} -ansi"
-}
-
-variant openmpi conflicts gcc46 gcc47 gcc48 mpich description {Build parallel version with OpenMPI} {
-    depends_lib-append     path:bin/mpif90-openmpi-mp:openmpi-default
-    configure.fc           mpif90-openmpi-mp
-    configure.cc           mpicc-openmpi-mp
-    configure.cxx          mpicxx-openmpi-mp
-    configure.args-append  FCCPP="${configure.cc} -E -ansi"
-    configure.args-append  --enable-mpi="yes" --enable-mpi-io="yes" --with-mpi-level="1"
-    configure.args-append  MPI_RUNNER=${prefix}/bin/mpiexec-openmpi-mp
-    test.env-append        MPIEXEC=${prefix}/bin/mpiexec-openmpi-mp
-}
-
-variant mpich conflicts gcc46 gcc47 gcc48 openmpi description {Build parallel version with MPICH} {
-    depends_lib-append     path:bin/mpif90-mpich-mp:mpich-default
-    configure.fc           mpif90-mpich-mp
-    configure.cc           mpicc-mpich-mp
-    configure.cxx          mpicxx-mpich-mp
-    configure.args-append  FCCPP="${configure.cc} -E -ansi"
-    configure.args-append  --enable-mpi="yes" --enable-mpi-io="yes"
-    configure.args-append  MPI_RUNNER=${prefix}/bin/mpiexec-mpich-mp
-    test.env-append        MPIEXEC=${prefix}/bin/mpiexec-mpich-mp
-}
-
 variant fftw3 description {Build with support for fftw-3 FFT library} {
     depends_lib-append      port:fftw-3
+    depends_lib-append      port:fftw-3-single
+#   require_active_variants fftw-3 gfortran
+#   require_active_variants fftw-3-single gfortran
     if { [variant_isset threads] } {
         configure.args-append   --with-fft-flavor="fftw3-threads"
-        configure.args-append   --with-fft-libs="-lfftw3_threads -lfftw3"
+        configure.args-append   --with-fft-libs="-lfftw3_threads -lfftw3 -lfftw3f"
     } else {
         configure.args-append   --with-fft-flavor="fftw3"
-        configure.args-append   --with-fft-libs="-lfftw3"
+        configure.args-append   --with-fft-libs="-lfftw3 -lfftw3f"
     }
 }
 
@@ -270,6 +253,12 @@
     depends_lib-append      port:wannier90
 }
 
+variant atompaw description {Build including AtomPAW atomic dataset generator} {
+}
+
+variant sequential conflicts mpich mpich_devel openmpi openmpi_devel description {Build without MPI support} {
+}
+
 # FIXME: the code's build system will download BigDFT itself, which is contrary to the
 # way MacPorts should work. Make a bigdft port to support this.
 # There is an error compiling anyway, which could be fixed with a patch,
@@ -282,9 +271,7 @@
 #    patchfiles-append       patch-fallbacks-sources-bigdft-1.7.0.93-src-Makefile.in.diff
 #}
 
-variant atompaw description {Build including AtomPAW atomic dataset generator} {
-}
- 
 livecheck.type      regex
 livecheck.url       ${homepage}/downloads/source-packages/abinit-1/releases/index.html
 livecheck.regex     ABINIT (\[0-9.\]+)
+
