--- qt5-1.0.tcl.orig	2016-12-31 10:16:20.000000000 -0700
+++ qt5-1.0.tcl	2016-12-31 10:25:49.000000000 -0700
@@ -34,10 +34,14 @@
 # Usage:
 # PortGroup     qt5 1.0
 
+options qt5.kde_variant
+
 global available_qt_versions
 set available_qt_versions {
     qt5
+    qt56
     qt55
+    qt5-kde
 }
 
 proc qt5.get_default_name {} {
@@ -121,8 +125,10 @@
 global qt_name
 
 if { [info exists qt_name] } {
+    default qt5.kde_variant no
 } else {
     set qt_name [qt5.get_default_name]
+    default qt5.kde_variant yes
 }
 
 # Qt has what is calls reference configurations, which are said to be thoroughly tested
@@ -237,6 +243,18 @@
     return
 }
 
+# a procedure for declaring dependencies on Qt5 components, which will expand them
+# into the appropriate subports for the Qt5 flavour installed
+# e.g. qt5.depends_component qtbase qtsvg qtdeclarative
+proc qt5.depends_component {first args} {
+    global qt5_private_components
+
+    set args [linsert $args[set list {}] 0 ${first}]
+    foreach comp ${args} {
+        lappend qt5_private_components ${comp}
+    }
+}
+
 # no universal binary support in Qt 5
 #     see http://lists.qt-project.org/pipermail/interest/2012-December/005038.html
 #     and https://bugreports.qt.io/browse/QTBUG-24952
@@ -283,24 +301,6 @@
     set qt_qmake_spec ""
 }
 
-# do not try to install if qt5-qtbase dependency will fail to build
-# warn about non-reference configurations
-if { ${os.major} < 11 } {
-    pre-fetch {
-        ui_warn "Qt dependency is not supported on this platform and may not build"
-    }
-}
-
-if { ${qt_name} ne "qt5" } {
-    pre-fetch {
-        ui_warn "Qt dependency is not the latest version but may be the latest supported on your OS"
-    }
-}
-
-if {![info exists building_qt5]} {
-    depends_lib-append path:lib/pkgconfig/Qt5Core.pc:${qt_name}-qtbase
-}
-
 # use PKGCONFIG for Qt discovery in configure scripts
 depends_build-append    port:pkgconfig
 
@@ -313,3 +313,200 @@
         lappend merger_destroot_env($arch) INSTALL_ROOT=${workpath}/destroot-${arch}
     }
 }
