Index: Portfile
===================================================================
--- Portfile	(revision 23759)
+++ Portfile	(working copy)
@@ -3,7 +3,7 @@
 PortSystem      1.0
 name            php5
 version         5.2.1
-revision        3
+revision        4
 categories      lang php www
 maintainers     ryandesign@macports.org jwa@macports.org
 platforms       darwin freebsd
@@ -135,7 +135,10 @@
 		--with-iodbc=/usr
 }
 
-variant apache conflicts apache2 fastcgi {
+# Build an Apache 1 module. On Mac OS X, it uses Apple's provided Apache 1 server.
+# On other platforms, the MacPorts apache port is used. Keep the options here
+# in sync with those in the relevant part of the post-destroot phase.
+variant apache conflicts apache2 {
 	if { ! [variant_isset macosx] } {
 		depends_lib-append \
 			path:${prefix}/sbin/apxs:apache
@@ -147,18 +150,24 @@
 	}
 }
 
-variant apache2 conflicts apache fastcgi {
+# Build an Apache 2 module. Keep the options here in sync with those in the
+# relevant part of the post-destroot phase.
+variant apache2 conflicts apache {
 	depends_lib-append \
 		path:${prefix}/apache2/bin/apxs:apache2
 	configure.args-append \
 		--with-apxs2=${prefix}/apache2/bin/apxs
 }
 
-variant fastcgi conflicts apache apache2 {
-	configure.args-append \
-		--enable-fastcgi \
-		--enable-force-cgi-redirect \
-		--enable-memory-limit
+# Build a FastCGI binary. Keep the options here in sync with those in the
+# relevant part of the post-destroot phase.
+variant fastcgi {
+	if { ![variant_isset apache] && ![variant_isset apache2] } {
+		configure.args-append \
+			--enable-fastcgi \
+			--enable-force-cgi-redirect \
+			--enable-memory-limit
+	}
 }
 
 variant snmp conflicts macports_snmp {
@@ -291,20 +300,15 @@
 	install-cli install-build install-headers install-programs
 
 post-destroot	{
-	# copy fastcgi php binary to the bin dir under a new name so it doesn't overwrite cli version
-	if { [variant_isset fastcgi] } {
-		xinstall -m 755 ${worksrcpath}/sapi/cgi/php ${destroot}${prefix}/bin/php-fcgi
-	}
-	
-	#copy module
+	# Copy the Apache 1 module.
 	if { [variant_isset apache] } {
-	
 		xinstall -m 755 -d ${destroot}${prefix}/libexec/apache \
 			${destroot}${prefix}/etc/apache/extras-conf
 		xinstall -m 755 ${worksrcpath}/libs/libphp5.so ${destroot}${prefix}/libexec/apache/
 		xinstall -m 755 -c ${filespath}/mod_php.conf ${destroot}$prefix/etc/apache/extras-conf
 	} 
 	
+	# Copy the Apache 2 module.
 	if { [variant_isset apache2] } {
 		xinstall -m 755 -d ${destroot}${prefix}/apache2/modules \
 			${destroot}${prefix}/apache2/conf/extras-conf
@@ -312,9 +316,50 @@
 		xinstall -m 755 -c ${filespath}/mod_php.conf ${destroot}${prefix}/apache2/conf/extras-conf
 	}
 	
+	if { [variant_isset fastcgi] } {
+		# If we've built an Apache module (any version) then the FastCGI binary
+		# will not have been built, so we need to run through the whole process
+		# again and build just the FastCGI binary. Keep the options here in sync
+		# with the options specified in the apache and apache2 variants.
+		if { [variant_isset apache] } {
+			if { ![variant_isset macosx] } {
+				configure.args-delete \
+					--with-apxs=${prefix}/sbin/apxs
+			} else {
+				configure.args-delete \
+					--with-apxs=/usr/sbin/apxs
+			}
+		}
+		if { [variant_isset apache2] } {
+			configure.args-delete \
+				--with-apxs2=${prefix}/apache2/bin/apxs
+		}
+		
+		# Run the build again to get the FastCGI binary. Keep the options here
+		# in sync with those in the fastcgi variant.
+		if { [variant_isset apache] || [variant_isset apache2] } {
+			configure.args-append \
+				--enable-fastcgi \
+				--enable-force-cgi-redirect \
+				--enable-memory-limit
+			ui_msg "$UI_PREFIX Configuring ${name} fastcgi"
+			# Note: Use command_exec as soon as a released MacPorts supports it!
+			#command_exec configure
+			my_command_exec configure
+			ui_msg "$UI_PREFIX Building ${name} fastcgi"
+			#command_exec build
+			my_command_exec build
+			ui_msg "$UI_PREFIX Staging ${name} fastcgi into destroot"
+		}
+		
+		# Copy the FastCGI binary to the bin dir under a new name so it doesn't
+		# conflict with the cli version.
+		xinstall -m 755 ${worksrcpath}/sapi/cgi/php ${destroot}${prefix}/bin/php-fcgi
+	}
+	
 #	file rename ${destroot}${prefix}/etc/pear.conf ${destroot}${prefix}/etc/pear.conf.sample
 	
-	#copy php.ini
+	# Copy the default php.ini files.
 	xinstall -m 755 -d ${destroot}${prefix}/etc
 	xinstall -m 755 ${workpath}/${worksrcdir}/php.ini-dist ${destroot}${prefix}/etc/php.ini-dist
 	xinstall -m 755 ${workpath}/${worksrcdir}/php.ini-recommended ${destroot}${prefix}/etc/php.ini-recommended
@@ -340,3 +385,8 @@
 #	ui_msg "* copy  ${prefix}/etc/pear.conf.sample to  ${prefix}/etc/pear.conf" 
 }
 
+proc my_command_exec {cmd} {
+	if {[catch {system "[command $cmd]"} result]} {
+		return -code error "[format [msgcat::mc "%s failure: %s"] $cmd $result]"
+	}
+}
