#!/bin/sh
# Packager GioPower, luca.gio.85~at~gmail~dot~com
# InsightTooolkit (ITK: Insight Segmentation and Registration Toolkit)
# Heavily based on the Slackware 12.2 SlackBuild
# ITK is an open-source, cross-platform system that provides developers 
# with an extensive suite of software tools for image analysis.
# Developed through extreme programming methodologies, ITK employs 
# leading-edge algorithms for registering and segmenting 
# multidimensional data.
#
# Web site: www.itk.org

# Cmake flags
SHARED=ON
DOXYGEN=OFF
EXAMPLES=OFF

DO_STRIP=true

# MAKE_FLAGS=-j5
# multithreading: may use -j3 for dual-core or -j5 for quad-core (or more ;))

CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/tmp/tgz
fi

NAME=InsightToolkit
SNAME=insighttoolkit
VERSION=3.14.0
ARCH=${ARCH:-i686}
BUILD=1gp
PKG=$TMP/package-$NAME
DESTDIR=$PKG/usr
rm -rf $PKG
mkdir -p $PKG
mkdir -p $DESTDIR

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"
fi

#SOURGEFORGE
SOURCE=http://surfnet.dl.sourceforge.net/sourceforge/itk/$NAME-$VERSION.tar.gz

if [ ! -e $NAME-$VERSION.tar.gz ]; then
wget -c $SOURCE
fi

cd $TMP
rm -rf $NAME-$VERSION
tar xzvf $CWD/$NAME-$VERSION.tar.gz || exit 1
cd $NAME-$VERSION
chown -R root:root

#configure and build
cmake -D BUILD_TESTING=OFF \
      -D BUILD_EXAMPLES=$EXAMPLES \
	  -D BUILD_DOXYGEN=$DOXYGEN \
	  -D BUILD_SHARED_LIBS=$SHARED \
	  -D ITK_USE_KWSTYLE=OFF \
	  -D CMAKE_INSTALL_PREFIX=$DESTDIR \
	  -D CMAKE_CXX_FLAGS="$SLKCFLAGS" \
	  -D CMAKE_C_FLAGS="$SLKCFLAGS" \
	  $TMP/$NAME-$VERSION || exit 1
#make $MAKE_FLAGS || exit 1
make || exit 1
make install || exit 1

#strip
if [ $DO_STRIP ]; then
strip --strip-unneeded $DESTDIR/lib/*
strip --strip-unneeded $DESTDIR/bin/*
fi

mkdir -p $PKG/install
cat $CWD/slack-desc >$PKG/install/slack-desc
cat $CWD/doinst.sh >$PKG/install/doinst.sh
DOCDIR=$PKG/usr/doc/$NAME-$VERSION
mkdir -p $DOCDIR
cat $CWD/slack-desc >$DOCDIR/slack-desk
cat $CWD/$SNAME.SlackBuild >$DOCDIR/$SNAME.SlackBuild

cd $PKG
requiredbuilder -y -v -s $CWD $PKG
makepkg -p -l y -c n $CWD/$SNAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME-$VERSION
  rm -rf $PKG
fi