+
+pre-configure {
+    set qt_installed_name ""
+
+    foreach qt_test_name ${available_qt_versions} {
+
+        if { [string range ${qt_test_name} end-3 end] eq "-kde" } {
+            set qt_test_port_name ${qt_test_name}
+        } else {
+            set qt_test_port_name ${qt_test_name}-qtbase
+        }
+
+        if {![catch {set installed [lindex [registry_active ${qt_test_port_name}] 0]}]} {
+            set qt_installed_name ${qt_test_name}
+        }
+    }
+
+    if { ${qt_installed_name} eq "" } {
+        ui_error "at least one Qt must be installed"
+        return -code error "insufficient dependencies"
+    }
+
+    ui_debug "qt5 PortGroup: Qt is provided by ${qt_installed_name}"
+
+    if { [variant_exists qt5kde] && [variant_isset qt5kde] } {
+        if { [string range ${qt_installed_name} end-3 end] ne "-kde" } {
+            ui_error "qt5 PortGroup: Qt is installed but not qt5-kde, as is required by this variant"
+            ui_error "qt5 PortGroup: please run `sudo port uninstall --follow-dependents ${qt_installed_name}-qtbase and try again"
+        }
+    } else {
+        if { ${qt_installed_name} ne [qt5.get_default_name] } {
+            # see https://wiki.qt.io/Qt-Version-Compatibility
+            ui_warn "qt5 PortGroup: default Qt for this platform is [qt5.get_default_name] but ${qt_installed_name} is installed"
+        }
+    }
+}
+
+if { [tbool qt5.kde_variant] } {
+    variant qt5kde description {use Qt patched for KDE compatibility} {}
+}
+
+namespace eval Qt5PortGroup {
+    proc register_dependents {} {
+        global qt5_private_components
+
+        if { ![exists qt5_private_components] || ${qt5_private_components} eq "" } {
+            # no Qt components have been requested
+            # qt5.depends_component has never been called
+            set qt5_private_components ""
+        }
+
+        if { [variant_exists qt5kde] && [variant_isset qt5kde] } {
+            set qt_kde_name qt5-kde
+
+            depends_lib-append port:${qt_kde_name}
+
+            foreach comp ${qt5_private_components} {
+                switch -exact ${comp} {
+                    qtwebkit -
+                    qtwebengine -
+                    qtwebview -
+                    qtenginio {
+                        # these components are subports
+                        depends_lib-append port:${qt_kde_name}-${comp}
+                    }
+                    default {
+                        # qt5-kde provides all components except those above
+                    }
+                }
+            }
+        } else {
+            # ![variant_isset qt5kde]
+            foreach comp "qtbase ${qt5_private_components}" {
+                switch -exact ${comp} {
+                    qtquickcontrols2 {
+                        set path lib/pkgconfig/Qt5QuickControls2.pc
+                    }
+                    qtbase {
+                        set path lib/pkgconfig/Qt5Core.pc
+                    }
+                    qtwebengine {
+                        set path lib/pkgconfig/Qt5WebEngine.pc
+                    }
+                    qtdeclarative {
+                        set path lib/pkgconfig/Qt5Qml.pc
+                    }
+                    qtserialport {
+                        set path lib/pkgconfig/Qt5SerialPort.pc
+                    }
+                    qtsensors {
+                        set path lib/pkgconfig/Qt5Sensors.pc
+                    }
+                    qtcharts {
+                        set path lib/pkgconfig/Qt5Charts.pc
+                    }
+                    qtwebkit {
+                        set path lib/pkgconfig/Qt5WebKit.pc
+                    }
+                    qtwebchannel {
+                        set path lib/pkgconfig/Qt5WebChannel.pc
+                    }
+                    qtdeclarative-render2d {
+                        set path lib/cmake/Qt5Quick/Qt5Quick_ContextPlugin.cmake
+                    }
+                    qtimageformats {
+                        set path lib/cmake/Qt5Gui/Qt5Gui_QTiffPlugin.cmake
+                    }
+                    qtvirtualkeyboard {
+                        set path lib/cmake/Qt5Gui/Qt5Gui_QVirtualKeyboardPlugin.cmake
+                    }
+                    qtsvg {
+                        set path lib/pkgconfig/Qt5Svg.pc
+                    }
+                    qtmacextras {
+                        set path lib/pkgconfig/Qt5MacExtras.pc
+                    }
+                    qtlocation {
+                        set path lib/pkgconfig/Qt5Location.pc
+                    }
+                    qtxmlpatterns {
+                        set path lib/pkgconfig/Qt5XmlPatterns.pc
+                    }
+                    qtserialbus {
+                        set path lib/pkgconfig/Qt5SerialBus.pc
+                    }
+                    qtmultimedia {
+                        set path lib/pkgconfig/Qt5Multimedia.pc
+                    }
+                    qtcanvas3d {
+                        set path libexec/qt5/qml/QtCanvas3D/libqtcanvas3d.dylib
+                    }
+                    qtdatavis3d {
+                        set path lib/pkgconfig/Qt5DataVisualization.pc
+                    }
+                    qtgraphicaleffects {
+                        set path libexec/qt5/qml/QtGraphicalEffects/libqtgraphicaleffectsplugin.dylib
+                    }
+                    qtpurchasing {
+                        set path lib/pkgconfig/Qt5Purchasing.pc
+                    }
+                    qtscript {
+                        set path lib/pkgconfig/Qt5Script.pc
+                    }
+                    qtscxml {
+                        set path lib/pkgconfig/Qt5Scxml.pc
+                    }
+                    qt3d {
+                        set path lib/pkgconfig/Qt53DCore.pc
+                    }
+                    qtconnectivity {
+                        set path lib/pkgconfig/Qt5Bluetooth.pc
+                    }
+                    qtgamepad {
+                        set path lib/pkgconfig/Qt5Gamepad.pc
+                    }
+                    qtwebview {
+                        set path lib/pkgconfig/Qt5WebView.pc
+                    }
+                    qttools {
+                        set path lib/pkgconfig/Qt5Designer.pc
+                    }
+                    qtquickcontrols {
+                        set path libexec/qt5/qml/QtQuick/Controls/libqtquickcontrolsplugin.dylib
+                    }
+                    qtwebsockets {
+                        set path lib/pkgconfig/Qt5WebSockets.pc
+                    }
+                    qttranslations {
+                        set path libexec/qt5/translations/qt_ar.qm
+                    }
+                    mysql-plugin {
+                        set path lib/cmake/Qt5Sql/Qt5Sql_QMYSQLDriverPlugin.cmake
+                    }
+                    psql-plugin {
+                        set path lib/cmake/Qt5Sql/Qt5Sql_QPSQLDriverPlugin.cmake
+                    }
+                    sqlite-plugin {
+                        set path lib/cmake/Qt5Sql/Qt5Sql_QSQLiteDriverPlugin.cmake
+                    }
+                    qtquick1 {
+                        set path lib/pkgconfig/Qt5Declarative.pc
+                    }
+                    qtenginio {
+                        set path lib/pkgconfig/Enginio.pc
+                    }
+                    default {
+                        set path ""
+                        return -code error "unknown component"
+                    }
+                }
+                depends_lib-append path:${path}:[qt5.get_default_name]-${comp}
+            }
+        }
+    }
+}
+
+port::register_callback Qt5PortGroup::register_dependents
