# -*- 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
# $Id: Portfile 120167 2014-05-17 21:10:28Z ryandesign@macports.org $

PortSystem                  1.0
PortGroup                   elisp           1.0
PortGroup                   texlive         1.0
PortGroup                   xcodeversion    1.0
PortGroup                   wxWidgets       1.0

name                        gnuplot
version                     4.6.5
revision                    1
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 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}

distfiles-append            ${name}.pdf
extract.only                ${distname}.tar.gz
checksums                   ${distname}.tar.gz \
                            rmd160  8073289c066037664fe814d96d441630f313656f \
                            sha256  e550f030c7d04570e89c3d4e3f6e82296816508419c86ab46c4dd73156519a2d \
                            ${name}.pdf \
                            rmd160  885ad93e408d1eca5697d8b17ecd2da410e70dae \
                            sha256  4059a37d635079534ec8c9e9710485d9a1f048e9a75bef197510c9a2e95f8c7c

depends_build               path:bin/pkg-config:pkgconfig

depends_lib                 port:expat \
                            port:fontconfig \
                            port:gd2 \
                            port:jpeg \
                            port:libcerf \
                            port:libiconv \
                            port:libpng \
                            port:ncurses \
                            port:readline \
                            port:zlib

patchfiles                  patch-configure-aquaterm.diff

minimum_xcodeversions       {9 3.1}

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

variant aquaterm description "Enable AquaTerm terminal" {
    depends_lib-append      port:aquaterm
    configure.args-delete   --without-aquaterm
    configure.args-append   --with-aquaterm=${frameworks_dir}
}

variant x11 {
    configure.args-delete   --without-x
    configure.args-append   --with-x
    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
    configure.args-append   --with-lua
}

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

variant pdflib description "Enable pdflib-based pdf terminal" {
    depends_lib-append      port:pdflib
    configure.args-replace  --without-pdf --with-pdf=${prefix}
}

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

# remove in September 2014
variant wxwidgets30     description "Legacy compatibility variant" {}
variant wxwidgets_devel description "Legacy compatibility variant" {}

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

default_variants            +aquaterm +luaterm +pangocairo +wxwidgets +x11

if {[variant_isset wxwidgets30] || [variant_isset wxwidgets_devel]} {
    default_variants-append +wxwidgets
}

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 ${distpath}/${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}
    }
}
