diff --git a/math/SuiteSparse/Portfile b/math/SuiteSparse/Portfile
index 22402a9..0fef358 100644
--- a/math/SuiteSparse/Portfile
+++ b/math/SuiteSparse/Portfile
@@ -5,8 +5,7 @@ PortSystem          1.0
 
 name                SuiteSparse
 epoch               20120107
-version             3.4.0
-revision            3
+version             4.0.0
 categories          math science
 platforms           darwin
 maintainers         nomaintainer
@@ -19,46 +18,51 @@ long_description    SuiteSparse is a single archive that contains all packages \
 
 homepage            http://www.cise.ufl.edu/research/sparse/SuiteSparse/
 master_sites        http://www.cise.ufl.edu/research/sparse/SuiteSparse/
+distname            SuiteSparse-${version}
 worksrcdir          ${name}
 
-checksums           md5     e59dcabc9173b1ba1b3659ae147006b8 \
-                    sha1    6de027d48a573659b40ddf57c10e32b39ab034c6 \
-                    rmd160  77ff7376691d2bc8581de6ae89f246cb289b2708 
+checksums           rmd160  395d9cfa86785db17e30ce02ba55984b2c896b00 \
+                    sha256  b26cf214bf981d735a2f517aee1975cee0034c865b288d2629c6280e026f6bdb
 
