# -*- 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 79078 2011-06-01 15:46:04Z macsforever2000@macports.org $

PortSystem  1.0
PortGroup           qt4 1.0

name                qtiplot
version             0.9.8.3
revision            2
categories          aqua science
maintainers         jonas 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/

distfiles           ${distname}.zip \
                    QTeXEngine-0.2-opensource.zip \
                    liborigin2-13092010.zip

depends_lib-append  path:bin/qmake-mac:qt4-mac \
                    port:muparser \
                    port:boost \
                    port:gsl \
                    port:libpng \
                    port:zlib

checksums           qtiplot-${version}.zip \
                    md5     c90e9dc08e03dc1cd0db19dfe8941f0c \
                    sha1    35dec539fb20b8c7ca121cca11f7b800b4ff0c6b \
                    rmd160  2b0fd5b710d2752a1a413f83cbee9daf1e88edbf \
                    QTeXEngine-0.2-opensource.zip \
                    md5     fc2fe7f3fdb9e06c00d1f5c3c3e68920 \
                    sha1    b484a4da7a0f3103e188539bfa6c1910dc9b55ca \
                    rmd160  76ff1f2f96cd6df0ea5e6965013e0db452c55f09 \
                    liborigin2-13092010.zip \
                    md5     e2b41c4d8a1e2d357dd2b5b331d31375 \
                    sha1    3f17d5a8c484ddb864a29a91a94d677b0b3b18d7 \
                    rmd160  df7f4bafc1b9815c5921c09b435f431d8a1947e6

patchfiles          patch-ApplicationWindow.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 "unix:QWT3D_LIBS = \$\$QTI_ROOT/3rdparty/qwtplot3d/lib/libqwtplot3d.a"
    puts $confFD "LIBPNG_LIBS = -lpng"
    puts $confFD "TEX_ENGINE_INCLUDEPATH = \$\$QTI_ROOT/3rdparty/QTeXEngine/src"
    puts $confFD "TEX_ENGINE_LIBS = \$\$QTI_ROOT/3rdparty/QTeXEngine/libQTeXEngine.a"
    if {[variant_isset origin]} {
      puts $confFD "LIBORIGIN_INCLUDEPATH = \$\$QTI_ROOT/3rdparty/liborigin2"
      puts $confFD "LIBORIGIN_LIBS = \$\$QTI_ROOT/3rdparty/liborigin2/liborigin2.a"
    }

    # linking to deprecated library
    reinplace "s|<QAssistantClient>|\"qassistantclient.h\"|g" ${worksrcpath}/qtiplot/src/core/ApplicationWindow.cpp

    # 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]} {
        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 origin]} {
      # prepare liborigin2
      copy ${workpath}/liborigin2 ${worksrcpath}/3rdparty/liborigin2
      reinplace "s|INCLUDEPATH += ../boost|INCLUDEPATH += ${prefix}/include|g" ${worksrcpath}/3rdparty/liborigin2/liborigin2.pro
      reinplace "s|lboost_thread|lboost_thread-mt|g" ${worksrcpath}/3rdparty/liborigin2/liborigin2.pro
      reinplace "s|liborigin|liborigin2|g" ${worksrcpath}/qtiplot/src/origin/origin.pri
    }

    # prepare qtexengine
    copy ${workpath}/QTeXEngine ${worksrcpath}/3rdparty/QTeXEngine

    # add 3rdparty to compilation
    reinplace "s|3rdparty/qwtplot3d \|3rdparty/qwtplot3d 3rdparty/QTeXEngine \|g" \
    ${worksrcpath}/qtiplot.pro
    if {[variant_isset origin]} {
      reinplace "s|3rdparty/QTeXEngine \|3rdparty/QTeXEngine 3rdparty/liborigin2 \|g" \
      ${worksrcpath}/qtiplot.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}"
configure.env-append QMAKESPEC=${qt_mkspecs_dir}/macx-g++
build.env-append QMAKESPEC=${qt_mkspecs_dir}/macx-g++
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 origin description "add support for originlab files" {
    depends_lib-append port:treehh
    patchfiles-append patch-importOPJ.cpp.diff
}

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

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