--- Portfile.orig	2008-10-10 14:55:34.000000000 -0400
+++ Portfile	2008-10-28 08:13:15.000000000 -0400
@@ -5,9 +5,10 @@
 
 name                qt4-mac
 version             4.4.3
+revision            1
 categories          aqua
 platforms           macosx
-maintainers         erickt clubjuggler@gmail.com openmaintainer
+maintainers         erickt gmail.com:clubjuggler openmaintainer
 
 homepage            http://www.trolltech.com/
 description         Qt Tool Kit (Native Aqua Version)
@@ -22,25 +23,33 @@
                     sha1 00cdf6df27a590565f557a7c02ba2edc634354d1 \
                     rmd160 7c39303f7aef6a369a16e787681b78d07c59fc76
 
-depends_lib         port:libmng port:libpng port:jpeg port:tiff
+depends_lib         port:libmng port:libpng port:jpeg port:tiff port:libiconv
 
-patchfiles          patch-mac.conf.diff
+patchfiles          patch-mac.conf.diff patch-compile.test.diff
+
+post-patch {
+    reinplace -E "s|^I_FLAGS=\$|I_FLAGS=-isystem${prefix}/include|" \
+        ${worksrcpath}/configure
+}
 
 # have to build with Apple gcc because of -fconstant-cfstrings
-configure.compiler  gcc-4.0
-configure.cmd       "CFLAGS=-isystem${prefix}/include CXXFLAGS=-isystem${prefix}/include ./configure"
 configure.pre_args
 
 # directory for .app and executable files
 # Once installed, they should not be moved so they can find each other
 # (e.g. assistant.app) and the examples directory
 set bindir ${prefix}/libexec/${name}/bin
-set frameworkdir ${prefix}/Library/Frameworks
 
 # Can be removed once MacPorts 1.7.0 is released
 if {![info exists applications_dir]} {
     set applications_dir /Applications/MacPorts
 }
+if {![info exists frameworks_dir]} {
+   set frameworks_dir ${prefix}/Library/Frameworks
+}
+
+# Location of .a and .dylib files
+set libdir ${frameworks_dir}
 
 # See
 #   http://doc.trolltech.com/qtopia4.3/buildsystem/over-configure-options-qt-1.html
@@ -61,37 +70,47 @@
     -system-zlib        -qt-gif                                             \
     -no-sql-ibase       -no-sql-mysql   -no-sql-odbc    -no-sql-psql        \
     -no-sql-sqlite      -no-nis         -no-cups                            \
-    -framework          -libdir '${frameworkdir}'                           \
+    -framework          -libdir '${frameworks_dir}'                         \
     -make libs          -make tools                                         \
-    -I${worksrcpath}/include \
-    -L${worksrcpath}/lib -L${prefix}/lib
+    -L${prefix}/lib
 
-    # Take care of this with the CXXFLAGS=-isystem${prefix}/include
-    # this puts the include at the end so that qt's include files are found first.
-    # -I${prefix}/include
-    # not made by default, anyway: -nomake demos -nomake examples
+# default: -nomake demos  -nomake examples
+# default: -iconv -svg -webkit -phonon -phonon-backend -xmlpatterns
+# default: -no-assistant-webkit
 
 build.target        first
 use_parallel_build  yes
 
 destroot.destdir    INSTALL_ROOT="${destroot}"
 
