# -*- 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 92122 2012-04-19 13:44:29Z nicos@macports.org $

PortSystem  1.0
PortGroup           qt4 1.0

name                qtiplot
version             0.9.8.9
revision            1
categories          aqua science
license             GPL-2+
maintainers         nicos 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.

platforms           darwin
homepage            http://soft.proindependent.com/qtiplot.html
master_sites        http://download.berlios.de/qtiplot/

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

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

patchfiles          patch-ApplicationWindow.diff \
                    patch-qti.sip.diff \
                    patch-scripting.pri.diff \
                    patch-qwt3d_openglhelper.diff

#Workaround as qtiplot build-phase hangs indefinitely on 
#Lion (ticket #30761)
platform darwin 11 {
    patchfiles-append   patch-disable-O2.diff
}

use_zip             yes

universal_variant   no

use_parallel_build  yes

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 python25]} {
       puts $confFD "PYTHON = ${prefix}/bin/python2.5"
   }
   if {[variant_isset python26]} {
       puts $confFD "PYTHON = ${prefix}/bin/python2.6"
   }
   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 python25] || [variant_isset python26] || [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
    }

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

configure.pre_args  "PREFIX=${prefix}"
configure.cmd   "LOCALSOFT=${prefix} ${qt_qmake_cmd}"
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}
    # in this dir python tries to store .pyc files, so it has to be writable
    file attributes ${destroot}${applications_dir}/qtiplot.app/Contents/MacOS -permissions ugo+w
    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
}

variant python25 conflicts python26 python27 description "add support for python26 scripting" {
    depends_lib-append port:python25 \
                       port:py25-sip \
                       port:py25-pyqt4
}

variant python26 conflicts python25 python27 description "add support for python26 scripting" {
    depends_lib-append port:python26 \
                       port:py26-sip \
                       port:py26-pyqt4
}

variant python27 conflicts python25 python26 description "add support for python26 scripting" {
    depends_lib-append port:python27 \
                       port:py27-sip \
                       port:py27-pyqt4
}