-patchfiles          makefiles.patch \
-                    UFconfig_UFconfig.mk-patch
-
-use_configure       no
-
-# cflags recommended for Macs in ${worksrcpath}/UFconfig/UFconfig.mk
-configure.optflags  -O3 -fno-common -fexceptions
-
-variant universal {}
-if {[variant_isset universal]} {
-    append configure.cflags " " ${configure.universal_cflags}
-} else {
-    append configure.cflags " " ${configure.cc_archflags}
-}
-
-use_parallel_build  no
-
-build.target
+global makefile
+set makefile ${worksrcpath}/SuiteSparse_config/SuiteSparse_config.mk
 
 post-patch {
     if {[variant_isset metis]} {
         # Use the MacPorts version rather than extracting copy in source dir
-        
+
         # -I$(METIS_PATH)/Lib -> -I$(METIS_PATH)/include
         reinplace \
             "s|-I\$(METIS_PATH)/Lib|-I\$(METIS_PATH)/include|g" \
             ${worksrcpath}/CHOLMOD/Lib/Makefile
-        
+
         # #include "metis.h" -> #include <metis/metis.h>
         reinplace \
             "s|#include \"metis.h\"|#include <metis/metis.h>|g" \
             ${worksrcpath}/CHOLMOD/Partition/cholmod_metis.c
+
+        # CHOLMOD Makefile is set up to detect metis local to build dir. Use Macports version.
+        reinplace \
+            "s|ifeq (../../metis-4.0, \$(wildcard ../../metis-4.0))|ifneq \(,\$\(METIS_PATH\)\)|" \
+            ${worksrcpath}/CHOLMOD/Lib/Makefile ${worksrcpath}/CHOLMOD/Demo/Makefile
+
+        # Do not try to build Metis in /opt/local
+        reinplace \
+            "s|^\$\(METIS\):$|fooblafoo:|" \
+            ${worksrcpath}/UMFPACK/Demo/Makefile
+
+        # There is a patch on the SuiteSparse homepage to get Metis 5.0 to work with SuiteSparse.
+        # It replaces all idxtype with idx_t
+        # and METIS_NodeComputeSeparator with METIS_ComputeVertexSeparator
+        reinplace \
+            "s|idxtype|idx_t|g" \
+            ${worksrcpath}/CHOLMOD/Partition/cholmod_metis.c
+        reinplace \
+            "s|METIS_NodeComputeSeparator \(\&nn, Mp, Mi, Mnw, Mew, Opt, \&csp, Mpart\)|\
+               METIS_ComputeVertexSeparator \(\\\&nn, Mp, Mi, Mnw, Opt, \\\&csp, Mpart\)|g" \
+            ${worksrcpath}/CHOLMOD/Partition/cholmod_metis.c
     }
-    
+
     # klu_version.h defines Real and Imag which conflicts with math.h on ppc
     reinplace -E \
         "s|(\[^a-zA-Z\])Real(\[^a-zA-Z\])|\\1RealPart\\2|g" \
@@ -66,12 +70,13 @@ post-patch {
     reinplace -E \
         "s|(\[^a-zA-Z\])Imag(\[^a-zA-Z\])|\\1ImagPart\\2|g" \
         ${worksrcpath}/KLU/Include/klu_version.h
-    # ranlib after ar
-    # THESE LINES NEED TO HAVE TABS ENCODED IN THE PORTFILE
-    reinplace "s|ccolamd_global\.o|ccolamd_global.o\\
-	- \$(RANLIB) libccolamd.a|" ${worksrcpath}/CCOLAMD/Lib/Makefile
-    reinplace "s|colamd_global\.o|colamd_global.o\\
-	- \$(RANLIB) libcolamd.a|" ${worksrcpath}/COLAMD/Lib/Makefile
+}
+
+configure {
+    file rename ${worksrcpath}/SuiteSparse_config/SuiteSparse_config.mk \
+                ${worksrcpath}/SuiteSparse_config/SuiteSparse_config_Linux.mk
+    file rename ${worksrcpath}/SuiteSparse_config/SuiteSparse_config_Mac.mk \
+                ${worksrcpath}/SuiteSparse_config/SuiteSparse_config.mk
 }
 
 post-configure {
@@ -79,62 +84,95 @@ post-configure {
     #    set manually.
     # This is done in post-configure so that ${configure.cc} is
     #    set to its default value.
-    reinplace -E \
-        "s|^CC = .*$|CC = ${configure.cc}|g" \
-        ${worksrcpath}/UFconfig/UFconfig.mk \
-        ${worksrcpath}/CSparse/Demo/Makefile \
-        ${worksrcpath}/CSparse/Lib/Makefile
-    reinplace -E \
-        "s|^CPLUSPLUS = .*$|CPLUSPLUS = ${configure.cxx}|g" \
-        ${worksrcpath}/UFconfig/UFconfig.mk
-    reinplace -E \
-        "s|^CFLAGS = .*$|CFLAGS = ${configure.cflags}|g" \
-        ${worksrcpath}/UFconfig/UFconfig.mk
-    reinplace -E \
-        "s|^CFLAGS = -O|CFLAGS = ${configure.cflags}|g" \
-        ${worksrcpath}/CSparse/Demo/Makefile \
-        ${worksrcpath}/CSparse/Lib/Makefile
-    reinplace -E \
-        "s|^BLAS = .*$|BLAS = -framework Accelerate|g" \
-        ${worksrcpath}/UFconfig/UFconfig.mk
-    reinplace -E \
-        "s|^LAPACK = .*$|LAPACK = -framework Accelerate|g" \
-        ${worksrcpath}/UFconfig/UFconfig.mk
-    reinplace "s|(CC)|(CC) \$(CFLAGS)|" ${worksrcpath}/CXSparse/Demo/Makefile
-    
+    reinplace "s|INSTALL_LIB = .*$|INSTALL_LIB = ${destroot}${prefix}/lib|g" ${makefile}
+    reinplace "s|INSTALL_INCLUDE = .*$|INSTALL_INCLUDE = ${destroot}${prefix}/include|g" ${makefile}
+
+    # Include build archs
+    reinplace -E "s|^\[^#\].*(CF = .*)$|\\1 \$(TARGET_ARCH)|g" ${makefile}
+
+    if {[variant_isset atlas]} {
+        # linking to the threaded dylib, is there a reason to use non-threaded?
+        reinplace "s|BLAS = .*$|BLAS = ${prefix}/lib/libtatlas.dylib|g" ${makefile}
+        reinplace "s|LAPACK = .*$|LAPACK = ${prefix}/lib/libtatlas.dylib|g" ${makefile}
+    }
+
     if {[variant_isset metis]} {
-        reinplace -E \
-            "s|^METIS_PATH = .*$|METIS_PATH = ${prefix}|g" \
-            ${worksrcpath}/UFconfig/UFconfig.mk
-        reinplace -E \
-            "s|^METIS = .*$|METIS = ${prefix}/lib/libmetis.a|g" \
-            ${worksrcpath}/UFconfig/UFconfig.mk
+        reinplace "s|^METIS_PATH = .*$|METIS_PATH = ${prefix}|g" ${makefile}
+        reinplace "s|^METIS = .*$|METIS = ${prefix}/lib/libmetis.dylib|g" ${makefile}
+    } else {
+        reinplace "s|^METIS_PATH = .*$|METIS_PATH = |g" ${makefile}
+        reinplace "s|^METIS = .*$|METIS = |g" ${makefile}
+        reinplace "s|^CHOLMOD_CONFIG = .*$|CHOLMOD_CONFIG = -DNPARTITION|g" ${makefile}
     }
 }
 
-destroot {
-    # SuiteSparse does not support "make install"
-    
-    eval xinstall -m 644 \
-        [glob ${worksrcpath}/*/Lib/*.a] \
-        ${destroot}${prefix}/lib
-    
-    xinstall -m 755 -d ${destroot}${prefix}/include/ufsparse
-    eval xinstall -m 644 \
-        [glob ${worksrcpath}/*/Include/*.h] \
-        [glob ${worksrcpath}/*/Include/*.hpp] \
-        ${worksrcpath}/UFconfig/UFconfig.h \
-        ${destroot}${prefix}/include/ufsparse
-    
+use_parallel_build  no
+
+build.target        default
+build.args-append   CC="${configure.cc}" CXX="${configure.cxx}" \
+                    CFLAGS="${configure.cflags}" LDFLAGS="${configure.ldflags}"
+
+if {[variant_isset universal]} {
+    build.args-append   TARGET_ARCH="${configure.universal_cflags}"
+} else {
+    build.args-append   TARGET_ARCH="${configure.cc_archflags}"
+}
+
+post-destroot {
+    # Create a dylib from all .a static libs
+    if {[variant_isset universal]} {
+        set arch ${configure.universal_cflags}
+    } else {
+        set arch ${configure.cc_archflags}
+    }
+
+    if {[variant_isset metis]} {
+        catch {exec port installed metis} output
+        if {[string match *+openmpi* ${output}]} {
+            # metis installed with +openmpi variant
+            set metis "-lmetis -fopenmp"
+        } else {
+            set metis "-lmetis"
+        }
+    } else {
+        set metis ""
+    }
+
+    if {[variant_isset atlas]} {
+        set atlas "-ltatlas"
+    } else {
+        set atlas "-framework Accelerate"
+    }
+
+    set major [join [lrange [split ${version} .] 0 0] .]
+
+    set libs [list libamd.a libbtf.a libcamd.a libccolamd.a \
+             libcholmod.a libcolamd.a libcxsparse.a libklu.a \
+             libldl.a librbio.a libspqr.a libsuitesparseconfig.a libumfpack.a]
+
+    system "cd ${destroot}${prefix}/lib && \
+        ${configure.cc} -Wall ${configure.cflags} ${arch} -dynamiclib -Wl,-all_load \
+        -L${prefix}/lib ${atlas} ${metis} ${libs} -o lib${name}.${version}.dylib \
+        -compatibility_version ${major} -current_version ${version} \
+        -install_name ${prefix}/lib/lib${name}.${version}.dylib"
+
+    ln -s lib${name}.${version}.dylib ${destroot}${prefix}/lib/lib${name}.dylib
+    ln -s lib${name}.${version}.dylib ${destroot}${prefix}/lib/lib${name}.${major}.dylib
+
     xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
     eval xinstall -m 644 \
         [glob ${worksrcpath}/*/Doc/*.pdf] \
         ${destroot}${prefix}/share/doc/${name}
 }
 
-variant metis description {Use METIS (not distributable)} {
+variant universal {}
+
+variant metis description {Use METIS for graph partitioning and sparse matrix ordering} {
     depends_build       port:metis
-    patchfiles-delete   UFconfig_UFconfig.mk-patch
+}
+
+variant atlas description {Use Macports' ATLAS instead of Accelerate Framework} {
+    depends_lib-append  port:atlas
 }
 
 livecheck.type      regex
