# $Id: PKGBUILD 138932 2015-08-29 17:18:32Z anatolik $
# Maintainer: Andrzej Giniewicz <gginiu@gmail.com>
# Contributor: Thomas Dziedzic < gostrc at gmail >
# Contributor: Christofer Bertonha <christoferbertonha at gmail dot com>
# Contributor: leepesjee <lpeschier at xs4all dot nl>
# Contributor: Olivier Medoc
# Contributor: ignotus
# Contributor: Fabian Moser
# Contributor: djscholl

# TODO: for Qt5 see FS#43007

pkgname=vtk
pkgver=6.1.0
_majorver=6.1
pkgrel=9
pkgdesc='A software system for 3D computer graphics, image processing, and visualization.'
arch=('i686' 'x86_64')
url='http://www.vtk.org/'
license=('BSD')
depends=('boost' 'ffmpeg' 'qtwebkit' 'lesstif' 'mariadb' 'gdal' 'unixodbc' 'openmpi' 'jsoncpp')
makedepends=('cmake' 'java-environment' 'doxygen' 'gnuplot' 'tk' 'wget' 'python2-matplotlib' 'python2-twisted')
optdepends=('python2: python bindings'
            'java-runtime: java bindings'
            'tk: tcl bindings'
            'gnuplot: plotting tools'
            'graphviz: drawing tools'
            'python2-matplotlib: for Matplotlib rendering'
            'python2-twisted: for vtkWeb')
provides=('python2-autobahn')
source=("http://www.vtk.org/files/release/${_majorver}/VTK-${pkgver}.tar.gz"
        "http://www.vtk.org/files/release/${_majorver}/VTKData-${pkgver}.tar.gz"
        "http://www.vtk.org/files/release/${_majorver}/VTKLargeData-${pkgver}.tar.gz"
        mesa.diff hdf5.diff vtk-type.patch)
options=(staticlibs)
md5sums=('25e4dfb3bad778722dcaec80cd5dab7d'
         'f82c8fe151279c0422ab8a6cfe991c94'
         '5bc47885923ef30e3ac77fcdf6d4e599'
         '381921f3169facf5d6ef22de3cb3fbe5'
         '1cf6b69312ed424a3595c3c11c82750d'
         'ec131e2c51b826d75bd996a410745279')

prepare() {
  cd "${srcdir}"/VTK-$pkgver

  # fix compilation errors:
  patch -Np1 -i "${srcdir}/mesa.diff"
  patch -Np1 -i "${srcdir}/hdf5.diff"
  patch -Np1 -i "${srcdir}/vtk-type.patch"

  sed -e "s|#![ ]*/usr/bin/python$|#!/usr/bin/python2|" \
      -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
      -e "s|#![ ]*/bin/env python$|#!/usr/bin/env python2|" \
      -i $(find . -name '*.py')
}

build() {
  cd "${srcdir}"
  rm -rf build
  mkdir build
  cd build

  # to help cmake find java
  export JAVA_HOME=/usr/lib/jvm/default

  # flags to enable using system libs
  local cmake_system_flags=""
  # TODO: try to use system provided AUTOBAHN, GL2PS, XDMF2, LIBPROJ4 and NETCDF
  for lib in HDF5 EXPAT FREETYPE JPEG PNG TIFF ZLIB LIBXML2 OGGTHEORA TWISTED ZOPE JSONCPP; do
    cmake_system_flags+="-DVTK_USE_SYSTEM_${lib}:BOOL=ON "
  done

  # flags to use python2 instead of python which is 3.x.x on archlinux
  local cmake_system_python_flags="-DPYTHON_EXECUTABLE:PATH=/usr/bin/python2 -DPYTHON_INCLUDE_DIR:PATH=/usr/include/python2.7 -DPYTHON_LIBRARY:PATH=/usr/lib/libpython2.7.so"

  cmake \
    -Wno-dev \
    -DBUILD_SHARED_LIBS:BOOL=ON \
    -DCMAKE_INSTALL_PREFIX:FILEPATH=/usr \
    -DBUILD_DOCUMENTATION:BOOL=ON \
    -DDOCUMENTATION_HTML_HELP:BOOL=ON \
    -DDOCUMENTATION_HTML_TARZ:BOOL=ON \
    -DBUILD_EXAMPLES:BOOL=ON \
    -DVTK_USE_FFMPEG_ENCODER:BOOL=ON \
    -DVTK_BUILD_ALL_MODULES:BOOL=ON \
    -DVTK_USE_LARGE_DATA:BOOL=ON \
    -DVTK_QT_VERSION:STRING="4" \
    -DVTK_WRAP_JAVA:BOOL=ON \
    -DVTK_WRAP_PYTHON:BOOL=ON \
    -DVTK_WRAP_TCL:BOOL=ON \
    -DCMAKE_CXX_FLAGS="-D__STDC_CONSTANT_MACROS" \
    ${cmake_system_flags} \
    ${cmake_system_python_flags} \
    "${srcdir}/VTK-$pkgver"

  make
}

package() {
  cd "${srcdir}/build"

  make DESTDIR="${pkgdir}" install

  # Move the vtk.jar to the arch-specific location
  install -dv "${pkgdir}/usr/share/java/vtk"
  mv -v "${pkgdir}/usr/lib/vtk.jar" "${pkgdir}/usr/share/java/vtk"
  rm -rf "${pkgdir}/usr/lib/vtk-${_majorver}/java"

  # Install license
  install -dv "${pkgdir}/usr/share/licenses/vtk"
  install -m644 "${srcdir}/VTK-$pkgver/Copyright.txt" "${pkgdir}/usr/share/licenses/vtk"

  # Fix path of QtDesigner plugin
  install -dv "${pkgdir}/usr/lib/qt4"
  mv "$pkgdir"/usr/plugins "$pkgdir"/usr/lib/qt4/plugins
}

