# -*- 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           texlive         1.0
PortGroup           xcodeversion    1.0
PortGroup           wxWidgets       1.0

name                gnuplot
version             5.4.1
revision            3

categories          math science
# the license has some inconvenient requirements that we're not meeting
# to be allowed to distribute binaries
license             Restrictive
# luckily nothing has to link against this
installs_libs       no
platforms           darwin
maintainers         {mojca @mojca} openmaintainer

description         A command-driven interactive function plotting program

long_description    Gnuplot is a command-driven interactive function \
                    plotting program. Plots any number of functions, \
                    built up of C operators, C library functions, and \
                    some things C does not have like **, sgn(), etc. \
                    Also support for plotting data files, to compare \
                    actual data to theoretical curves.

homepage            http://gnuplot.sourceforge.net/
master_sites        sourceforge:project/gnuplot/gnuplot/${version}
dist_subdir         ${name}/${version}

checksums           rmd160  0ccca1f34480fd3bcae3012c200e8f534010c5e4 \
                    sha256  6b690485567eaeb938c26936e5e0681cf70c856d273cc2c45fabf64d8bc6590e \
                    size    5608076

depends_build       port:pkgconfig

depends_lib         port:expat \
                    port:fontconfig \
                    port:gd2 \
                    port:webp \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    path:lib/libcaca.dylib:libcaca \
                    port:libcerf \
                    port:libiconv \
                    port:libpng \
                    port:ncurses \
                    port:readline \
                    port:zlib

minimum_xcodeversions \
                    {9 3.1}

patchfiles          patch-configure.diff

# If the compiler defaults to C++20, a header file `version` exists.
# `-I$(top_builddir)` will find the local file VERSION instead.
patchfiles-append   patch-cxx20.diff

configure.args      --with-caca=${prefix} \
                    --with-gd=${prefix} \
                    --with-readline=${prefix} \
                    --without-aquaterm \
                    --without-x \
                    --infodir='\${prefix}/share/info' \
                    --mandir='\${prefix}/share/man' \
                    --disable-wxwidgets \
                    --without-bitmap-terminals \
                    --without-cairo \
                    --without-latex \
                    --without-lua \
                    --without-qt \
                    --without-tutorial \
                    --with-cwdrc

variant aquaterm description "Enable AquaTerm terminal" {
    depends_lib-append          port:aquaterm
    configure.args-replace      --without-aquaterm --with-aquaterm
    configure.cflags-append     "-F${frameworks_dir}"
    configure.ldflags-append    "-F${frameworks_dir}"
}

variant x11 {
    configure.args-replace      --without-x --with-x
    depends_lib-append          port:xpm
}

variant luaterm description "Enable lua-based terminals" {
    depends_lib-append          port:lua
    configure.args-replace      --without-lua --with-lua
}

variant pangocairo description "Enable cairo-based terminals" {
    configure.args-delete       --without-cairo
}

variant wxwidgets description "Enable wxt terminal" {
    wxWidgets.use               wxWidgets-3.0
    depends_lib-append          port:${wxWidgets.port}
    configure.args-replace      --disable-wxwidgets --with-wx=${wxWidgets.wxdir}
}

variant qt conflicts qt5 description "Enable qt terminal with Qt 4" {
    PortGroup qt4 1.0

    configure.args-replace      --without-qt --with-qt=qt4
}

variant qt5 conflicts qt description "Enable qt terminal with Qt 5" {
    # Latest versions of Qt require C++11
    PortGroup qt5   1.0

    compiler.cxx_standard       2011
    configure.cxxflags-append   -std=c++11

    qt5.depends_component       qtbase   \
                                qtsvg    \
                                qttools

    configure.args-replace      --without-qt --with-qt=qt5
}


# 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'.
variant old_bitmap_terminals description "Enable PBM (Portable Bit Map) and other older bitmap terminals" {
    configure.args-replace      --without-bitmap-terminals --with-bitmap-terminals
}

default_variants                +aquaterm +luaterm +pangocairo +wxwidgets +x11

if {[variant_isset pangocairo] || [variant_isset wxwidgets]} {
    depends_lib-append          path:lib/pkgconfig/pango.pc:pango
}

# Building and installing TeX macro files for TikZ could/should be done by gnuplot's Makefile,
# but without a patch that would require a dependency on texlive.
# Apart from that the files would fly to the wrong place in TDS,
# so that plain TeX and ConTeXt could not find them (an upstream bug).
# So we 'manually' generate and copy the necessary files.
post-build {
    if {[variant_isset luaterm]} {
        system -W ${worksrcpath}/share/LaTeX "${prefix}/bin/lua ../../term/lua/gnuplot-tikz.lua style"
    }
}

post-destroot {
    if {[variant_isset luaterm]} {
        set texmf ${destroot}${texlive_texmfports}
        set luadir ${worksrcpath}/share/LaTeX
        xinstall -d -m 0755 ${texmf}/tex/context/third/gnuplot-lua-tikz
        xinstall -d -m 0755 ${texmf}/tex/generic/gnuplot-lua-tikz
        xinstall -d -m 0755 ${texmf}/tex/latex/gnuplot-lua-tikz
        xinstall -d -m 0755 ${texmf}/tex/plain/gnuplot-lua-tikz

        xinstall -m 0644 ${luadir}/t-gnuplot-lua-tikz.tex      ${texmf}/tex/context/third/gnuplot-lua-tikz
        xinstall -m 0644 ${luadir}/gnuplot-lua-tikz-common.tex ${texmf}/tex/generic/gnuplot-lua-tikz
        xinstall -m 0644 ${luadir}/gnuplot-lua-tikz.sty        ${texmf}/tex/latex/gnuplot-lua-tikz
        xinstall -m 0644 ${luadir}/gnuplot-lua-tikz.tex        ${texmf}/tex/plain/gnuplot-lua-tikz
    }

    set destdocdir ${destroot}${prefix}/share/doc/${name}
    xinstall -d -m 0755 ${destdocdir}
    xinstall -m 0644 ${worksrcpath}/docs/${name}.pdf ${destdocdir}

    # copy demo files
    file copy ${worksrcpath}/demo ${destdocdir}
    foreach {makefile} {Makefile Makefile.in Makefile.am Makefile.am.in} {
        delete ${destdocdir}/demo/${makefile}
    }
}
