--- Portfile.orig	2015-10-30 06:01:17.000000000 -0700
+++ Portfile	2016-01-18 17:47:51.000000000 -0800
@@ -3,42 +3,53 @@
 
 PortSystem              1.0
 
-if {[variant_isset qt]} {
-    PortGroup           qt4 1.0
-}
-
 name                    gpsd
-version                 3.14
-revision                4
+version                 3.16
 license                 BSD
 categories              net
 maintainers             ryandesign openmaintainer
 platforms               darwin
 
+# The GPSD build procedure does not curently support universal binaries.
+universal_variant       no
+
 description             GPS service daemon
 
-long_description        GPSD is a service daemon that handles GPSes and other navigation-related sensors \
-                        reporting over USB, serial, TCP/IP, or UDP connections and presents reports in \
-                        a well-documented JSON application.
+long_description        GPSD is a service daemon that handles GPSes and other \
+                        navigation-related sensors reporting over USB, serial, \
+                        TCP/IP, or UDP connections and presents reports in \
+                        a well-documented JSON format.  The package also \
+                        includes a number of clients which can be run against \
+                        a local GPSD or a GPSD on another machine.
 
 homepage                http://www.catb.org/${name}/
 master_sites            savannah
 
-checksums               rmd160  fe0059e2b3d64af5857f9c150ef2fdd6bc6e115f \
-                        sha256  504fc812f3c1525a1a48e04bf4d77f9a8066c201448d98089df89d58ef53a8cb
+checksums               rmd160  f61444826db6175f27cb41535c3d62d2536a35d4 \
+                        sha256  03579af13a4d3fe0c5b79fa44b5f75c9f3cac6749357f1d99ce5d38c09bc2029
 
-depends_lib-append      port:ncurses port:python27
-depends_build-append    port:scons
+depends_lib-append      port:ncurses
+
+depends_build           port:scons
+
+# The Python executables now specify "python2" in the shebang line; hence
+# the dependency on python2_select.
+#
+# GPSD requires Python 2.6 or 2.7, but since the scons port requires 2.7,
+# we don't bother with a 2.6 variant.
+#
+depends_run             port:python2_select port:python27
 
 patchfiles              patch-SConstruct.diff \
                         patch-driver_rtcm2.c.diff \
-                        patch-libgps.pc.in.diff \
-                        patch-libgpsd.pc.in.diff \
                         leopard-IPV6_TCLASS.patch
 
-use_configure           no
+post-patch {
+    # Fix the bad permissions on gpsprof (in the 3.16 tarball)
+    system "chmod 755 ${worksrcpath}/gpsprof"
+}
 
