# -*- 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.1.1
revision            1
categories-append   graphics math
platforms           darwin
license             {PSF BSD}

python.versions     27 34 35 36 37

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/
master_sites        pypi:[string index ${python.rootname} 0]/${python.rootname}
distname            ${python.rootname}-${version}

checksums           rmd160  af7de250fbfe2ca051d31d7886b14d62396880ce \
                    sha256  1febd22afe1489b13c6749ea059d392c03261b2950d1d45c17e3aed812080c93 \
                    size    37793603

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

    depends_lib-append  port:freetype \
                        port:libpng \
                        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}-pyobjc-cocoa

    patchfiles          patch-v31-setup.cfg.diff \
                        patch-v31-src-macosx.m.diff \
                        patch-v31-qhull.diff \
                        patch-v31-setupext.py.diff \
                        patch-v31-jquery-ui.diff

    if {${python.version} in "27 34"} {
        version         2.2.4
        revision        0
        distname        ${python.rootname}-${version}
        checksums       rmd160  dc047125cdedb6371d400648059d70851e05ffc1 \
                        sha256  029620799e581802961ac1dcff5cb5d3ee2f602e0db9c0f202a90495b37d2126 \
                        size    36974286

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

        patchfiles          patch-v2-setup.cfg.diff \
                            patch-v2-src-macosx.m.diff \
                            patch-v2-qhull.diff \
                            patch-v2-setupext.py.diff \
                            patch-v2-jquery-ui.diff
    }

    if {${python.version} eq 27} {
        depends_lib-append  port:py27-backports-functools_lru_cache \
                            port:py27-subprocess32
    }

    if {${python.version} eq 35} {
        version         3.0.3
        revision        0
        distname        ${python.rootname}-${version}
        checksums       rmd160  98ecd1ca25d555bde5d43fcaef800f7436d7d738 \
                        sha256  e1d33589e32f482d0a7d1957bf473d43341115d40d33f578dad44432e47df7b7 \
                        size    36640137

        patchfiles      patch-v30-setup.cfg.diff \
                        patch-v30-src-macosx.m.diff \
                        patch-v30-qhull.diff \
                        patch-v30-setupext.py.diff \
                        patch-v30-jquery-ui.diff
    }

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

    post-patch {
        reinplace "s|@@PREFIX@@|${prefix}|" ${worksrcpath}/setup.cfg
    }

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

    # 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.
    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 -m 0755 -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
"
}