--- Portfile.orig	2013-06-06 17:55:00.000000000 -0400
+++ Portfile	2013-06-17 13:07:07.000000000 -0400
@@ -5,38 +5,62 @@
 PortGroup  active_variants 1.1
 
 name          arpack
-version       3.1.2
-revision      1
+version       3.1.3
 categories    math
 license       BSD
 platforms     darwin
 maintainers		mmoll
-description		Package for solving large scale eigenvalue problems
+description		Package for solving large-scale eigenvalue problems
 long_description  \
     ARPACK is a collection of Fortran77 subroutines designed to solve large \
     scale eigenvalue problems. Parallel ARPACK (PARPACK) is included if built with an MPI variant.
 homepage            http://forge.scilab.org/index.php/p/arpack-ng
-master_sites        ${homepage}/downloads/497
-distfiles           get
-dist_subdir         ${name}/${version}
-checksums           md5     7644e234f219ec1a29849b6d7811a6b1 \
-                    sha1    f5453e2d576f131890ca023e1d853e18920f9c3c \
-                    rmd160  2c55eca384cc0a1aebacdef527f88426932bf57b
-worksrcdir          ${name}-ng_${version}
+master_sites        ${homepage}/downloads/get
+distfiles           arpack-ng-${version}.tar.gz
+checksums           md5     90ba42eae70b517ebdb72921cb4186df \
+                    sha1    c1ac96663916a4e11618e9557636ba1bd1a7b556 \
+                    rmd160  30a3b8a707417afeb3f8a794ee3aa0160fa3e745
+worksrcdir          ${name}-ng-${version}
 
 configure.args-append home=${worksrcpath} --disable-mpi
-# LDFLAGS needs to be defined here to avoid it having also a "-arch" option, considered illegal by gfortran
 if { ${build_arch}=="x86_64" || ${build_arch}=="ppc64" } {
-    configure.args-append FFLAGS='-O2 -m64' LDFLAGS='-L${prefix}/lib'
+    configure.args-append FFLAGS='-O2 -m64'
 } else {
-    configure.args-append FFLAGS='-O2 -m32' LDFLAGS='-L${prefix}/lib'
+    configure.args-append FFLAGS='-O2 -m32'
 }
+# LDFLAGS needs to be cleared to avoid it having a "-arch" option, considered illegal by gfortran
+# also, if it has ${prefix}/lib it will prevent +accelerate from working if atlas is present.
+configure.args-append LDFLAGS=''
 
 pre-fetch {
-	if {"darwin" == ${os.platform} && ${os.major} < 9} {
-		ui_error "${name} ${version} requires Mac OS X 10.5 or greater"
-		return -code error "incompatible Mac OS X version"
-	}
+   if {"darwin" == ${os.platform} && ${os.major} < 9} {
+	ui_error "${name} ${version} requires Mac OS X 10.5 or greater"
+	return -code error "incompatible Mac OS X version"
+   }
+
+   if {[variant_isset openmpi]} {
+      if { ![active_variants openmpi gcc43] && ![active_variants openmpi gcc44] && \
+           ![active_variants openmpi gcc45] && ![active_variants openmpi gcc46] && \
+           ![active_variants openmpi gcc47] && ![active_variants openmpi gcc48] && \
+           ![active_variants openmpi g95] } {
+	ui_error "${name} with +openmpi requires that openmpi was built with a Fortran variant"
+	return -code error "openmpi needs Fortran variant"
+      }
+   }
+
+   if {[variant_isset mpich]} {
+      # mpich has a path dependency on mpif77, which could be satisfied by mpich-devel instead
+      # Fortunately, if mpif77 is present, this check is not needed.
+      if { [_portnameactive mpich] } {
+         if { ![active_variants mpich gcc43] && ![active_variants mpich gcc44] && \
+           ![active_variants mpich gcc45] && ![active_variants mpich gcc46] && \
+           ![active_variants mpich gcc47] } {
+	  ui_error "${name} with +mpich requires that mpich was built with a Fortran variant"
+	  return -code error "mpich needs Fortran variant"
+        }
+      }
+    }
+
 }
 
 # Fortran compilers can not cross-compile
