--- Portfile.orig	2018-05-14 08:17:48.000000000 -0500
+++ Portfile	2018-12-16 08:34:12.000000000 -0600
@@ -1,20 +1,18 @@
 # -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
 
+
 PortSystem              1.0
 
 name                    reduce
-# snapshot of the REDUCE development, dated Nov 30,2014 (svn revision 2855)
-version                 20141130
+version             20180920
+
 set version_dashes      [regsub {^(\d{4})(\d{2})(\d{2})$} ${version} {\1-\2-\3}]
+set svnrev          4765
+
 categories              math
+license             BINARY BSD-2 LGPL-2.1
 platforms               darwin
-maintainers             {gmail.com:mark.brethen @mbrethen} openmaintainer
-# The BULK of the files are subject to the (modified) BSD license.
-# There are some components that are subject to more restrictive
-# terms (notably the FOX GUI Toolkit, used in one version of the
-# code, is under LGPL).
-# See http://sourceforge.net/p/reduce-algebra/code/HEAD/tree/trunk/README
-license                 BSD LGPL-2.1
+maintainers         gmail.com:mark.brethen openmaintainer
 
 description             REDUCE Computer Algebra System
 
@@ -23,49 +21,13 @@
     matrix manipulation and 3D plotting. It also contains a\
     large number of additional packages.
 
-homepage                http://reduce-algebra.sourceforge.net/
-master_sites            sourceforge:project/reduce-algebra/Snapshot_${version_dashes}
-distname                reduce-src-${version_dashes}
-use_bzip2               yes
-checksums               rmd160  b08512ea6bc8900a3a557c85d0576be640489202 \
-                        sha256  b15debc944475d4daee1bcbdbaff415693522306785507f2d3110d75df6e7006
-
-depends_lib-append \
-    port:libedit\
-    port:xorg-libXcursor\
-    port:Xft2\
-    port:xorg-libXrandr\
-    port:freetype\
-    port:ncurses
-
-depends_build-append    port:autoconf \
-                        port:automake
-
-depends_run             port:gnuplot
-
-universal_variant       no
-use_parallel_build      no
-
-set docsrcdir        ${worksrcpath}/doc/manual
-set docprimersdir    ${worksrcpath}/doc/primers
-
-set docbasename      manual
-
-set genericdir       ${worksrcpath}/generic
-set redfrontsrcdir   ${genericdir}/redfront/src
-set redfrontbuilddir ${genericdir}/redfront
-
-set libexecdir       ${destroot}${prefix}/libexec/${name}
-set sharedir         ${destroot}${prefix}/share/${name}
-set redfrontsharedir ${destroot}${prefix}/share/reduce-addons/redfront
-set fontdir          ${destroot}${prefix}/share/${name}/fonts
-set resourcesdir     ${destroot}${prefix}/share/${name}/resources
-set testlogsdir      ${destroot}${prefix}/share/${name}/testlogs
-set docdir           ${destroot}${prefix}/share/doc/${name}
-set addondocdir      ${destroot}${prefix}/share/doc/reduce-addons
-set mandir           ${destroot}${prefix}/share/man/man1
-set sitelispdir      ${destroot}${prefix}/share/emacs/site-lisp/${name}
-set bindir           ${destroot}${prefix}/bin
+homepage            https://reduce-algebra.sourceforge.io/
+master_sites        sourceforge:project/reduce-algebra/snapshot_${version_dashes}
+distname            Reduce-svn${svnrev}-src
+
+checksums           rmd160 d547b19362af6e4ca31e52f718bdcdbf0cd0e063                         \
+                    sha256 58873b4337e3bb31bb561cbea53866dc60bcfa7ea5faed4297597bfb85483a2e \
+                    size   261850189
 
 pre-fetch {
     if {${os.major} < 13} {
@@ -74,265 +36,193 @@
     }
 }
 
