# Disable OSMesa builds for now - see Bug 744434
%bcond_with OSMesa
# Make Java conditional
%bcond_without java

%{!?tcl_version: %global tcl_version %(echo 'puts $tcl_version' | tclsh)}
%{!?tcl_sitelib: %global tcl_sitelib %{_datadir}/tcl%{tcl_version}}

%define _hdf5_version 1.8.20

Summary: The Visualization Toolkit - A high level 3D visualization library
Summary(ja): The Visualization Toolkit - ハイレベル3D可視化ライブラリ
Name: vtk
Version: 6.3.0
Release: 1%{?_dist_release}

# This is a variant BSD license, a cross between BSD and ZLIB.
# For all intents, it has the same rights and restrictions as BSD.
# http://fedoraproject.org/wiki/Licensing/BSD#VTKBSDVariant
License: BSD style
Group: System Environment/Libraries
URL: http://www.vtk.org/

Source0: http://www.vtk.org/files/release/6.1/VTK-%{version}.tar.gz
Source1: http://www.vtk.org/files/release/6.1/VTKData-%{version}.tar.gz

# Use system libraries
# http://public.kitware.com/Bug/view.php?id=11823
# Patch0: vtk-6.1.0-system.patch
# Install some more needed cmake files to try to support paraview build
# http://www.vtk.org/Bug/view.php?id=14157
# Patch1: vtk-install.patch
# Patch to vtk to use system netcdf library
# Patch2: vtk-6.1.0-netcdf.patch
# Fix compilation with mesa 10.4
# https://bugzilla.redhat.com/show_bug.cgi?id=1138466
Patch3: vtk-glext.patch
# Fix types for std::min/man
# http://www.vtk.org/Bug/view.php?id=15249
# Patch4: vtk-type.patch

BuildRoot: %{_tmppath}/%{name}-%{version}-root
BuildRequires: cmake >= 2.0.0
BuildRequires: gcc-c++
%{?with_java:BuildRequires: java-devel}
BuildRequires: libX11-devel, libXt-devel, libXext-devel
BuildRequires: libICE-devel, libGL-devel
%{?with_OSMesa:BuildRequires: mesa-libOSMesa-devel}
BuildRequires: tk-devel, tcl-devel
BuildRequires: python-devel
BuildRequires: expat-devel, freetype2-devel, libjpeg-turbo-devel, libpng-devel
BuildRequires: gl2ps-devel
BuildRequires: libtiff-devel, zlib-devel
BuildRequires: libxml2-devel
BuildRequires: qt4-devel qt4-designer
#BuildRequires: qtwebkit-devel
BuildRequires: chrpath
BuildRequires: doxygen, graphviz
BuildRequires: gnuplot
BuildRequires: libboost-devel
BuildRequires: hdf5-devel
BuildRequires: jsoncpp-devel
BuildRequires: libtheora-devel
#BuildRequires: mysql-devel
BuildRequires: mariadb-devel
BuildRequires: netcdf-cxx-devel
#BuildRequires: postgresql-devel
BuildRequires: libpq-devel
BuildRequires: R
BuildRequires: PyQt4-devel
BuildRequires: sip-devel
BuildRequires: sqlite3-devel
BuildRequires: wget
#BuildRequires: %{_includedir}/Xm
BuildRequires: openMotif-devel
BuildRequires: blas-devel
BuildRequires: lapack-devel
%{!?with_java:Conflicts: vtk-java}
Requires: hdf5 = %{_hdf5_version}

Distribution: Project Vine
Vendor: Vine Linux
Packager: owa

# Do not check .so files in the python_sitearch directory
%global __provides_exclude_from ^%{python_sitearch}/.*\\.so$

%description
VTK is an open-source software system for image processing, 3D
graphics, volume rendering and visualization. VTK includes many
advanced algorithms (e.g., surface reconstruction, implicit modeling,
decimation) and rendering techniques (e.g., hardware-accelerated
volume rendering, LOD control).