@@ -46,81 +70,86 @@
     set universal_archs_supported "ppc ppc64"
 }
 
-variant openmpi description {build PARPACK, with OpenMPI} conflicts mpich universal {
-    depends_lib-append    port:openmpi
-    configure.args-delete --disable-mpi
-    configure.args-append --enable-mpi
-    configure.env-append  MPIF77=${prefix}/bin/openmpif77
+variant openmpi description {build PARPACK, with OpenMPI} conflicts gcc43 gcc44 gcc45 gcc46 gcc47 g95 mpich universal {
+    depends_lib-append     port:openmpi
+    configure.args-delete  --disable-mpi
+    configure.args-append  --enable-mpi
+    configure.env-append   F77=${prefix}/bin/openmpif77
+    # see g95 variant below for explanation
+    if { [active_variants openmpi g95] } {
+      configure.args-append --disable-shared
+    }
 }
 
-variant mpich description {build PARPACK, with MPICH} conflicts openmpi {
-    depends_lib-append    path:bin/mpif77:mpich
-    configure.args-delete --disable-mpi
-    configure.args-append --enable-mpi
-    configure.env-append  MPIF77=${prefix}/bin/mpif77
+variant mpich description {build PARPACK, with MPICH} conflicts gcc43 gcc44 gcc45 gcc46 gcc47 g95 openmpi {
+    depends_lib-append     path:bin/mpif77:mpich
+    # unfortunately, by default mpich builds without Fortran support, which won't work here. ticket #39428
+    configure.args-delete  --disable-mpi
+    configure.args-append  --enable-mpi
+    configure.env-append   F77=${prefix}/bin/mpif77
 }
 
-if {![variant_isset mpich]} {
-    default_variants +openmpi
+if { ![variant_isset gcc43] && ![variant_isset gcc44] && ![variant_isset gcc45] && ![variant_isset gcc46] \
+   && ![variant_isset gcc47] && ![variant_isset g95] && ![variant_isset mpich]} {
+        default_variants +openmpi
 }
 
 
-variant gcc43 description {build with gfortran-mp-4.3} conflicts gcc44 gcc45 gcc46 gcc47 g95 universal {
+variant gcc43 description {build with gfortran-mp-4.3} conflicts gcc44 gcc45 gcc46 gcc47 g95 universal openmpi mpich {
 	depends_build-append    port:gcc43
-	configure.env-append F77=${prefix}/bin/gfortran-mp-4.3
-	if {[variant_isset mpich]}   { require_active_variants mpich   gcc43 }
-	if {[variant_isset openmpi]} { require_active_variants openmpi gcc43 }
+	configure.compiler      macports-gcc-4.3
 }
 
-variant gcc44 description {build with gfortran-mp-4.4} conflicts gcc43 gcc45 gcc46 gcc47 g95 {
+variant gcc44 description {build with gfortran-mp-4.4} conflicts gcc43 gcc45 gcc46 gcc47 g95 openmpi mpich {
 	depends_build-append    port:gcc44
-	configure.env-append F77=${prefix}/bin/gfortran-mp-4.4
-	if {[variant_isset mpich]}   { require_active_variants mpich   gcc44 }
-	if {[variant_isset openmpi]} { require_active_variants openmpi gcc44 }
+	configure.compiler      macports-gcc-4.4
 }
 
-variant gcc45 description {build with gfortran-mp-4.5} conflicts gcc43 gcc44 gcc46 gcc47 g95 {
+variant gcc45 description {build with gfortran-mp-4.5} conflicts gcc43 gcc44 gcc46 gcc47 g95 openmpi mpich {
 	depends_build-append    port:gcc45
-	configure.env-append F77=${prefix}/bin/gfortran-mp-4.5
-	if {[variant_isset mpich]}   { require_active_variants mpich   gcc45 }
-	if {[variant_isset openmpi]} { require_active_variants openmpi gcc45 }
+	configure.compiler      macports-gcc-4.5
 }
 
-variant gcc46 description {build with gfortran-mp-4.6} conflicts gcc43 gcc44 gcc45 gcc47 g95 {
+variant gcc46 description {build with gfortran-mp-4.6} conflicts gcc43 gcc44 gcc45 gcc47 g95 openmpi mpich {
 	depends_build-append    port:gcc46
-	configure.env-append F77=${prefix}/bin/gfortran-mp-4.6
-	if {[variant_isset mpich]}   { require_active_variants mpich   gcc46 }
-	if {[variant_isset openmpi]} { require_active_variants openmpi gcc46 }
+	configure.compiler      macports-gcc-4.6
 }
 
-variant gcc47 description {build with gfortran-mp-4.7} conflicts gcc43 gcc44 gcc45 gcc46 g95 {
+variant gcc47 description {build with gfortran-mp-4.7} conflicts gcc43 gcc44 gcc45 gcc46 g95 openmpi mpich {
 	depends_build-append    port:gcc47
-	configure.env-append F77=${prefix}/bin/gfortran-mp-4.7
-	if {[variant_isset mpich]}   { require_active_variants mpich   gcc47 }
-	if {[variant_isset openmpi]} { require_active_variants openmpi gcc47 }
+	configure.compiler      macports-gcc-4.7
 }
-variant g95 description {build with g95} conflicts gcc43 gcc44 gcc45 gcc46 gcc47 universal {
+
+# does not work without --shared-lib, will not accept -force_load in linking stage
+variant g95 description {build with g95 (static only)} conflicts gcc43 gcc44 gcc45 gcc46 gcc47 universal openmpi mpich {
         depends_build-append    port:g95
-        configure.env-append F77=${prefix}/bin/g95
-	if {[variant_isset mpich]}   { require_active_variants mpich   g95 }
-	if {[variant_isset openmpi]} { require_active_variants openmpi g95 }
+        configure.args-append   --disable-shared
+        configure.env-append    F77=${prefix}/bin/g95
 }
 
-if { ![variant_isset gcc43] && ![variant_isset gcc44] && ![variant_isset gcc45] && ![variant_isset gcc46] && ![variant_isset g95] } {
+if { ![variant_isset gcc43] && ![variant_isset gcc44] && ![variant_isset gcc45] && ![variant_isset gcc46] \
+   && ![variant_isset g95] && ![variant_isset openmpi] && ![variant_isset mpich]} {
 	default_variants      +gcc47
 }
 
 variant accelerate conflicts atlas description {build with Accelerate framework} {
-    configure.args-append --with-blas=/usr/lib/libblas.dylib --with-lapack=/usr/lib/liblapack.dylib
-    patchfiles-append patch-ARPACK-cdot-and-zdot.diff
+    # the change of LDFLAGS is required to avoid linking against the ATLAS ones if they are present;
+    # if we specify /usr/lib/libblas.dylib directly, the linking fails. http://forge.scilab.org/index.php/p/arpack-ng/issues/1220/
+    configure.args-delete  LDFLAGS=''
+    configure.args-append  LDFLAGS='/usr/lib/libblas.dylib /usr/lib/liblapack.dylib' --with-blas="-lblas -llapack"
+    patchfiles-append      patch-ARPACK-cdot-and-zdot.diff
     if {[variant_isset openmpi] || [variant_isset mpich]} {
         patchfiles-append patch-PARPACK-cdot-and-zdot.diff
+	# PARPACK unwisely uses 'second' from LAPACK (not provided by Accelerate) instead of internal 'arscnd' like ARPACK
+        # so, we make a 'second' which wraps 'arscnd'. Really, 'second' should just be replaced in PARPACK source.
+        # http://forge.scilab.org/index.php/p/arpack-ng/issues/1245/
+	patchfiles-append patch-UTIL-second_NONE.f.diff
     }
 }
 
 variant atlas description {build with atlas} {
     depends_lib-append port:atlas
-    configure.args-append --with-blas=${prefix}/lib/libsatlas.dylib
+    configure.args-append --with-blas="-L${prefix}/lib -lsatlas"
 }
 
 if {![variant_isset atlas]} {
