--- Portfile_0	2014-03-18 22:29:21.000000000 +0100
+++ Portfile_0_new	2014-03-29 23:02:54.000000000 +0100
@@ -3,6 +3,7 @@
 
 PortSystem          1.0
 PortGroup           active_variants 1.1
+PortGroup           mpi 1.0
 
 name                abinit
 version             7.6.2
@@ -34,6 +35,11 @@
 checksums           rmd160  07fd2c968a251635d03d57b244516409845637bc \
                     sha256  79645461f7611aaf007bcddb662d21a042c64ac4085d14057387f036f9dae2c5
 
+#Restrict to gcc/clang because of dependencies
+compilers.choose    fc cc cxx cpp
+compilers.setup     -clang -dragonegg -llvm
+mpi.setup
+
 depends_lib         port:atlas
 
 configure.args      --with-linalg-flavor="atlas" --enable-gw-dpc
@@ -47,74 +53,51 @@
 use_parallel_build  no
 
 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
-    }
-}
-
-pre-fetch {
-    set fortran unknown
-    set fortrans { 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} }
-            }
+#Select mpi/gcc default
+set current_mpi "none"
+if { ![variant_isset no_mpi] } {
+    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 { ${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 { ${current_mpi} eq "none"} {
+            default_variants-append +mpich
         }
     }
+} 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 "\nlibxc must have been built with +[c_variant_name]\n"
+            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 "\nnetcdf-fortran must have been built with +[c_variant_name]\n"
+            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 "\netsf_io must have been built with +[fortran_variant_name]\n"
+            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 "\nfftw-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\n"
+            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
 }
 
 pre-configure {
@@ -156,6 +139,26 @@
         }
     }
     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 {
@@ -176,52 +179,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 fftw-3 description {Build with support for fftw-3 FFT library} {
+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"
     }
 }
 
@@ -255,13 +223,16 @@
 }
 
 variant bigdft description {Build with support for the wavelet BigDFT library} {
-#   Patch bigdft-abi-1.0.4 (missing config.Sub and config.guess)
+#   Patch bigdft-abi-1.0.4 (missing config.sub and config.guess)
     patchfiles-append       patch-bigdft-config.diff
 }
 
 variant atompaw description {Build including AtomPAW atomic dataset generator} {
 }
  
+variant no_mpi conflicts mpich mpich_devel openmpi openmpi_devel description {Build without MPI support} {
+}
+
 livecheck.type      regex
 livecheck.url       ${homepage}/downloads/source-packages/abinit-1/releases/index.html
 livecheck.regex     ABINIT (\[0-9.\]+)