%package devel
Summary: VTK header files for building C++ code.
Summary(ja): C++ プログラムをビルドするための VTK ヘッダファイル集
Group: Development/Libraries
Requires: %{name} = %{version}-%{release}
%{?with_OSMesa:Requires: mesa-libOSMesa-devel}
Requires: cmake
Requires: blas-devel
Requires: gl2ps-devel
Requires: expat-devel
Requires: freetype2-devel
Requires: hdf5-devel
Requires: lapack-devel
Requires: libjpeg-turbo-devel
Requires: libpng-devel
Requires: libogg-devel
Requires: libtheora-devel
Requires: libtiff-devel
Requires: libxml2-devel
Requires: libpq-devel
Requires: libmysqlclient-devel
Requires: qt4-devel
#Requires: qtwebkit-devel%{?_isa}
#Requires: jsoncpp-devel%{?_isa}
# bz #1183210 + #1183530
Requires: python-devel

%description devel
This provides the VTK header files required to compile C++
programs that use VTK to do 3D visualisation.


%package tcl
Summary: Tcl bindings for VTK
Summary(ja): VTK の tcl バインディング
Group: System Environment/Libraries
Requires: %{name} = %{version}-%{release}
Requires: tcl

%description tcl
tcl bindings for VTK

%description tcl -l ja
VTK の tcl バインディングです。


%package python
Summary: Python bindings for VTK.
Summary(ja): VTK の python バインディング
Group: System Environment/Libraries
Requires: %{name} = %{version}-%{release}
Requires: python

%description python
python bindings for VTK

%description python -l ja
VTK の python バインディングです。

%if %{with java}
%package java
Summary: Java bindings for VTK
Summary(ja): VTK の Java バインディング
Group: System Environment/Libraries
Requires: %{name} = %{version}-%{release}
Requires: java

%description java
Java bindings for VTK

%description java -l ja
VTK の Java バインディングです。
%endif


%package qt
Summary: Qt bindings for VTK
Summary(ja): VTK の Qt バインディング
Group: System Environment/Libraries
Requires: %{name} = %{version}-%{release}

%description qt
Qt bindings for VTK

%description qt -l ja
VTK の Qt バインディングです。


%package qt-python
Summary: Qt Python bindings for VTK
Summary(ja): VTK の Qt Python バインディング
Group: System Environment/Libraries
Requires: %{name} = %{version}-%{release}

%description qt-python
Qt Python bindings for VTK

%description qt-python -l ja
VTK の Qt Python バインディングです。

%package qt-tcl
Summary: Qt TCL bindings for VTK
Summary(ja): VTK の Qt TCL バインディング
Group: System Environment/Libraries
Requires: %{name} = %{version}-%{release}

%description qt-tcl
Qt TCL bindings for VTK

%description qt-tcl -l ja
VTK の Qt TCL バインディングです。


%package testing-progs
Summary: Tests programs for VTK.
Summary(ja): VTK のテスト用プログラム
Group: Development/Libraries
Requires: %{name} = %{version}-%{release}

%description testing-progs
Testing programs for VTK

%description testing-progs -l ja
VTK のテスト用プログラムです。


%package examples
Summary: Examples for VTK
Summary(ja): VTK のサンプル集
Group: Applications/Engineering
Requires: %{name} = %{version}-%{release}
Requires: vtk-data = %{version}

%description examples
This package contains all the examples from the VTK source.
To compile the C++ examples you will need to install the vtk-devel
package as well. The Python and Tcl examples can be run with the
corresponding packages (vtk-python, vtk-tcl).

%package data
Summary: Example data file for VTK
Summary(ja): VTK のサンプルデータファイル
Group: Development/Libraries
BuildArch: noarch

%description data
This package contains all the data from the VTKData repository.
These data are required to run various examples from the examples package.

%prep
%setup -q -n VTK-%{version}
# %patch0 -p1 -b .system
# %patch1 -p1 -b .install
# %patch2 -p1 -b .netcdf
%patch3 -p1 -b .glext
# %patch4 -p1 -b .type

