Index: Portfile
===================================================================
--- Portfile	(revision 131021)
+++ Portfile	(working copy)
@@ -8,7 +8,11 @@
 PortGroup           wxWidgets       1.0
 
 name                gnuplot
-version             4.6.6
+version             5.0.0
+subport             gnuplot-4.6 {
+    version         4.6.6
+}
+
 categories          math science
 # the license has some inconvenient requirements that we're not meeting
 # to be allowed to distribute binaries
@@ -31,9 +35,46 @@
 master_sites        sourceforge:project/gnuplot/gnuplot/${version}
 dist_subdir         ${name}/${version}
 
-distfiles-append    ${name}.pdf
-extract.only        ${distname}.tar.gz
-checksums           ${distname}.tar.gz \
+if {${subport} eq ${name}} {
+    conflicts       gnuplot-4.6
+
+    checksums       rmd160  e91ae15d1b50110d323450be451db5d180917f4d \
+                    sha256  417d4bc5bc914a60409bb75cf18dd14f48b07f53c6ad3c4a4d3cd9a8d7370faf
+
+    depends_lib-append \
+                    port:libcaca
+    configure.args-append \
+                    --with-caca=${prefix} \
+                    --with-cwdrc \
+                    --without-qt \
+
+    variant qt conflicts qt5 description "Enable qt terminal with Qt 4" {
+        depends_lib-append          port:qt4-mac
+        configure.args-replace      --without-qt --with-qt=qt4
+    }
+
+    variant qt5 conflicts qt description "Enable qt terminal with Qt 5" {
+        depends_lib-append          port:qt5-mac
+        # TODO: this is a workaround for a bug, either in gnuplot or in qt5 packaging on Mac
+        configure.ldflags-append    "-F${frameworks_dir}"
+        configure.cxxflags-append   "-F${frameworks_dir}" \
+                                    "-I${frameworks_dir}/QtCore.framework/Versions/5/Headers" \
+                                    "-I${frameworks_dir}/QtGui.framework/Versions/5/Headers" \
+                                    "-I${frameworks_dir}/QtNetwork.framework/Versions/5/Headers" \
+                                    "-I${frameworks_dir}/QtSvg.framework/Versions/5/Headers" \
+                                    "-I${frameworks_dir}/QtWidgets.framework/Versions/5/Headers"
+
+        configure.args-replace      --without-qt --with-qt=qt5
+    }
+
+} elseif {${subport} eq "gnuplot-4.6"} {
+    conflicts       ${name}
+
+    distfiles-append \
+                    ${name}.pdf
+    extract.only    ${distname}.tar.gz
+
+    checksums       ${distname}.tar.gz \
                     rmd160  eb1185c94d93d19b272311c71d93ba3402eea030 \
                     sha256  1f19596fd09045f22225afbfec11fa91b9ad1d95b9f48406362f517d4f130274 \
                     ${name}.pdf \
@@ -40,9 +81,27 @@
                     rmd160  659b2bba04077c3b048b6e501dde870e9f62d413 \
                     sha256  b26ebf783ef9422856b9be5e8904e7901708baee5c7e86a6bf2dab71de8f9100
 
+    patchfiles      patch-configure-aquaterm.diff
+
+    configure.args-append \
+                    --without-lisp-files
+
+    variant emacs description "An emacs mode for working with gnuplot" {
+        depends_build-append    port:${emacs_binary_provider}
+        configure.env-append    EMACS=${emacs_binary}
+        configure.args-delete   --without-lisp-files
+        configure.args-append   --with-lisp-files --with-lispdir=${emacs_lispdir}
+    }
+
+    variant qt description "Enable qt terminal" {
+        depends_lib-append      port:qt4-mac
+        configure.args-append   --enable-qt
+    }
+}
+
 depends_build       path:bin/pkg-config:pkgconfig
 
-depends_lib         port:expat \
+depends_lib-append  port:expat \
                     port:fontconfig \
                     port:gd2 \
                     port:jpeg \
@@ -53,12 +112,11 @@
                     port:readline \
                     port:zlib
 
-patchfiles          patch-configure-aquaterm.diff
-
 minimum_xcodeversions \
                     {9 3.1}
 
-configure.args      --with-readline=${prefix} \
+configure.args-append \
+                    --with-readline=${prefix} \
                     --with-gd=${prefix} \
                     --without-aquaterm \
                     --without-x \
@@ -76,7 +134,14 @@
 variant aquaterm description "Enable AquaTerm terminal" {
     depends_lib-append      port:aquaterm
     configure.args-delete   --without-aquaterm
-    configure.args-append   --with-aquaterm=${frameworks_dir}
+    # TODO: fix the patch for 4.6 to make the arguments compatible with each other
+    if {${subport} eq ${name}} {
+        configure.cflags-append     "-F${frameworks_dir}"
+        configure.ldflags-append    "-F${frameworks_dir}"
+        configure.args-append       --with-aquaterm
+    } else {
+        configure.args-append       --with-aquaterm=${frameworks_dir}
+    }
 }
 
 variant x11 {
@@ -85,13 +150,6 @@
     depends_lib-append      port:xpm
 }
 
-variant emacs description "An emacs mode for working with gnuplot" {
-    depends_build-append    port:${emacs_binary_provider}
-    configure.env-append    EMACS=${emacs_binary}
-    configure.args-delete   --without-lisp-files
-    configure.args-append   --with-lisp-files --with-lispdir=${emacs_lispdir}
-}
-
 variant luaterm description "Enable lua-based terminals" {
     depends_lib-append      port:lua
     configure.args-delete   --without-lua
@@ -111,13 +169,13 @@
     wxWidgets.use           wxWidgets-3.0
     depends_lib-append      port:${wxWidgets.port}
     configure.args-delete   --disable-wxwidgets
-    configure.args-append   --with-wx-config=${wxWidgets.wxdir}
+    if {${subport} eq ${name}} {
+        configure.args-append   --with-wx=${wxWidgets.wxdir}
+    } else {
+        configure.args-append   --with-wx-config=${wxWidgets.wxdir}
+    }
 }
 
-variant qt description "Enable qt terminal" {
-    depends_lib-append      port:qt4-mac
-    configure.args-append   --enable-qt
-}
 
 # Ideally one would enable just PMB by default, but '--with-bitmap-terminals'
 # enables lots of other obsolete terminals which clutter the output of 'set term'.
@@ -160,7 +218,11 @@
 
     set destdocdir ${destroot}${prefix}/share/doc/${name}
     xinstall -d -m 0755 ${destdocdir}
-    xinstall -m 0644 ${distpath}/${name}.pdf ${destdocdir}
+    if {${subport} eq ${name}} {
+        xinstall -m 0644 ${worksrcpath}/docs/${name}.pdf ${destdocdir}
+    } else {
+        xinstall -m 0644 ${distpath}/${name}.pdf ${destdocdir}
+    }
 
     # copy demo files
     file copy ${worksrcpath}/demo ${destdocdir}
