--- Portfile	2013-06-21 18:30:22.000000000 -0400
+++ Portfile.combined	2013-07-17 00:43:27.000000000 -0400
@@ -5,7 +5,7 @@
 
 name                nginx
 version             1.4.1
-revision            1
+revision            5
 categories          www mail
 platforms           darwin
 license             BSD
@@ -45,6 +45,19 @@
 set nginx_pidfile   ${nginx_rundir}/${name}.pid
 set nginx_all_confs {fastcgi.conf fastcgi_params mime.types nginx.conf scgi_params uwsgi_params}
 
+# portfile-scoped vars for variant routines to re-def and use in port-stages (post-extract, pre-patch, etc)
+set nginx_upload_mod_dirname   ""
+set nginx_upload_progress_mod_dirname   ""
+
+
+# for aforementioned re-defs of portfile-scoped vars in variant routines
+proc nginx_set_portfile_var {var_name new_value} {
+    upvar   $var_name   portfile_var
+    set portfile_var   $new_value
+}
+
+
+
 configure.args-append \
                     --with-cc-opt=\"${configure.cppflags} ${configure.cflags}\" \
                     --with-ld-opt=\"${configure.ldflags}\" \
@@ -60,7 +73,7 @@
                     --with-ipv6
 
 # remove --disable-dependency-tracking
-configure.universal_args
+configure.universal_args-delete   --disable-dependency-tracking
 
 build.target        build
 destroot.keepdirs   ${destroot}${nginx_logdir} \
@@ -77,16 +90,20 @@
     file rename ${destroot}${prefix}/html ${destroot}${prefix}/share/nginx
 }
 
+
 startupitem.create      yes
 startupitem.pidfile     auto ${nginx_pidfile}
 startupitem.executable  ${prefix}/sbin/nginx
 
+
 notes                   \
     "A sample nginx configuration file has been installed in
     ${nginx_confdir}/nginx.conf.default.\nCopy the configuration to
     ${nginx_confdir}/nginx.conf\nand adjust it to your needs before starting\
     nginx."
 
+
+
 variant addition description {Append text to pages} {
     configure.args-append   --with-http_addition_module
 }
@@ -109,8 +126,30 @@
 }
 
 variant google_perftools description {Enable Google Performance Tools profiling for workers} {
-    configure.args-append   --with-google_perftools_module
     depends_lib-append      port:google-perftools
+
+    # If 'google_perftools' port doesn't find headers needed for the profiler lib, it just quietly skips it.
+    # Check if "-lprofiler" will fail -- if so, pre-empt configure-stage error with a more useful error msg.
+    if {    [file exists "${prefix}/lib/libprofiler.dylib"] ||
+                    [file exists "${prefix}/lib/libprofiler.a"]
+    } {
+        configure.args-append   --with-google_perftools_module
+    } \
+    else {
+        ui_warn [ join   [list                                                                       \
+            "On some OSX versions, the 'google_perftools' port is installed without library files "   \
+            "needed by the \"google_perftools\" variant ( libprofiler.dylib or libprofiler.a ). \n"   \
+            "Until the 'google_perftools' port is fixed, this variant is incompatible with these "    \
+            "OSX versions. \n\n"                                                                      \
+            "If the 'google_perftools' port HAS BEEN updated, running the command: \n"                \
+            "    port -f upgrade --enforce-variants nginx +google_perftools \n"                       \
+            "will indicate if this bug was fixed, based on whether it installs successfully and "     \
+            "DOES NOT display this message again. \n\n"                                               \
+            "Aborting 'nginx' port installation... "                                                  \
+        ] "" ]
+
+        return -code error   "Variant \"+google_perftools\" does not support current 'google_perftools' port."
+    }
 }
 
 variant gzip_static description {Avoids compressing the same file each time it is requested} {
@@ -177,20 +216,41 @@
                             rmd160  5734af837be3fe8ec444a7e5e7f6707118594098 \
                             sha256  b1c26abe0427180602e257627b4ed21848c93cc20cefc33af084983767d65805
     configure.args-append   --add-module=${workpath}/${upload_distname}
+
+    # v2.2.0 release references members of ngx_http_request_body_t that were renamed in later nginx versions.
+    # Patch nginx_upload_module.c with fixed version from https://github.com/TimothyKlim/nginx-upload-module.
+    patchfiles-append       patch-nginx_upload_module.tmp-ngx_http_upload_module.c.diff
+
+
+    nginx_set_portfile_var   nginx_upload_mod_dirname    "${upload_distname}"
+
+    # patch routine balks on file names with "../", but rel-paths in patchfiles start from $worksrcpath
+    pre-patch {
+        ln -s   "${workpath}/${nginx_upload_mod_dirname}" "${worksrcpath}/nginx_upload_module.tmp"
+    }
+    post-patch {
+        file delete   "${worksrcpath}/nginx_upload_module.tmp"
+    }
 }
 
 variant upload_progress description {Enable Brice Figureau's upload progress module (https://github.com/masterzen/nginx-upload-progress-module)} {
     set uploadprg_filename  nginx_uploadprogress_module
     set uploadprg_version   0.9.0
     set uploadprg_distname  ${uploadprg_filename}-${uploadprg_version}
+
     distfiles-append        ${uploadprg_distname}.tar.gz:uploadprogress_module
-    post-extract {
-        move [glob ${workpath}/masterzen-nginx-upload-progress-module-a788dea] ${workpath}/nginx_uploadprogress_module-0.9.0
-    }
     checksums-append        ${uploadprg_distname}.tar.gz \
                             rmd160  10ff91748a06afd81abb9fdb203f164ea369463a \
                             sha256  3fb903dab595cf6656fa0fc5743a48daffbba2f6b5c554836be630800eaad4e2
     configure.args-append   --add-module=${workpath}/${uploadprg_distname}
+
+
+    nginx_set_portfile_var   nginx_upload_progress_mod_dirname    "${uploadprg_distname}"
+
+    post-extract {
+        move   [glob -directory "${workpath}"   masterzen-nginx-upload-progress-module*]   \
+               "${workpath}/${nginx_upload_progress_mod_dirname}"
+    }
 }
 
 variant xslt description {Post-process pages with XSLT} {