-variant universal {}
+use_configure           no
 
 set cxx_stdlibflags {}
 if {[string match *clang* ${configure.cxx}]} {
@@ -47,38 +58,148 @@
 
 build.cmd               ${prefix}/bin/scons
 build.target
-build.args              prefix=${prefix} libQgpsmm=no usb=no strip=no
+build.args              prefix=${prefix} \
+                        qt=no \
+                        usb=no \
+                        strip=no \
+                        dbus_export=no \
+                        xgps=no
 build.env-append        CC="${configure.cc} [get_canonical_archflags cc]" \
                         CFLAGS="${configure.cflags}" \
                         CXX="${configure.cxx} [get_canonical_archflags cxx] ${cxx_stdlibflags}" \
                         CXXFLAGS="${configure.cxxflags}" \
-                        LDFLAGS="${configure.ldflags}" \
-                        PYTHON=${prefix}/bin/python2.7
+                        LDFLAGS="${configure.ldflags}"
+
+# Allow the regression tests to be run via "port test gpsd".
+#
+# The speed of the daemon tests is highly dependent on the WRITE_PAD value.
+# In particular, the "ais-nmea-sample" test (with >85000 lines of test data)
+# takes over 44 minutes to run with the standard (for OSX) 30ms WRITE_PAD.
+# Empirically, 200us seems to be adequate on a MacPro, and 500us seems to be
+# adequate on a PowerBook G4, so we use 1ms here.  If needed, this value can
+# be overridden with WRITE_PAD=XXX on the command line, but only if WRITE_PAD
+# is included in extra_env in macports.conf.  In all cases, the WRITE_PAD
+# value in use is reported by the test framework and is visible with -v.
+#
+# Note also that the MacPorts sandbox doesn't allow ptys, so the daemon tests
+# report "gpsfake: ptys not available, falling back to UDP.", but still work.
+# This can be worked around by running "scons testregress" directly in
+# ${worksrcpath}, rather than running it via "port test".
+#
+# The test phase duplicates the arguments and environment (except WRITE_PAD)
+# from the build phase, mainly to avoid gratuitous rebuilds between the phases.
+#
+test.run                yes
+test.target             testregress
+set test.args           ${build.args}
+set test.env            ${build.env}
+if { [info exists ::env(WRITE_PAD)] } {
+    test.env-append     WRITE_PAD=$::env(WRITE_PAD)
+} else {
+    test.env-append     WRITE_PAD=0.001
+}
 
 destroot.args           {*}${build.args}
 destroot.env-append     {*}${destroot.destdir} {*}${build.env}
 
+pre-test {
+    if {[catch {exec which -s python2}]} {
+        ui_error "Running tests requires the python2 command."
+        ui_error "This can be set up by, e.g.:"
+        ui_error ""
+        ui_error "    sudo port select --set python2 python27"
+        ui_error ""
+        return -code error "tests not runnable"
+    }
+}
+
 post-destroot {
-    xinstall -m 644 -W ${worksrcpath} libgps.dylib libgpsd.dylib ${destroot}${prefix}/lib
+    xinstall -m 644 -W ${worksrcpath} libgps.dylib ${destroot}${prefix}/lib
 }
 
-variant qt description {Build Qt bindings} {
-    build.args-delete       libQgpsmm=no
-    destroot.args-delete    libQgpsmm=no
+if {[catch {exec which -s python2}]} {
+    notes "
+        #####################################################################
+        # Running any of the Python utilities requires the python2 command. #
+        # This can be set up via python2_select, e.g.:                      #
+        #                                                                   #
+        #     sudo port select --set python2 python27                       #
+        #                                                                   #
+        #####################################################################"
+}
+
+if {${os.platform} == "darwin"} {
+    # Qt5 isn't officially supported for OSX < 10.8
+    if {${os.major} < 12} {
+        set qt_default 4
+        variant qt conflicts qt5 description {Build Qt4 bindings} {}
+        variant qt4 conflicts qt5 description {Build Qt4 bindings} {}
+        variant qt5 conflicts qt qt4 description {Build Qt5 bindings} {}
+    } else {
+        set qt_default 5
+        variant qt conflicts qt4 description {Build Qt5 bindings} {}
+        variant qt4 conflicts qt qt5 description {Build Qt4 bindings} {}
+        variant qt5 conflicts qt4 description {Build Qt5 bindings} {}
+    }
+} else {
+    set qt_default 0
+    variant qt4 conflicts qt5 description {Build Qt4 bindings} {}
+    variant qt5 conflicts qt4 description {Build Qt5 bindings} {}
+}
+
+if {[variant_isset qt4] || [variant_isset qt] && $qt_default == 4} {
+    PortGroup               qt4 1.0
+    # The following lines are a workaround for #50347
+    build.env-append        PKG_CONFIG_PATH=${qt_pkg_config_dir}
+    test.env-append         PKG_CONFIG_PATH=${qt_pkg_config_dir}
+}
+
+if {[variant_isset qt5] || [variant_isset qt] && $qt_default == 5} {
+    PortGroup               qt5 1.0
+    build.args-append       qt_versioned=5
+    test.args-append        qt_versioned=5
+}
+
+if {[variant_isset qt] || [variant_isset qt4] || [variant_isset qt5]} {
+    build.args-delete       qt=no
+    build.args-append       qt=yes
+    test.args-delete        qt=no
+    test.args-append        qt=yes
+    destroot.args-delete    qt=no
+    destroot.args-append    qt=yes
 }
 
 variant libusb \
 description {Include support for better USB device discovery} {
-    depends_build-append port:pkgconfig
-    depends_lib-append   port:libusb
-    build.args-delete    usb=no
-    destroot.args-delete usb=no
+    depends_build-append    port:pkgconfig
+    depends_lib-append      port:libusb
+    build.args-delete       usb=no
+    build.args-append       usb=yes
+    test.args-delete        usb=no
+    test.args-append        usb=yes
+    destroot.args-delete    usb=no
+    destroot.args-append    usb=yes
 }
 
 variant dbus description {Include support for DBUS} {
     depends_lib-append      port:dbus-glib
-    build.args-append       dbus=yes dbus_export=yes
-    destroot.args-append    dbus=yes dbus_export=yes
+    build.args-delete       dbus_export=no
+    build.args-append       dbus_export=yes
+    test.args-delete        dbus_export=no
+    test.args-append        dbus_export=yes
+    destroot.args-delete    dbus_export=no
+    destroot.args-append    dbus_export=yes
+}
+
+variant xgps \
+description {Include xgps/xgpsspeed X11 clients (dependency-intensive)} {
+    depends_run-append      port:py-gobject port:py-pygtk
+    build.args-delete       xgps=no
+    build.args-append       xgps=yes
+    test.args-delete        xgps=no
+    test.args-append        xgps=yes
+    destroot.args-delete    xgps=no
+    destroot.args-append    xgps=yes
 }
 
 livecheck.type          regex
