--- Portfile.orig	2017-11-11 11:11:12.000000000 -0700
+++ Portfile	2017-11-12 07:17:17.000000000 -0700
@@ -3,6 +3,7 @@
 PortSystem 1.0
 PortGroup select 1.0
 PortGroup compiler_blacklist_versions 1.0
+PortGroup muniversal 1.0
 
 #remember to update the -doc and -server as well
 name                postgresql96
@@ -29,6 +30,9 @@
 
 use_bzip2           yes
 
+# do not build man or html files (use postgresqlXY-doc instead)
+patchfiles-append   patch-no_doc.diff
+
 depends_lib         port:readline path:lib/libssl.dylib:openssl port:zlib port:libxml2 port:libxslt
 depends_build       port:bison
 depends_run         port:postgresql_select
@@ -41,6 +45,7 @@
                     --libdir=${libdir} \
                     --includedir=${prefix}/include/${name} \
                     --datadir=${prefix}/share/${name} \
+                    --docdir=${prefix}/share/doc/${name} \
                     --mandir=${prefix}/share/man \
                     --with-includes=${prefix}/include \
                     --with-libraries=${prefix}/lib \
@@ -63,66 +68,24 @@
 
 if {[variant_isset universal]} {
     configure.cflags-append    -DAC_APPLE_UNIVERSAL_BUILD
-    post-configure {
-        system "cd ${worksrcpath}/src/include \
-                && ed - pg_config.h < ${filespath}/pg95_config.h.ed \
-                && touch stamp-h"
-        system "cd ${worksrcpath}/src/interfaces/ecpg/include \
-                && ed - ecpg_config.h < ${filespath}/ecpg_config.h.ed \
-                && touch stamp-h"
-    }
 }
 
 build.type          gnu
-build.target
+build.target        world
 
 test.run            yes
 test.target         check
 
+destroot.target     install-world
+
 # remember to change livecheck with first proper release
 livecheck.type      regex
 #livecheck.regex     (9\\.6\[.0-9\]*\[a-z\]+\[0-9\])
 livecheck.regex    (9\\.6\\.\[.0-9\]+)
 livecheck.url       ${homepage}/ftp/source/
 
-# prepare the contrib modules
-set contribs {}
-
-post-build {
-# remove a Linux-specific contrib before continuing
-    file delete -force ${worksrcpath}/contrib/sepgsql
-# remove perl-specific contrib, if no perl variant
-    if {![variant_isset perl]} {
-        file delete -force ${worksrcpath}/contrib/hstore_plperl
-    }
-# remove python-specific contrib, if no python27 variant
-    if {![variant_isset python27]} {
-        file delete -force ${worksrcpath}/contrib/hstore_plpython
-        file delete -force ${worksrcpath}/contrib/ltree_plpython
-    }
-    set contribdirs [glob -nocomplain -type d ${worksrcpath}/contrib/*]
-    foreach d [lsort $contribdirs] {
-        if { [file exists $d/Makefile] } {
-            lappend contribs [file tail $d]
-        }
-    }
-    foreach contrib ${contribs} {
-        system "cd ${worksrcpath}/contrib/${contrib}/ && ${build.cmd}"
-    }
-}
-
 post-destroot {
-    foreach contrib ${contribs} {
-        system "echo contrib: \"${contrib}\""
-        system "cd ${worksrcpath}/contrib/${contrib}/ && \
-            ${destroot.cmd} install ${destroot.destdir} && touch DONE"
-    }
-    system "cd ${destroot}${prefix}/bin && ln -sf ${libdir}/bin/psql psql96"
-    file delete -force ${destroot}${prefix}/share/doc/postgresql \
-        ${destroot}${prefix}/share/man
-        
-    reinplace -E "s#-arch\[\[:space:\]\]+\[\[:alnum:\]_\]+##g" \
-        ${destroot}${prefix}/lib/${name}/pgxs/src/Makefile.global
+    ln -sf ${libdir}/bin/psql ${destroot}${prefix}/bin/psql96
     
     reinplace -E "s#^CFLAGS =#CFLAGS +=#g" \
         ${destroot}${prefix}/lib/${name}/pgxs/src/Makefile.global
@@ -131,6 +94,23 @@
         ${destroot}${prefix}/lib/${name}/pgxs/src/Makefile.global
 }
 
+if {![variant_isset universal]} {
+    post-destroot {
+        reinplace -E "s#-arch\[\[:space:\]\]+\[\[:alnum:\]_\]+##g" \
+            ${destroot}${prefix}/lib/${name}/pgxs/src/Makefile.global
+    }
+} else {
+    merger-post-destroot {
+        foreach arch ${configure.universal_archs} {
+            reinplace -E "s#-arch\[\[:space:\]\]+\[\[:alnum:\]_\]+##g" \
+                ${destroot}-${arch}${prefix}/lib/${name}/pgxs/src/Makefile.global
+
+            reinplace -E "s#${worksrcpath}-${arch}#${worksrcpath}#g" \
+                ${destroot}-${arch}${prefix}/lib/${name}/pgxs/src/Makefile.global
+        }
+    }
+}
+
 select.group        postgresql
 select.file         ${filespath}/${name}
 