-# This is just a stub that installs all REDUCE subports.
-if {${name} eq ${subport}} {
-    supported_archs noarch
-    distfiles
-    archive_sites
-
-    use_configure   no
-
-    build           {}
+universal_variant   no
 
-    depends_run-append\
-        port:reduce-common\
-        port:reduce-csl\
-        port:reduce-psl\
-        port:reduce-addons
+if {${name} eq ${subport}} {
+    patchfiles-append   patch-packages-plot-gnuintfc.red.diff      \
+                        patch-csl-cslbase-create_bundle.sh.diff    \
+                        patch-csl-cslbase-create_old_bundle.sh.diff
 
-    destroot {
-        # Create a dummy file so the port can be successfully activated.
-        xinstall -d ${docdir}
-        set docfile [open ${docdir}/README.${name}.txt "w"]
-        puts $docfile "REDUCE ${version} (MacPorts revision ${version}_${revision})\n"
-        puts $docfile "${long_description}\n"
-        close $docfile
+    post-patch {
+        reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/packages/plot/gnuintfc.red
     }
 
-    livecheck.version       ${version_dashes}
-    livecheck.url           https://sourceforge.net/projects/reduce-algebra/files/
-    livecheck.regex         {snapshot_(\d{4}-\d{2}-\d{2})}
-} else {
-    livecheck.type          none
-}
+    use_parallel_build  no
+
+    depends_lib-append  port:libedit         \
+                        port:libtool         \
+                        port:xorg-libXcursor \
+                        port:Xft2            \
+                        port:xorg-libXrandr  \
+                        port:fontconfig      \
+                        port:freetype        \
+                        port:ncurses
 
-subport reduce-common {
-    license                 BSD
-    revision                0
-    description             ${description} -- common files
-
-    long_description        "${long_description}
-
-This installs the files that are independent of the Lisp system used.
-"
-    use_configure           no
-
-    build.dir               ${worksrcpath}/doc/manual
-    # Build errors were caused by broken symlinks in the texlive-bin port.
-    # Fixed in r142269.
     depends_build-append \
-        port:texlive-basic\
-        port:texlive-formats-extra\
-        port:dvipng
+                        port:automake \
+                        port:autoconf \
+                        port:netpbm
 
-    patch {
-        reinplace "s|'\${JOBNAME}'|'\"\${JOBNAME}\"'|g" ${docsrcdir}/mkhtml.sh
-    }
+    configure.args-append --with-csl
+    configure.ldflags-append  "-lintl"
 
-    pre-build {
-        system -W ${docsrcdir} "${build.cmd} clean"
+    # Technically portconfigure::configure_main is not part of the official exposed
+    # MacPorts API, so theoretically it could change in the future and the port
+    # would break... but it should work for now.
+    post-configure {
+        configure.args-replace --with-csl --with-psl
+        portconfigure::configure_main
     }
 
-    destroot {
-        # Create target directory
-        xinstall -d ${docdir} ${resourcesdir}
-
-        # Copy common files
-        xinstall -m 644 -W ${docprimersdir}\
-            primer.pdf\
-            sl.pdf\
-            insidereduce.pdf ${docdir}
-        xinstall -m 644 -W ${worksrcpath}/csl/reduce.doc\
-            BSD-LICENSE.txt\
-            LGPL-2.1.txt ${docdir}
-        xinstall -m 644 {*}[glob ${docsrcdir}/${docbasename}*.tex] ${docdir}
-        xinstall -m 644 {*}[glob ${docsrcdir}/${docbasename}*.html] ${docdir}
-        xinstall -m 644 {*}[glob ${docsrcdir}/${docbasename}*.png] ${docdir}
-        xinstall -m 644 -W ${docsrcdir}\
-            ${docbasename}.pdf\
-            index.html\
-            redlogo.png\
-            ${docbasename}.css ${docdir}
-        xinstall -m 644 ${worksrcpath}/README ${docdir}
-        xinstall -m 644 ${worksrcpath}/packages/package.map ${resourcesdir}
+    # Documentation is also re-generated in the tree so that regardless of
+    # history it should end up clean and organized.
+    post-build {
+        if {[variant_isset doc]} {
+            system -W ${worksrcpath}/doc/misc ${build.cmd}
+            system -W ${worksrcpath}/doc/manual ${build.cmd}
     }
 }
 
+    depends_run         port:gnuplot
 
-subport reduce-csl {
-    license                 {BSD LGPL-2.1}
-    revision                1
-    description             ${description} -- CSL version
+    destroot {
+        set builddir [exec ${worksrcpath}/scripts/findhost.sh [exec ${worksrcpath}/config.guess]]
 
-    long_description        "${long_description}
+        set cslbuilddir ${worksrcpath}/cslbuild/${builddir}
+        set pslbuilddir ${worksrcpath}/pslbuild/${builddir}
 
-This installs the Codemist Standard Lisp version of REDUCE.
-"
+        set genericdir  ${worksrcpath}/generic
 
-    patchfiles-append       patch-csl-cslbase-crlibm-configure.diff \
-                            patch-csl-cslbase-print.c.diff
+        set sharedir    ${prefix}/share/${subport}
+        set libexecdir  ${prefix}/libexec/${subport}
+        set bindir      ${prefix}/bin
+        set mandir      ${prefix}/share/man/man1
 
-    configure.args-append   --with-csl
-
-    destroot {
-        set builddir [exec ${worksrcpath}/scripts/findhost.sh [exec ${worksrcpath}/config.guess]]
-        set cslbuilddir ${worksrcpath}/cslbuild/${builddir}
         # Create target directories
         xinstall -d \
-            ${libexecdir}/cslbuild/csl\
-            ${sharedir}\
-            ${fontdir}\
-            ${resourcesdir}\
-            ${mandir}
-        # Copy CSL files
-        xinstall -p -W ${cslbuilddir}/csl\
-            reduce\
-            reduce.img\
-            csl\
-            csl.img ${libexecdir}/cslbuild/csl
-        ln -s ${prefix}/share/doc/${name} ${libexecdir}/cslbuild/csl/reduce.doc
-        ln -s ${prefix}/share/${name}/fonts ${libexecdir}/cslbuild/csl/reduce.fonts
-        ln -s ${prefix}/share/${name}/resources ${libexecdir}/cslbuild/csl/reduce.resources
-        ln -s ${prefix}/libexec/${name}/cslbuild ${sharedir}/cslbuild
-        # Font files
-        foreach f [glob -tails -directory ${cslbuilddir}/csl/reduce.fonts/ *] {
-            if {![string equal $f "src"]} {
-                file copy ${cslbuilddir}/csl/reduce.fonts/$f ${fontdir}
-            }
-        }
-        # Resource files
-        xinstall -m 644 -W ${cslbuilddir}/csl/reduce.resources\
-            mma.awk\
-            qepcad.awk ${resourcesdir}
-        # Copy man files
-        xinstall -m 644 ${worksrcpath}/debianbuild/reduce/debian/redcsl.1 ${mandir}
-        # Shell script for running REDUCE
-        xinstall ${filespath}/runcsl.in ${bindir}/redcsl
-        reinplace "s|@LIBEXECDIR@|${prefix}/libexec/${name}|g" ${bindir}/redcsl
-    }
+            ${destroot}${sharedir}                   \
+            ${destroot}${libexecdir}/csl             \
+            ${destroot}${libexecdir}/psl/psl         \
+            ${destroot}${libexecdir}/psl/red         \
+            ${destroot}${applications_dir}/${subport}
+        # Manual files
+        copy                                  \
+            ${filespath}/redcsl.1             \
+            ${filespath}/redpsl.1             \
+            ${genericdir}/newfront/redfront.1 \
+            ${destroot}${mandir}
+        ln -s ${prefix}/share/man/man1/redfront.1 ${destroot}${mandir}/rfcsl.1
+        ln -s ${prefix}/share/man/man1/redfront.1 ${destroot}${mandir}/rfpsl.1
+        # CSL files
+        foreach d {reduce bootstrapreduce csl} {
+            copy ${cslbuilddir}/csl/${d}.app ${destroot}${libexecdir}/csl
+            ln -s ${prefix}/libexec/${subport}/csl/${d}.app \
+                ${destroot}${applications_dir}/${subport}/${d}.app
+        }
+        copy ${filespath}/runcsl.sh ${destroot}${bindir}/redcsl
+        copy ${filespath}/runbootstrapreduce.sh ${destroot}${bindir}/bootstrapreduce
+        copy ${filespath}/runcsllisp.sh ${destroot}${bindir}/csl
+        foreach f {redcsl bootstrapreduce csl} {
+            file attributes ${destroot}${bindir}/${f} -permissions +x
+        }
+        copy ${cslbuilddir}/redfront/rfcsl ${destroot}${bindir}
+        # PSL files
+        copy {*}[glob ${pslbuilddir}/psl/*] ${destroot}${libexecdir}/psl/psl
+        copy {*}[glob ${pslbuilddir}/red/*] ${destroot}${libexecdir}/psl/red
+        copy ${filespath}/runpsl.sh ${destroot}${bindir}/redpsl
+        file attributes ${destroot}${bindir}/redpsl -permissions +x
+        # Note (ha ha) that rfpsl is built in the cslbuild part of the tree.
+        copy ${cslbuilddir}/redfront/rfpsl ${destroot}${bindir}
 }
 
+    # Install licenses and (if +doc) documentation
+    post-destroot {
+        set docsrcdir       ${worksrcpath}/doc/manual
+        set docmiscdir      ${worksrcpath}/doc/misc
 
-subport reduce-psl {
-    license                 BSD
-    revision                0
-    description             ${description} -- PSL version
-
-    long_description        "${long_description}
+        set docbasename     manual
+        set extradocimagebasenames {bild cmsy10 gnuplotex turtleeg}
 
-This installs the Portable Standard Lisp version of REDUCE.
-"
+        set docdir ${prefix}/share/doc/${subport}
+        xinstall -d ${destroot}${docdir}/html ${destroot}${docdir}/pdf
+        xinstall -m 0644 -W ${worksrcpath}/csl/reduce.doc \
+            BINARY-LICENSE.txt                            \
+            BSD-LICENSE.txt                               \
+            LGPL-2.1.txt                                  \
+            ${destroot}${docdir}
 
-    configure.args-append   --with-psl
+        # Install documentation if requested
+        if {[variant_isset doc]} {
+            copy                                \
+                {*}[glob ${docmiscdir}/*.pdf]   \
+                ${docsrcdir}/${docbasename}.pdf \
+                ${destroot}${docdir}/pdf
+            copy                                            \
+                ${docsrcdir}/redlogo.png                    \
+                {*}[glob ${docsrcdir}/${docbasename}*.html] \
+                {*}[glob ${docsrcdir}/${docbasename}*.png]  \
+                ${docsrcdir}/${docbasename}.css             \
+                ${docsrcdir}/index.html                     \
+                ${destroot}${docdir}/html
+            foreach n ${extradocimagebasenames} {
+                copy {*}[glob ${docsrcdir}/${n}*.png] ${destroot}${docdir}/html
+            }
+        }
+    }
 
-    destroot {
-        set builddir [exec ${worksrcpath}/scripts/findhost.sh [exec ${worksrcpath}/config.guess]]
-        set pslbuilddir ${worksrcpath}/pslbuild/${builddir}
-        # create target directories
-        xinstall -d \
-            ${libexecdir}/pslbuild/psl\
-            ${libexecdir}/pslbuild/red\
-            ${sharedir}\
-            ${mandir}
-        ### Create a second image after all (psl) modules have been built, with
-        ### the final value of loaddirectories!*.
-        ui_debug "Saving a new image..."
-        system "cd ${pslbuilddir}; exec ${worksrcpath}/psl/saveimage.sh\
-           ${pslbuilddir} \
-           ${pslbuilddir}/red \
-           ${prefix}/libexec/${name}/pslbuild"
-        # copy PSL files
-        xinstall -p {*}[glob ${pslbuilddir}/psl/*] ${libexecdir}/pslbuild/psl
-        xinstall -p {*}[glob ${pslbuilddir}/red/*] ${libexecdir}/pslbuild/red
-        ln -s ${prefix}/libexec/${name}/pslbuild ${sharedir}/pslbuild
-        # copy man files
-        xinstall -m 644 ${worksrcpath}/debianbuild/reduce/debian/redpsl.1 ${mandir}
-        # shell script for running REDUCE
-        xinstall ${filespath}/runpsl.in ${bindir}/redpsl
-        reinplace "s|@LIBEXECDIR@|${prefix}/libexec/${name}|g" ${bindir}/redpsl
+    variant doc description {Install HTML and PDF documentation} {
+        depends_build-append              \
+            port:dvipng                   \
+            port:texlive-fonts-extra      \
+            port:texlive-formats-extra    \
+            port:texlive-latex-extra      \
+            port:texlive-plain-generic    \
+            port:texlive-bin-extra        \
+            port:texlive-fonts-recommended
     }
+
+    livecheck.version   ${version_dashes}
+    livecheck.url       https://sourceforge.net/projects/reduce-algebra/files/
+    livecheck.regex     {snapshot_(\d{4}-\d{2}-\d{2})}
+} else {
+    livecheck.type      none
 }
 
+subport breduce {
+    license             BSD-2
+    description         A REDUCE batch processing utility.
+    long_description    ${description}
 
-subport reduce-addons {
-    license                 GPL
-    revision                1
-    description             Useful addons for the REDUCE Computer Algebra System
-
-    long_description        "${description}
-These include:
-  reduce-mode, an emacs mode
-  redfront, a frontend with line editing and color output
-  breduce, a REDUCE batch processing utility"
-
-    depends_lib-append      bin:makeinfo:texinfo
-
-    worksrcdir              ${redfrontbuilddir}
-
-    patchfiles-append       patch-src-redline.c.diff
-
-    configure.cmd           ./src/configure
-    configure.args-append\
-        --with-libedit\
-        --with-package-map=${prefix}/share/${name}/resources/package.map\
-        --with-csl-reduce=${prefix}/libexec/${name}/cslbuild/csl/reduce\
-        --with-bpsl=${prefix}/libexec/${name}/pslbuild/psl/bpsl\
-        --with-reduce-img=${prefix}/libexec/${name}/pslbuild/red/reduce.img
+    patchfiles-append   patch-generic-breduce-breduce.1.diff
 
     post-patch {
-        reinplace "s|/usr/share/doc/reduce-addons|${prefix}/share/doc/reduce-addons|g"\
-            ${genericdir}/breduce/breduce.1
+        reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/generic/breduce/breduce.1
     }
 
-    pre-configure {
-        system -W ${redfrontbuilddir} "${build.cmd} maintainer-clean"
-        system -W ${redfrontsrcdir} "autoconf ; autoheader"
-    }
+    configure {}
 
-    post-build {
-        system -W ${genericdir}/emacs "makeinfo reduce-ide.texinfo"
-    }
+    build {}
+
+    depends_run         path:bin/latex:texlive \
+                        port:reduce
 
     destroot {
-        # create target directories
-        xinstall -d \
-            ${addondocdir}\
-            ${mandir}\
-            ${sitelispdir}
-        # copy addons
-        xinstall -m 644 ${redfrontsrcdir}/README ${addondocdir}
-        xinstall -m 644 -W ${genericdir}/breduce\
+        set libexecdir  ${prefix}/libexec/${subport}
+        set mandir      ${prefix}/share/man/man1
+        xinstall -d ${destroot}/${libexecdir}
+        copy ${worksrcpath}/generic/breduce/breduce ${destroot}/${libexecdir}
+        copy ${worksrcpath}/packages/breduce/breduce.red ${destroot}/${libexecdir}
+        ln -s ${prefix}/libexec/breduce/breduce ${destroot}${prefix}/bin/breduce
+        copy ${worksrcpath}/generic/breduce/breduce.1 ${destroot}/${mandir}
+    }
+
+    post-destroot {
+        set docdir ${prefix}/share/doc/${subport}
+        xinstall -d ${destroot}${docdir}
+        xinstall -m 0644 -W ${worksrcpath}/generic/breduce \
             breduce.bbl\
             breduce.pdf\
-            breduce.tex ${addondocdir}
-        xinstall -m 644 ${redfrontsrcdir}/redfront.1 ${mandir}
-        ln -s ${prefix}/share/man/man1/redfront.1 ${mandir}/rfcsl.1
-        ln -s ${prefix}/share/man/man1/redfront.1 ${mandir}/rfpsl.1
-        xinstall -m 644 ${genericdir}/breduce/breduce.1 ${mandir}
-        xinstall ${redfrontbuilddir}/redfront ${bindir}
-        ln -s ${prefix}/bin/redfront ${bindir}/rfcsl
-        ln -s ${prefix}/bin/redfront ${bindir}/rfpsl
-        xinstall ${genericdir}/breduce/breduce ${bindir}
-        xinstall -m 644 -W ${genericdir}/emacs\
-            reduce-ide.info\
-            reduce-ide.texinfo ${addondocdir}
-        xinstall -m 644 -W ${genericdir}/emacs \
-            reduce-mode.el \
-            reduce-run.el \
-            ${sitelispdir}
-    }
-
-    notes-append    "
-REDUCE edit mode is provided by a file called 'reduce-mode.el'. This file\
-is installed in ${prefix}/share/emacs/site-lisp/${name}. To automatically\
-load this file, put the following lines in your '.emacs' file:
-
-\(autoload ’reduce-mode \"reduce-mode\" \"Major mode for REDUCE code editing\" t)
-
-;; Automatically start .red files in reduce mode.
-\(add-to-list ’auto-mode-alist ’(\"\\\.red\\\’\" . reduce-mode))
-
-Installation of REDUCE run mode is documented separately. See\
-'${prefix}/share/doc/reduce-addons/reduce-ide.info'.
-"
+            breduce.tex                                    \
+            ${destroot}${docdir}
+    }
 }
