# -*- 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
PortGroup           python 1.0

name                py-matplotlib
version             3.4.1
revision            0

categories-append   graphics math
platforms           darwin
license             {PSF BSD}
maintainers         {reneeotten @reneeotten} openmaintainer

description         Matplotlib is a python plotting library
long_description    Matplotlib strives to produce publication quality 2D \
                    graphics for interactive graphing, scientific publishing, \
                    user interface development and web application servers \
                    targeting multiple user interfaces and hardcopy output \
                    formats. There is a 'pylab' mode which emulates matlab \
                    graphics. The library uses numpy for handling large data \
                    sets and supports a variety of output backends. This port \
                    provides variants for the different GUIs (gtk2, gtk3, \
                    tkinter, qt4, qt5, cairo, latex).

homepage            https://matplotlib.org/

checksums           rmd160  494973d4d5fd80a40766b92ccdff78f3b851ec9c \
                    sha256  84d4c4f650f356678a5d658a43ca21a41fca13f9b8b00169c0b76e6a6a948908 \
                    size    37253296

use_parallel_build  no

python.versions     27 35 36 37 38 39

if {${name} ne ${subport}} {
    depends_build-append \
                    port:pkgconfig \
                    port:py${python.version}-certifi \
                    port:py${python.version}-setuptools

    depends_lib-append \
                    port:freetype \
                    port:qhull \
                    port:py${python.version}-cycler \
                    port:py${python.version}-dateutil \
                    port:py${python.version}-kiwisolver \
                    port:py${python.version}-numpy \
                    port:py${python.version}-parsing \
                    port:py${python.version}-Pillow

    patchfiles      patch-v34-setup.cfg.diff \
                    patch-v34-src-macosx.m.diff

    if {${python.version} eq 27} {
        version     2.2.5
        revision    2
        checksums   rmd160  4532a205e8f40d6f40346b2e461d3dca144b38b9 \
                    sha256  a3037a840cd9dfdc2df9fee8af8f76ca82bfab173c0f9468193ca7a89a2b60ea \
                    size    36678633

        depends_lib-append \
                    port:py${python.version}-backports-functools_lru_cache \
                    port:py${python.version}-six \
                    port:py${python.version}-subprocess32 \
                    port:py${python.version}-tz

        patchfiles  patch-v2-setup.cfg.diff \
                    patch-v2-qhull.diff \
                    patch-v2-setupext.py.diff \
                    patch-v2-jquery-ui.diff
    } elseif {${python.version} eq 35} {
        version     3.0.3
        revision    3
        checksums   rmd160  98ecd1ca25d555bde5d43fcaef800f7436d7d738 \
                    sha256  e1d33589e32f482d0a7d1957bf473d43341115d40d33f578dad44432e47df7b7 \
                    size    36640137

        patchfiles  patch-v30-setup.cfg.diff \
                    patch-v30-qhull.diff \
                    patch-v30-setupext.py.diff \
                    patch-v30-jquery-ui.diff
    } elseif {${python.version} eq 36} {
        version     3.3.4
        revision    0
        checksums   rmd160  3d579c5c5f8734d7199be8880dfac4eec4248e89 \
                    sha256  3e477db76c22929e4c6876c44f88d790aacdf3c3f8f3a90cb1975c0bf37825b0 \
                    size    37941665

        patchfiles  patch-v33-setup.cfg.diff \
                    patch-v33-src-macosx.m.diff
    }

    # fixup dependencies for version <= 3.3.0
    if {${python.version} in "27 35"} {
        depends_lib-replace \
                    port:py${python.version}-Pillow \
                    port:libpng

        depends_build-delete \
                    port:py${python.version}-certifi
    }

    build.env       PKG_CONFIG_PATH=${python.prefix}/lib/pkgconfig

    # build fails with gcc-4.0 on Leopard, use gcc-4.2 (#37069)
    compiler.blacklist gcc-4.0

    # additionally on Leopard the macosx backend cannot be compiled anymore, see Trac ticket:
    # https://trac.macports.org/ticket/61757
    if {${os.platform} eq "darwin" && ${os.major} < 10} {
        post-patch {
            reinplace "s|^macosx=True|macosx=False|" ${worksrcpath}/setup.cfg
        }
    }

    if {${python.version} in "27 35"} {
        post-patch {
            reinplace "s|@@PREFIX@@|${prefix}|" ${worksrcpath}/setup.cfg
        }

        # jquery-ui is included in the PyPI files, patch added for setup.py that avoids trying
        # to install if from the cache or download from URL. Just copy the directory in the post-
        # build phase; only needed for versions < 3.3.0 !
        post-build {
            set libdir [glob -nocomplain ${worksrcpath}/build/lib*]
            if {![file exists ${libdir}/matplotlib/backends/web_backend/jquery-ui-1.12.1]} {
                file copy ${worksrcpath}/lib/matplotlib/backends/web_backend/jquery-ui-1.12.1 \
                    ${libdir}/matplotlib/backends/web_backend/ \
            }
        }
    }

    post-destroot {
        xinstall -d ${destroot}${prefix}/share/doc/${subport} \
            ${destroot}${prefix}/share/${subport}
        xinstall -m 0644 -W ${worksrcpath} README.rst matplotlibrc.template \
            ${destroot}${prefix}/share/doc/${subport}
        file copy ${worksrcpath}/examples \
            ${destroot}${prefix}/share/${subport}
    }

    variant webagg description "Enable WebAgg backend" {
        depends_lib-append  port:py${python.version}-tornado
    }

    variant tkinter description "Enable TkAgg backend" {
        depends_lib-append  port:py${python.version}-tkinter
        post-patch {
            reinplace "s|^tkagg=False|tkagg=True|" ${worksrcpath}/setup.cfg
        }
    }

    if {${python.version} eq 27} {
        variant gtk2 description "Enable GTKAgg backend" {
            depends_lib-append  port:py${python.version}-pygtk
            post-patch {
                reinplace "s|^gtk=False|gtk=True|" ${worksrcpath}/setup.cfg
                reinplace "s|^gtkagg=False|gtkagg=True|" ${worksrcpath}/setup.cfg
            }
        }
    }

    variant gtk3 description "Enable GTK3Agg backend" {
        depends_lib-append  port:py${python.version}-gobject3 port:gtk3
        post-patch {
            reinplace "s|^gtk3agg=False|gtk3agg=True|" ${worksrcpath}/setup.cfg
        }
    }

    variant qt4 description "Enable Qt4Agg backend" {
        depends_lib-append  port:py${python.version}-pyqt4
        post-patch {
            reinplace "s|^qt4agg=False|qt4agg=True|" ${worksrcpath}/setup.cfg
        }
    }

    variant qt5 description "Enable Qt5Agg backend" {
        depends_lib-append  port:py${python.version}-pyqt5
        post-patch {
            reinplace "s|^qt5agg=False|qt5agg=True|" ${worksrcpath}/setup.cfg
        }
    }

    variant pyside description "Enable PySide backend" {
        depends_lib-append  port:py${python.version}-pyside
        post-patch {
            reinplace "s|^pyside=False|pyside=True|" ${worksrcpath}/setup.cfg
        }
    }

    variant cairo description "Enable Cairo backends" {
        depends_lib-append  port:py${python.version}-cairo
        post-patch {
            reinplace "s|^cairo=False|cairo=True|" ${worksrcpath}/setup.cfg
        }
    }

    variant dvipng description "Enable dvipng support" {
        depends_run-append  port:dvipng
    }

    variant ghostscript description "Enable GhostScript support" {
        depends_run-append  port:ghostscript
    }

    variant latex description "Enable LaTeX support" {
        depends_run-append  bin:latex:texlive
    }

    variant pdftops description "Enable pdftops support" {
        depends_run-append  bin:pdftops:poppler
    }

    default_variants +webagg

    if {![variant_isset gtk2] && ![variant_isset tkinter] && ![variant_isset qt5] &&
        ![variant_isset qt4] && ![variant_isset pyside]} {
            default_variants-append +cairo
    }

    if {[variant_isset gtk3] && [variant_isset cairo]} {
        post-patch {
            reinplace "s|^gtk3cairo=False|gtk3cairo=True|" ${worksrcpath}/setup.cfg
        }
    }

    livecheck.type  none

    notes "The default backend is the interactive Mac OS X backend. Different\
        backends can be specified using the ~/.matplotlib/matplotlibrc file.\
        More details regarding backends can be found in the matplotlib FAQ:

        https://matplotlib.org/tutorials/introductory/usage.html#backends"
}
