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

name                qtiplot
version             0.9.8.9
revision            22
categories          aqua science
license             GPL-2+
maintainers         {nicos @NicosPavlov} openmaintainer

description         Data Analysis and Scientific Plotting
long_description    Similar to Origin or SigmaPlot, QtiPlot can be used to \
                    present 2D and 3D data and has various data analysis functions \
                    like curve fitting. Plotting of 3D data can be rendered using \
                    OpenGL using the Qwt3D libraries.\
                    It is a full-featured data analysis and plotting package available \
                    on Linux, Windows and Mac OS X platforms that supports python scripting.

homepage            http://www.qtiplot.com/
master_sites        sourceforge:${name}.berlios
use_zip             yes

depends_lib-append  port:muparser \
                    port:gsl \
                    port:libpng \
                    port:zlib

checksums           ${name}-${version}.zip \
                    rmd160  d0ae793b4fbf6eab723f049493d6d954ee8b747e \
                    sha256  3d2cc2503c8f3d4e72af904fd6c8a528f3c38dcfe5eeca5dae64324db9f12d38 \
                    size    17466196

# gsl patch from https://packages.debian.org/sid/math/qtiplot
patchfiles          patch-ApplicationWindow.diff \
                    patch-qti.sip.diff \
                    patch-scripting.pri.diff \
                    patch-qwt3d_openglhelper.diff \
                    patch-gsl2.diff \
                    patch-sip-4.15.diff \
                    patch-comparison.diff \
                    patch-boolean.diff \
                    patch-unsetflags.diff

# error: unknown type name 'constexpr'
compiler.cxx_standard   2014
configure.cxxflags-append -std=c++14

if {[string match *clang* ${configure.compiler}]} {
    # qwt3d_lighting.cpp: error: non-constant-expression cannot be narrowed
    # from type 'double' to 'GLfloat' (aka 'float') in initializer list [-Wc++11-narrowing]
    configure.cxxflags-append -Wno-c++11-narrowing
}