+post-configure {
+    reinplace "s|gcc|${configure.cc}|g" \
+        ${worksrcpath}/mkspecs/common/mac-g++.conf
+    reinplace "s|g++|${configure.cxx}|g" \
+        ${worksrcpath}/mkspecs/common/mac-g++.conf
+}
+
 post-destroot {
-    xinstall -d -m 0755 ${destroot}${bindir}
     xinstall -d -m 0755 ${destroot}${applications_dir}/Qt
-    xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${portname}
+
+    # If we want to avoid conflict with qt4-x11, append -mac to all the binaries
+    if { [variant_isset qt4_select] } {
+        set binappend {-mac}
+    } else {
+        set binappend {}
+    }
 
     # create link in ${prefix}/bin to executable files in ${bindir}
-    foreach bin [glob -type f -directory ${destroot}${bindir} *] {
-        system "ln -s ${bindir}/[file tail ${bin}] ${destroot}${prefix}/bin"
+    foreach bin [glob -tails -type f -directory ${destroot}${bindir} *] {
+        system "ln -s ${bindir}/${bin} ${destroot}${prefix}/bin/${bin}${binappend}"
     }
 
     # create link in ${applications_dir}/Qt to .app directories in ${bindir}
     # create link in ${prefix}/bin to all *.app/Contents/MacOS/*
     foreach app [glob -type d -directory ${destroot}${bindir} *.app] {
         system "ln -s ${bindir}/[file tail ${app}] ${destroot}${applications_dir}/Qt"
-        foreach bin [glob -type f -directory ${app} Contents/MacOS/*] {
-            system "ln -s ${bindir}/[file tail ${app}]/Contents/MacOS/[file tail ${bin}] ${destroot}${prefix}/bin"
+        foreach bin [glob -tails -type f -directory ${app}/Contents/MacOS *] {
+            system "ln -s ${bindir}/[file tail ${app}]/Contents/MacOS/${bin} ${destroot}${prefix}/bin/${bin}${binappend}"
         }
     }
 
@@ -104,8 +123,32 @@
         }
     }
 
-    # Put pkgconfig directory in correct place (Fix Ticket #15353)
-    system "mv ${destroot}${frameworkdir}/pkgconfig ${destroot}/${prefix}/lib/"
+    # Put pkgconfig directory in the same place irrespective of the noframework variant
+    system "mv ${destroot}${libdir}/pkgconfig ${destroot}${prefix}/lib/pkgconfig-${name}"
+
+    # If the qt4_select variant is not set, allow pkg-config to find the .pc files without setting PKG_CONFIG_PATH
+    if { ! [variant_isset qt4_select] } {
+        xinstall -d -m 0755 ${destroot}${prefix}/lib/pkgconfig
+        foreach pcfile [glob -tails -directory ${destroot}${prefix}/lib/pkgconfig-${name} *.pc] {
+            system "ln -s ../pkgconfig-${name}/${pcfile} ${destroot}${prefix}/lib/pkgconfig"
+        }
+    }
+
+    # Fix the .pc and .prl files by removing ${destroot}
+    foreach fixfile [glob -directory ${destroot}${prefix}/lib/pkgconfig-${name} *.pc] {
+        reinplace \
+            "s|[regsub ${prefix} ${worksrcpath} {}]/bin|[regsub ${prefix} ${bindir} {}]|g" \
+            ${fixfile}
+    }
+    foreach fixfile [glob -nocomplain -directory ${destroot} ${prefix}/lib/pkgconfig-${name}/*.pc ${libdir}/*.prl ${libdir}/*/*.prl] {
+        reinplace  \
+            "s|-L${worksrcpath}/lib|-L${libdir}|g" \
+            ${fixfile}
+
+        reinplace  \
+            "s|-F${worksrcpath}/lib|-F${frameworks_dir}|g" \
+            ${fixfile}
+    }
 
     # install documentation
     foreach doc {INSTALL LICENSE.GPL2 LICENSE.GPL3 OPENSOURCE-NOTICE.TXT README \
@@ -116,73 +159,80 @@
 
 default_variants +docs +dbus
 
-variant dbus description "Include DBus support" {
+variant qt4_select description {Do not conflict with qt4-x11} {}
+
+variant dbus description {Include DBus support} {
     depends_lib-append      port:dbus
     configure.args-append   -dbus-linked
 }
 
-variant noframework description "Do not build frameworks" {
-    configure.args-delete   -framework -libdir '${frameworkdir}'
-    configure.args-append   -no-framework
+variant noframework description {Do not build frameworks} {
+    configure.args-delete   -framework -libdir '${frameworks_dir}'
+    configure.args-append   -no-framework -libdir ${prefix}/lib/${name}
+    set libdir ${prefix}/lib/${name}
 }
 
-variant nis description "Include Network Information Service (NIS) support" {
+variant nis description {Include Network Information Service (NIS) support} {
     configure.args-delete   -no-nis
     configure.args-append   -nis
 }
 
-variant cups description "Include Common Unix Printing System (CUPS) support" {
+variant cups description {Include Common Unix Printing System (CUPS) support} {
     configure.args-delete   -no-cups
     configure.args-append   -cups
 }
 
-variant mysql4 conflicts mysql5 description "Include support for SQL via mysql4 driver" {
+variant mysql4 conflicts mysql5 description {Include support for SQL via mysql4 driver} {
     depends_lib-append      lib:libmysqlclient.12:mysql4
     configure.args-delete   -no-sql-mysql
     configure.args-append   -qt-sql-mysql -plugin-sql-mysql \
                             -I${prefix}/include/mysql -L${prefix}/lib/mysql
 }
 
-variant mysql5 conflicts mysql4 description "Include support for SQL via mysql5 driver" {
+variant mysql5 conflicts mysql4 description {Include support for SQL via mysql5 driver} {
     depends_lib-append      lib:libmysqlclient.15:mysql5
     configure.args-delete   -no-sql-mysql
     configure.args-append   -qt-sql-mysql -plugin-sql-mysql \
                             -I${prefix}/include/mysql5/mysql -L${prefix}/lib/mysql5/mysql
 }
 
-variant psql description "Include support for SQL via postgresql driver" {
+variant psql description {Include support for SQL via postgresql driver} {
     depends_lib-append      port:postgresql83
 	configure.args-delete	-no-sql-psql
 	configure.args-append	-qt-sql-psql -plugin-sql-psql \
                             -I${prefix}/include/postgresql83 -L${prefix}/lib/postgresql83
 }
 
-variant sqlite description "Include support for SQL via sqlite driver" {
+variant sqlite description {Include support for SQL via sqlite driver} {
 	configure.args-delete	-no-sql-sqlite
 	configure.args-append	-qt-sql-sqlite -plugin-sql-sqlite
 }
 
-variant ssl description "Include OpenSSL support" {
+variant ssl description {Include OpenSSL support} {
     depends_lib-append      port:openssl
     configure.args-delete   -no-openssl
     configure.args-append   -openssl
 }
 
-variant examples description "Build Qt examples" {
+variant webkit description {Use WebKit as html rendering engine in Assistant} {
+    #configure.args-delete   -no-assistant-webkit
+    configure.args-append   -assistant-webkit
+}
+
+variant examples description {Build Qt examples} {
     # configure.args-delete   "-nomake examples"
     configure.args-append   -make examples -examplesdir ${prefix}/share/${portname}/examples
 }
 
-variant demos description "Build Qt demos" {
+variant demos description {Build Qt demos} {
     # configure.args-delete   "-nomake demos"
     configure.args-append   -make demos -demosdir ${prefix}/share/${portname}/demos
 }
 
-variant docs description "Include Qt Documentation" {
+variant docs description {Include Qt Documentation} {
     configure.args-append   -make docs
 }
 
-
 livecheck.check     regex
-livecheck.url       http://trolltech.com/developer/downloads/qt/mac
-livecheck.regex     "The current version of Qt/Mac Open Source Edition is (4(?:\\.\\d+)*)"
+livecheck.url       http://trolltech.com/downloads/opensource/appdev/mac-os-cpp
+livecheck.regex     "qt-mac-opensource-src-(4(?:\\.\\d+)*)${extract.suffix}"
