#!/bin/sh
#
# paraview.SlackBuild
# 2013-10-18
#
# ParaView is an open-source, multi-platform data analysis and visualization
# application. ParaView users can quickly build visualizations to analyze
# their data using qualitative and quantitative techniques.

PRGNAM=paraview
VERSION=4.0.1

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

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

HOMEPAGE="http://www.paraview.org/"
if [ "$ARCH" = "i686" ]; then
  SOURCE="ParaView-${VERSION}-Linux-32bit.tar.gz"
  MD5SUM="3e2521b1e5cc8435791002f9203fe5fd"
elif [ "$ARCH" = "x86_64" ]; then
  SOURCE="ParaView-${VERSION}-Linux-64bit.tar.gz"
  MD5SUM="0a9be32f8b8e7008e16646027cf65432"
fi
DOWNLOAD="${HOMEPAGE}files/v4.0/${SOURCE}"

REQUIRES=""

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

DESC="ParaView

is an open-source, multi-platform data analysis and visualization
application. ParaView users can quickly build visualizations to
analyze their data using qualitative and quantitative techniques.
The data exploration can be done interactively in 3D or
programmatically using ParaView's batch processing capabilities.

${HOMEPAGE}"

DOCS="$(pwd)/$0 doc"

# EOF info


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

# exit on error
set -e

# 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
APPDIR="ParaView-${VERSION}-Linux-32bit"
if [ "$ARCH" = "x86_64" ]; then
  APPDIR="ParaView-${VERSION}-Linux-64bit"
fi
cd $APPDIR || exit 1

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

# install everything into the package directory
rm -rf $PKG
mkdir -p $PKG/install
mkdir -p $PKG/usr
mv bin $PKG/usr
mv lib $PKG/usr

# 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

# 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

# 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/$APPDIR
