# -*- 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 107537 2013-07-02 16:49:01Z mojca@macports.org $

PortSystem                  1.0
PortGroup                   xcodeversion 1.0
PortGroup                   texlive 1.0
PortGroup                   elisp 1.0

name                        gnuplot
version                     4.6.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 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  42ff3a2066e5089b19c52a88cd2c17d9db87681d \
                            sha256  df5ffafa25fb32b3ecc0206a520f6bca8680e6dcc961efd30df34c0a1b7ea7f5 \
                            ${name}.pdf \
                            rmd160  91c0832dc41f6e08fd0f67264e710d8afdc088bc \
                            sha256  3da1b3f5c435d3e1d770081f09ab6b77432f20284be199d0fee0cda4b6abe269

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

patchfiles                  patch-src-variable.c.diff \
                            patch-configure-aquaterm.diff

minimum_xcodeversions       {9 3.1}

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

configure.ccache            no

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 conflicts no_x11 {
    configure.args-delete   --without-x
    configure.args-append   --with-x
    depends_lib-append      port:xpm
}
# remove this in August 2013
variant no_x11 conflicts x11 description {Legacy compatibility variant} {}
if {[variant_isset no_x11]} {
    default_variants -x11
} else {
    default_variants +x11
}

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" {
    depends_lib-append      path:lib/pkgconfig/pango.pc:pango
    configure.args-delete   --without-cairo
}

# One can in principle choose between wxWidgets, wxwidgets30 and wxWidgets-devel,
# but only 2.9 allows building 64-bit binaries.
# If it is acceptable to depend on wxWidgets-devel only,
# one could delete the variant that depends on wxWidgets 2.8.
variant wxwidgets conflicts wxwidgets30 wxwidgets_devel description "Enable wxt terminal (wxWidgets 2.8, 32-bit only)" {
    depends_lib-append      port:wxWidgets path:lib/pkgconfig/pango.pc:pango
    configure.args-delete   --disable-wxwidgets
    configure.args-append   --with-wx-config=${prefix}/bin/wx-config
}
if {[variant_isset wxwidgets]} {
    # wxWidgets is not universal and is 32-bit only
    universal_variant       no
    supported_archs         i386 ppc
}

variant wxwidgets30 conflicts wxwidgets wxwidgets_devel description "Enable wxt terminal (using wxWidgets 2.9, supports 64-bit)" {
    depends_lib-append      port:wxWidgets30 path:lib/pkgconfig/pango.pc:pango
    configure.args-delete   --disable-wxwidgets
    configure.args-append   --with-wx-config=${prefix}/bin/wx-config
}

variant wxwidgets_devel conflicts wxwidgets wxwidgets30 description "Enable wxt terminal (using wxWidgets devel, supports 64-bit)" {
    depends_lib-append      port:wxWidgets-devel path:lib/pkgconfig/pango.pc:pango
    configure.args-delete   --disable-wxwidgets
    configure.args-append   --with-wx-config=${prefix}/bin/wx-config
}

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 +x11

# 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 ${workpath}/${distname}/share/LaTeX "${prefix}/bin/lua ../../term/lua/gnuplot-tikz.lua style"
    }
}

post-destroot {
    if {[variant_isset luaterm]} {
        set texmf ${destroot}${texlive_texmfports}
        set luadir ${workpath}/${distname}/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
    }

    xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 ${distpath}/${name}.pdf \
        ${destroot}${prefix}/share/doc/${name}
}

# this temporary workaround removes files that accidentally circumvented destroot
# and were installed straight into ${prefix}/share/texmf/tex/latex/gnuplot
#
# it may be removed in August 2013; it is only a problem for those who installed
# with +tutorial
pre-activate {
    if [file exists ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot.cfg] {
        delete ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot.cfg
    }
    if [file exists ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot-lua-tikz.sty] {
        delete ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot-lua-tikz.sty
    }

    # only since 4.6.0
    if [file exists ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot-lua-tikz.tex] {
        delete ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot-lua-tikz.tex
    }
    if [file exists ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot-lua-tikz-common.tex] {
        delete ${prefix}/share/texmf/tex/latex/gnuplot/gnuplot-lua-tikz-common.tex
    }
    if [file exists ${prefix}/share/texmf/tex/latex/gnuplot/t-gnuplot-lua-tikz.tex] {
        delete ${prefix}/share/texmf/tex/latex/gnuplot/t-gnuplot-lua-tikz.tex
    }
}

livecheck.url http://sourceforge.net/projects/gnuplot/files/
livecheck.regex "gnuplot/.*/gnuplot-(.*)[quotemeta ${extract.suffix}]"