# Remove included thirdparty sources just to be sure
# TODO - vtksqlite
for x in autobahn vtkexpat vtkfreetype vtkgl2ps vtkhdf5 vtkjpeg vtklibxml2 vtknetcdf vtkoggtheora vtkpng vtktiff twisted vtkzlib zope
do
  rm -r ThirdParty/*/${x}
done

# Replace relative path ../../../VTKData with %{_datadir}/vtkdata-%{version}
# otherwise it will break on symlinks.
grep -rl '\.\./\.\./\.\./\.\./VTKData' . | xargs \
  perl -pi -e's,\.\./\.\./\.\./\.\./VTKData,%{_datadir}/vtkdata-%{version},g'

# Save an unbuilt copy of the Example's sources for %doc
mkdir vtk-examples
cp -a Examples vtk-examples
# Don't ship Win32 examples
rm -r vtk-examples/Examples/GUI/Win32
find vtk-examples -type f | xargs chmod -R a-x

%build
export CFLAGS="%{optflags} -D_UNICODE"
export CXXFLAGS="%{optflags} -D_UNICODE"
%if %{with java}
export JAVA_HOME=/usr/lib/jvm/java
# Arm/Aarch64 builders have less ram
# https://bugzilla.redhat.com/show_bug.cgi?id=1115920
%ifnarch s390
export JAVA_TOOL_OPTIONS=-Xmx2048m
%endif
%endif

mkdir build
pushd build
%cmake \
       -DBUILD_DOCUMENTATION:BOOL=ON \
       -DBUILD_EXAMPLES:BOOL=ON \
       -DBUILD_TESTING:BOOL=OFF \
       -DVTK_CUSTOM_LIBRARY_SUFFIX="" \
       -DVTK_INSTALL_ARCHIVE_DIR:PATH=%{_lib}/vtk \
       -DVTK_INSTALL_DATA_DIR=share/vtk \
       -DVTK_INSTALL_INCLUDE_DIR:PATH=include/vtk \
       -DVTK_INSTALL_LIBRARY_DIR:PATH=%{_lib}/vtk \
       -DVTK_INSTALL_PACKAGE_DIR:PATH=%{_lib}/cmake/vtk \
       -DVTK_INSTALL_PYTHON_MODULE_DIR:PATH=%{_lib}/python%{python_version}/site-packages \
       -DVTK_INSTALL_QT_DIR:PATH=%{_lib}/qt4/plugins/designer \
       -DVTK_INSTALL_TCL_DIR:PATH=share/tcl%{tcl_version}/vtk \
       -DTK_INTERNAL_PATH:PATH=/usr/include/tk-private/generic \
%if %{with OSMesa}
       -DVTK_OPENGL_HAS_OSMESA:BOOL=ON \
%endif
%if %{with java}
       -DVTK_WRAP_JAVA:BOOL=ON \
       -DJAVA_INCLUDE_PATH:PATH=$JAVA_HOME/include \
       -DJAVA_INCLUDE_PATH2:PATH=$JAVA_HOME/include/linux \
       -DJAVA_AWT_INCLUDE_PATH:PATH=$JAVA_HOME/include \
%else
       -DVTK_WRAP_JAVA:BOOL=OFF \
%endif
       -DVTK_WRAP_PYTHON:BOOL=ON \
       -DVTK_WRAP_PYTHON_SIP:BOOL=ON \
       -DSIP_INCLUDE_DIR:PATH=/usr/include/python%{python_version} \
       -DVTK_WRAP_TCL:BOOL=ON \
       -DVTK_Group_Imaging:BOOL=ON \
       -DVTK_Group_Qt:BOOL=ON \
       -DVTK_Group_Rendering:BOOL=ON \
       -DVTK_Group_StandAlone:BOOL=ON \
       -DVTK_Group_Tk:BOOL=ON \
       -DVTK_Group_Views:BOOL=ON \
       -DModule_vtkFiltersStatisticsGnuR:BOOL=ON \
       -DModule_vtkTestingCore:BOOL=ON \
       -DModule_vtkTestingRendering:BOOL=ON \
       -DVTK_USE_OGGTHEORA_ENCODER=ON \
       -DVTK_USE_SYSTEM_LIBRARIES=ON \
       -DVTK_USE_SYSTEM_HDF5:BOOL=ON \
       -DVTK_USE_SYSTEM_LIBPROJ4:BOOL=OFF \
       -DVTK_USE_SYSTEM_NETCDF:BOOL=ON \
       -DTCL_LIBRARY:FILEPATH=%{_libdir}/libtcl%{tcl_version}.so \
       -DTK_LIBRARY:FILEPATH=%{_libdir}/libtk%{tcl_version}.so \
       ..
       
# TODO - MPI
#-DVTK_Group_MPI:BOOL=ON \
# Needed for some tests.  Fails to compile at the moment.  We don't run test though.
#  -DVTK_DATA_ROOT:PATH=%{_datadir}/vtkdata-%{version} \
# Not working, see http://public.kitware.com/Bug/view.php?id=11978
# -DVTK_USE_ODBC=ON \
# Commented old flags in case we'd like to reactive some of them
# -DVTK_USE_DISPLAY:BOOL=OFF \ # This prevents building of graphics tests
# -DVTK_USE_MPI:BOOL=ON \

# Got intermittent build error with -j
make %{?_smp_mflags}

# Remove executable bits from sources (some of which are generated)
find . -name \*.c -or -name \*.cxx -or -name \*.h -or -name \*.hxx -or \
       -name \*.gif | xargs chmod -x


%install
rm -rf $RPM_BUILD_ROOT
pushd build
make install DESTDIR=$RPM_BUILD_ROOT

# ld config
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/ld.so.conf.d
echo %{_libdir}/vtk > $RPM_BUILD_ROOT%{_sysconfdir}/ld.so.conf.d/vtk-%{_arch}.conf

# Gather list of non-python/tcl libraries
ls $RPM_BUILD_ROOT%{_libdir}/vtk/*.so.* \
  | grep -Ev '(Java|Qt|Python27D|TCL)' | sed -e's,^%{buildroot},,' > libs.list

# List of executable utilities
cat > utils.list << EOF
vtkEncodeString
EOF

# List of executable examples
cat > examples.list << EOF
HierarchicalBoxPipeline
MultiBlock
Arrays
Cube
RGrid
SGrid
Medical1
Medical2
Medical3
finance
AmbientSpheres
Cylinder
DiffuseSpheres
SpecularSpheres
Cone
Cone2
Cone3
Cone4
Cone5
Cone6
EOF

# Install examples too
for filelist in examples.list; do
    for file in `cat $filelist`; do
  install -p bin/$file $RPM_BUILD_ROOT%{_bindir}
    done
done

# Fix up filelist paths
for filelist in utils.list examples.list; do
  perl -pi -e's,^,%{_bindir}/,' $filelist
done

# Remove any remnants of rpaths on files we install
# Seems to be some kind of java path
for file in `cat examples.list`; do
  chrpath -d $RPM_BUILD_ROOT$file
done
chrpath -d  $RPM_BUILD_ROOT%{_qt4_plugindir}/designer/libQVTKWidgetPlugin.so

# Main package contains utils and core libs
cat libs.list utils.list > main.list
popd

# Make scripts executable
#chmod a+x %{buildroot}%{_libdir}/vtk/doxygen/*.pl
#chmod a+x %{buildroot}%{_libdir}/vtk/testing/*.{py,tcl}

# Remove exec bit from non-scripts and %%doc
for file in `find %{buildroot} -type f -perm 0755 \
  | xargs -r file | grep ASCII | awk -F: '{print $1}'`; do
    head -1 $file | grep '^#!' > /dev/null && continue
    chmod 0644 $file
done
find Utilities/Upgrading -type f | xargs chmod -x

# install VTKdata
mkdir -p $RPM_BUILD_ROOT%{_datadir}
pushd $RPM_BUILD_ROOT%{_datadir}
tar -zpxf %{SOURCE1}
mv VTK-%{version} %{name}-%{version}

# (Verbosely) fix 0555 permissions
find . -type f -perm 0555 | xargs -r echo chmod 0755 | sh -x
# Remove execute bits from not-scripts
for file in `find . -type f -perm 0755`; do
  head -1 $file | grep '^#!' > /dev/null && continue
  chmod 0644 $file
done
popd

# Setup Wrapping docs tree
rm -rf _docs
mkdir -p _docs
cp -pr --parents Wrapping/*/README* _docs/ 