pre-configure {
    # don't step into the manual dir, we don't have all the tools to build it
    reinplace "/manual/d" ${worksrcpath}/qtiplot.pro

    # create the configuration, see build.conf.example which comes with the package
    set confFD [open ${worksrcpath}/build.conf "w"]
    puts $confFD "SYS_INCLUDEPATH = ${prefix}/include"
    # snow leopard seems to need zlib explicitly, see #23272
    puts $confFD "SYS_LIBS = -L${prefix}/lib -lz"
    puts $confFD "MUPARSER_LIBS = -lmuparser"
    puts $confFD "GSL_LIBS = -lgsl -lgslcblas"
    puts $confFD "BOOST_LIBS = -lboost_date_time-mt -lboost_thread-mt"
    # till upstream catches up, we use a local copy of qwt
    puts $confFD "QWT_INCLUDEPATH = \$\$QTI_ROOT/3rdparty/qwt/src"
    puts $confFD "QWT_LIBS = \$\$QTI_ROOT/3rdparty/qwt/lib/libqwt.a"
    puts $confFD "QWT3D_INCLUDEPATH = \$\$QTI_ROOT/3rdparty/qwtplot3d/include"
    puts $confFD "QWT3D_LIBS = \$\$QTI_ROOT/3rdparty/qwtplot3d/lib/libqwtplot3d.a"
    puts $confFD "LIBPNG_LIBS = -lpng"
    # Forcing TamuAnova to nothing
    puts $confFD "TAMUANOVA_LIBS = "
    if {[variant_isset qtexengine]} {
      puts $confFD "TEX_ENGINE_INCLUDEPATH = \$\$QTI_ROOT/3rdparty/QTeXEngine/src"
      puts $confFD "TEX_ENGINE_LIBS = \$\$QTI_ROOT/3rdparty/QTeXEngine/libQTeXEngine.a"
    }

   if {[variant_isset python27]} {
       puts $confFD "PYTHON = ${prefix}/bin/python2.7"
   }

    # target specific configuration
    puts $confFD "contains( TARGET, qtiplot ) \{"
    puts $confFD "SCRIPTING_LANGS += muParser"; # currently you can't build without
    if {[variant_isset python27]} {
        puts $confFD "SCRIPTING_LANGS += Python"
    }
    puts $confFD "DEFINES += SCRIPTING_CONSOLE"
    puts $confFD "DEFINES += SCRIPTING_DIALOG"
    puts $confFD "CONFIG += release"
    puts $confFD "\}"
    close $confFD

    # fix a bug in the python init script
    # append the current path *before* import __main__, else __file__ points to math.so after the import if this module
    reinplace "/import __main__/ i\\
    import sys, os.path\\
    sys.path.append(os.path.dirname(__file__))
    " ${worksrcpath}/qtiplot/qtiplotrc.py

    if {[variant_isset qtexengine]} {
      # prepare qtexengine
      copy ${workpath}/QTeXEngine ${worksrcpath}/3rdparty/QTeXEngine

      # add 3rdparty to compilation
      reinplace "s|3rdparty/qwtplot3d \|3rdparty/qwtplot3d 3rdparty/QTeXEngine \|g" \
      ${worksrcpath}/qtiplot.pro

      # suppress examples which do not compile
      reinplace "/example/d" ${worksrcpath}/3rdparty/QTeXEngine/QTeXEngine.pro
      reinplace "/test/d" ${worksrcpath}/3rdparty/QTeXEngine/QTeXEngine.pro
    }

    # use system-wide libraries
    reinplace "s|#unix|unix|g" ${worksrcpath}/qtiplot/qtiplot.pro

    # force use of zlib in qwtplot3d
    reinplace "s|GL2PS_HAVE_LIBPNG|GL2PS_HAVE_ZLIB GL2PS_HAVE_LIBPNG|g" \
    ${worksrcpath}/3rdparty/qwtplot3d/qwtplot3d.pro
}

destroot.destdir INSTALL_ROOT=${destroot}

destroot {
    # install the application bundle and create a symlink for cli invocation
    xinstall -d ${destroot}${applications_dir}
    copy "${worksrcpath}/qtiplot/qtiplot.app" ${destroot}${applications_dir}/QtiPlot.app
    ln -s ${applications_dir}/QtiPlot.app/Contents/MacOS/qtiplot ${destroot}${prefix}/bin/qtiplot
    # install some resources
    xinstall -W ${worksrcpath}/qtiplot/ \
        qti_wordlist.txt \
        qtiUtil.py \
        qtiplotrc.py \
        ${destroot}${applications_dir}/QtiPlot.app/Contents/MacOS/
    # install the fitting plugins
    set plugin_dir ${destroot}${applications_dir}/QtiPlot.app/Contents/fitPlugins/
    file mkdir ${plugin_dir}
    xinstall -W ${worksrcpath}/fitPlugins/ \
        libexp_saturation.dylib \
        libexplin.dylib \
        libfitRational0.dylib \
        libfitRational1.dylib \
        libplanck_wavelength.dylib \
        ${plugin_dir}
}

variant qtexengine description "add support for tex exportation" {
    distfiles-append  QTeXEngine-0.3-opensource.zip
    checksums-append  QTeXEngine-0.3-opensource.zip \
                      rmd160  6f75f8e6355515cf5eb8a7b90b9acca8aa889129 \
                      sha256  fc60c18bd0af5947d2d7dbc3d1b5b16ed251d9f317cc548228347f081a0b67d5 \
                      size    146118
}

variant python27 description "add support for python27 scripting" {

    post-destroot {
        system "${prefix}/bin/python2.7 -m compileall ${destroot}${applications_dir}/QtiPlot.app/Contents/MacOS"
    }

    depends_lib-append port:python27 \
                       port:py27-sip4 \
                       port:py27-pyqt4
}

livecheck.type  none
