#!/bin/sh
#
# vtk.SlackBuild
# 2010-09-29
#
# The Visualization Toolkit (VTK) is an open-source, freely available software
# system for 3D computer graphics, image processing and visualization.
# VTK consists of a C++ class library and several interpreted interface layers
# including Tcl/Tk, Java, and Python.

PRGNAM=vtk
VERSION=5.6.1

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i686) ARCH=i686 ;;
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$(uname -m) ;;
  esac
fi

BUILD=${BUILD:-1}
TAG=${TAG:-scr}

VTK_WRAP_JAVA="OFF"
VTK_WRAP_PYTHON="ON"
VTK_WRAP_TCL="OFF"

HOMEPAGE="http://www.vtk.org/"

SOURCE="${PRGNAM}-${VERSION}.tar.gz"
DOWNLOAD="http://www.vtk.org/files/release/5.6/${SOURCE}"
MD5SUM="b80a76435207c5d0f74dfcab15b75181"

REQUIRES=""

MAINTAINER="Gernot WALZL"
EMAIL="gernot.walzl@gmx.at"

DESC="Visualization Toolkit (VTK)

VTK is an open-source, freely available software system for
3D computer graphics, image processing and visualization.
VTK consists of a C++ class library and several interpreted
interface layers including Tcl/Tk, Java, and Python.

${HOMEPAGE}"

DOCS="$(pwd)/$0 Examples Copyright.txt README.html Testing.txt
TestingLogo.gif vtkBanner.gif vtkLogo.jpg"

# EOF info


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# download the source if needed
if [ ! -f $CWD/$SOURCE ]; then
  wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
fi

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi

# extract the application source
cd $TMP || exit 1
tar xvf $CWD/$SOURCE || exit 1

# change to the application source directory
cd VTK || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# set configure options
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
 elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
 elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
 else
  SLKCFLAGS="-O2"
fi

# configure and build
rm -rf $PKG
mkdir -p $PKG/install
mkdir -p $PKG/usr
DESTDIR=$PKG/usr
cmake -D CMAKE_INSTALL_PREFIX="$DESTDIR" \
      -D CMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
      -D CMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
      -D BUILD_SHARED_LIBS="ON" \
      -D VTK_WRAP_JAVA="$VTK_WRAP_JAVA" \
      -D VTK_WRAP_PYTHON="$VTK_WRAP_PYTHON" \
      -D VTK_WRAP_TCL="$VTK_WRAP_TCL" \
      . || exit 1
make || exit 1

if [ "$VTK_WRAP_PYTHON" = "ON" ]; then
  PYTHON_SITE_PACKAGES="/usr/lib/python2.6/site-packages"
  mkdir -p "${PKG}${PYTHON_SITE_PACKAGES}"
  export PYTHONPATH="${PKG}${PYTHON_SITE_PACKAGES}"
fi

make install || exit 1

if [ "$VTK_WRAP_PYTHON" = "ON" ]; then
  rm "${PKG}${PYTHON_SITE_PACKAGES}/site.py"
  rm "${PKG}${PYTHON_SITE_PACKAGES}/site.pyc"
  mv "${PKG}${PYTHON_SITE_PACKAGES}/easy-install.pth" \
    "${PKG}${PYTHON_SITE_PACKAGES}/vtk.pth"
fi

# add documentation
if [ "$DOCS" != "" ]; then
  mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
  cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
  find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
fi

# compress man pages
if [ -d $PKG/usr/man ]; then
  find $PKG/usr/man -type f -name "*.?" -exec gzip -9 {} \;
  for i in $(find $PKG/usr/man -type l -name "*.?") ; do
    ln -s $(readlink $i).gz $i.gz
    rm $i
  done
fi

# strip binaries
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# add some description to the package
if [ "$DESC" != "" ]; then
  echo "$DESC" | sed 's/^/'$PRGNAM': /g' > $PKG/install/slack-desc
fi

# append dependency information to the package
if [ "$REQUIRES" != "" ]; then
  for REQ in $REQUIRES; do
    echo "$REQ" >> $PKG/install/slack-required
  done
fi

# doinst.sh
cat > $PKG/install/doinst.sh <<EOF
#!/bin/sh
if ! grep /usr/lib/vtk-5.6 etc/ld.so.conf 1> /dev/null 2> /dev/null ; then
  echo "/usr/lib/vtk-5.6" >> etc/ld.so.conf
fi
if [ -x /sbin/ldconfig ]; then
  /sbin/ldconfig 2> /dev/null
fi
EOF

# build the package
cd $PKG
PKGVERSION=$(echo $VERSION | sed 's/-/./g')
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$PKGVERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz} \
  || exit 1

# make everything clean
cd $CWD
rm -rf $PKG
#rm -rf $TMP/VTK