%clean 
rm -rf $RPM_BUILD_ROOT

%post -p /sbin/ldconfig

%postun -p /sbin/ldconfig

%post tcl -p /sbin/ldconfig

%postun tcl -p /sbin/ldconfig

%post python -p /sbin/ldconfig

%postun python -p /sbin/ldconfig

%if %{with java}
%post java -p /sbin/ldconfig

%postun java -p /sbin/ldconfig
%endif

%post qt -p /sbin/ldconfig

%postun qt -p /sbin/ldconfig

%post qt-tcl -p /sbin/ldconfig

%postun qt-tcl -p /sbin/ldconfig

%post qt-python -p /sbin/ldconfig

%postun qt-python -p /sbin/ldconfig


%files -f build/main.list
%defattr(-,root,root)
%doc Copyright.txt README.html vtkLogo.jpg vtkBanner.gif _docs/Wrapping
%config(noreplace) %{_sysconfdir}/ld.so.conf.d/vtk-%{_arch}.conf
%{_datadir}/vtk
%dir %{_libdir}/vtk

%files devel
%defattr(-,root,root)
%doc Utilities/Upgrading
%{_bindir}/vtkHashSource
%{_bindir}/vtkWrapHierarchy
%{_includedir}/vtk
%{_libdir}/vtk/*.so
%{_libdir}/vtk/libvtkWrappingTools.a
%{_libdir}/cmake/vtk/
%{_bindir}/vtkParseOGLExt
%{_docdir}/vtk-6.3/
%{tcl_sitelib}/vtk/vtktcl.c

%files tcl
%defattr(-,root,root)
%{_libdir}/vtk/*TCL.so.*
%exclude %{_libdir}/vtk/*QtTCL.so.*
%{_bindir}/vtk
%{_bindir}/vtkWrapTcl
%{_bindir}/vtkWrapTclInit
%{tcl_sitelib}/vtk/
%exclude %{tcl_sitelib}/vtk/vtktcl.c

%files python
%defattr(-,root,root)
%{_bindir}/vtkpython
%{_bindir}/vtkWrapPython
%{_bindir}/vtkWrapPythonInit
%{_libdir}/vtk/*Python27D.so.*
%{python_sitearch}/*
%exclude %{_libdir}/vtk/*QtPython27D.so.*

%if %{with java}
%files java
%defattr(-,root,root)
%{_libdir}/vtk/*Java.so.*
%{_libdir}/vtk/vtk.jar
%{_bindir}/vtkParseJava
%{_bindir}/vtkWrapJava
%endif

%files qt
%defattr(-,root,root)
%{_libdir}/vtk/lib*Qt*.so.*
%exclude %{_libdir}/vtk/*TCL.so.*
%exclude %{_libdir}/vtk/*Python27D.so.*
%{_qt4_plugindir}/designer/libQVTKWidgetPlugin.so

%files qt-python
%defattr(-,root,root)
%{_libdir}/vtk/*Python27D.so.*

%files qt-tcl
%defattr(-,root,root)
%{_libdir}/vtk/*TCL.so.*

%files testing-progs
#-f testing-progs-list

%files examples -f build/examples.list
%defattr(-,root,root)
%doc vtk-examples/Examples

%files data
%defattr(-,root,root)
%{_datadir}/%{name}-%{version}


%changelog
* Sat Aug 25 2018 Toshiaki Ara <ara_t@384.jp> 6.3.0-1
- update to 6.3.0
- drop some Patches

* Thu Aug 23 2018 Toshiaki Ara <ara_t@384.jp> 6.1.0-3
- rebuild with hdf-1.8.20
- change BuildRequires: R-devel to BuildRequires: R

* Sat Sep 03 2016 Toshiaki Ara <ara_t@384.jp> 6.1.0-2
- rebuild with gcc-5.4.0

* Wed Jan 28 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 6.1.0-1
- updated to 6.1.0
- built with tcl/tk 8.5.15, libpng 1.6.12 and hdf5 1.8.14
- added java, qt-tcl and qt-python subpackage

* Mon May 09 2011 Shu KONNO <owa@bg.wakwak.com> 5.4.2-3
- s/qt-devel/qt4-devel/ at BR:
- add BR: libxml2-devel

* Sun May 08 2011 Shu KONNO <owa@bg.wakwak.com> 5.4.2-2
- remove _smp_mflags
- added tcl_ver macro
- added TCL_LIBRARY, TK_LIBRARY to cmake

* Tue May 04 2010 Shu KONNO <owa@bg.wakwak.com> 5.4.2-1
- updated vtk to 5.4.2
- updated vtkdata to 5.4.2
- added Obsoletes: %%{name}-ruby < 5.4.2
- added Patch2: vtk-5.2.1-boost-1.42.patch (from Mandriva)
- dropt Patch0: vtk-5.0-getsockname.patch
- dropt Patch1: vtk-ruby-060915.patch

* Thu Jul 31 2008 Shu KONNO <owa@bg.wakwak.com> 5.0.4-1vl5
- updated vtk to 5.0.4
- updated vtkdata to 5.0.4
- built with xorg-x11-devel, python-2.5
- applied new versioning policy
- spec in utf-8

* Mon Feb 25 2008 AKIYAMA Kazuhito <akiyama@yb3.so-net.ne.jp> 5.0.2-0vl6
- rebuild with expat-2.0.1
- add BuildPrereq: freetype2-devel and drop freetype-devel
- add BuildPrereq: qt-devel
- add Requires: freetype2 and drop freetype

* Sun Feb 17 2008 Shu KONNO <owa@bg.wakwak.com> 5.0.2-0vl5
- rebuild with tcl/tk-8.4.18

* Tue Oct 09 2007 Shu KONNO <owa@bg.wakwak.com> 5.0.2-0vl4
- rebuild with tcl/tk-8.4.16

* Mon Aug 27 2007 Shu KONNO <owa@bg.wakwak.com> 5.0.2-0vl3
- added %%define py_ver, py_dir
- added %%{?_smp_mflags} to make
- change for x86_64
  - changed lib to %%{_lib}
  - added VTK_INSTALL_LIB_DIR=PATH=/%{_lib} in cmake
  - added --install-lib at VTK_PYTHON_SETUP_ARGS in cmake

* Tue May 15 2007 Daisuke SUZUKI <daisuke@linux.or.jp> 5.0.2-0vl2
- rebuild with new environment/toolchain

* Fri Sep 15 2006 Seiya Nishizawa <seiya@kugi.kyoto-u.ac.jp> 5.0.2-0vl1
- new upstream
- add ruby patch

* Sat Jul 29 2006 Ryoichi INAGAKI <ryo1@bc.wakwak.com> 5.0.1-0vl1
- new upstream
- fixed spec file encoding
- changed Group to System Environment/Libraries

* Mon Feb 06 2006 Seiya Nishizawa <seiya@kugi.kyoto-u.ac.jp> 5.0.0-0vl1
- source verion up

* Tue Mar 29 2005 Seiya Nishizawa <seiya@kugi.kyoto-u.ac.jp> 4.4.2-0vl1
- source verion up

* Sun Mar 20 2005 Seiya Nishizawa <seiya@kugi.kyoto-u.ac.jp> 4.2.6-0vl1
- initial build for Vine Linux
