#!/bin/sh
#
# InsightToolkit.SlackBuild
# 2011-04-29
#
# Insight Segmentation and Registration Toolkit (ITK) is an open-source,
# cross-platform system that provides developers with  an extensive suite of
# software tools for image analysis.

PRGNAM=InsightToolkit
VERSION=3.20.0

# 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}

ITK_CSWIG_JAVA="OFF"
ITK_CSWIG_PYTHON="OFF"
ITK_CSWIG_TCL="OFF"
ITK_USE_REVIEW="ON"   # required by WrapITK
# WrapITK takes a day to build
WRAP_ITK_JAVA="OFF"
WRAP_ITK_PYTHON="ON"
WRAP_ITK_TCL="OFF"
BUILD_PYBUFFER="ON"

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

SOURCE="${PRGNAM}-${VERSION}.tar.gz"
DOWNLOAD="http://downloads.sourceforge.net/project/itk/itk/3.20/${SOURCE}"
MD5SUM="5d6b8e6e641624c6a8c06c7f95f05c9e"

CSWIG_SOURCE="CableSwig-ITK-${VERSION}.tar.gz"
CSWIG_DOWNLOAD="http://downloads.sourceforge.net/project/itk/itk/3.20/${CSWIG_SOURCE}"
CSWIG_MD5SUM="893882bf8b4fbfbae3fe8c747a75f7a0"

GUIDE="ItkSoftwareGuide-2.4.0.pdf"
GUIDE_DOWNLOAD="http://downloads.sourceforge.net/project/itk/itk/2.4/${GUIDE}"

REQUIRES=""

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

DESC="Insight Segmentation and Registration Toolkit (ITK)

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.

${HOMEPAGE}"

DOCS="$(pwd)/$0 $(pwd)/$GUIDE Copyright Documentation Examples
Copyright.txt GettingStarted.txt InsightLogo.gif README.html"

# EOF info


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

# use WrapITK?
if [ "$WRAP_ITK_JAVA" = "ON" -o \
    "$WRAP_ITK_PYTHON" = "ON" -o \
    "$WRAP_ITK_TCL" = "ON" ]; then
  USE_WRAP_ITK="ON"
else
  USE_WRAP_ITK="OFF"
fi

# use CableSwig?
if [ "$ITK_CSWIG_JAVA" = "ON" -o \
    "$ITK_CSWIG_PYTHON" = "ON" -o \
    "$ITK_CSWIG_TCL" = "ON" -o \
    "$USE_WRAP_ITK" = "ON" ]; then
  CSWIG="ON"
else
  CSWIG="OFF"
fi

# download the source if needed
if [ ! -f $CWD/$SOURCE ]; then
  wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
fi
if [ ! -f $CWD/$GUIDE ]; then
  wget -O $CWD/$GUIDE $GUIDE_DOWNLOAD || exit 1
fi
if [ "$CSWIG" = "ON" ]; then
  if [ ! -f $CWD/$CSWIG_SOURCE ]; then
    wget -O $CWD/$CSWIG_SOURCE $CSWIG_DOWNLOAD || exit 1
  fi
fi

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

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

# change to the application source directory
cd $PRGNAM-$VERSION || exit 1

if [ "$CSWIG" = "ON" ]; then
  cd Utilities || exit 1
  if [ ! -d CableSwig ]; then
    tar xvf $CWD/$CSWIG_SOURCE || exit 1
    mv CableSwig-${VERSION} CableSwig || exit 1
  fi
  cd ..
fi

# 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
# TODO: when using WrapITK
# changing compiler flags causes a segmentation fault of gcc 4.5.2
# gcc 4.4.4 compiles fine with additional compiler flags
#      -D CMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
#      -D CMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
cmake -D CMAKE_INSTALL_PREFIX="$DESTDIR" \
      -D BUILD_SHARED_LIBS="ON" \
      -D ITK_CSWIG_JAVA="$ITK_CSWIG_JAVA" \
      -D ITK_CSWIG_PYTHON="$ITK_CSWIG_PYTHON" \
      -D ITK_CSWIG_TCL="$ITK_CSWIG_TCL" \
      -D ITK_USE_REVIEW="$ITK_USE_REVIEW" \
      -D USE_WRAP_ITK="$USE_WRAP_ITK" \
      -D INSTALL_WRAP_ITK_COMPATIBILITY="OFF" \
      -D WRAP_ITK_JAVA="$WRAP_ITK_JAVA" \
      -D WRAP_ITK_PYTHON="$WRAP_ITK_PYTHON" \
      -D WRAP_ITK_TCL="$WRAP_ITK_TCL" \
      . || exit 1
make || exit 1
make install || exit 1

# setting PYTHONPATH
PYTHON_SITE_PACKAGES="/usr/lib/python2.6/site-packages"
if [ "$ITK_CSWIG_PYTHON" = "ON" ]; then
  mkdir -p "${PKG}${PYTHON_SITE_PACKAGES}"
  PTH="${PKG}${PYTHON_SITE_PACKAGES}/InsightToolkit.pth"
  echo "/usr/lib/InsightToolkit" >> "$PTH"
  echo "/usr/lib/InsightToolkit/python" >> "$PTH"
fi
if [ "$WRAP_ITK_PYTHON" = "ON" ]; then
  mkdir -p "${PKG}${PYTHON_SITE_PACKAGES}"
  PTH="${PKG}${PYTHON_SITE_PACKAGES}/WrapITK.pth"
  echo "/usr/lib/InsightToolkit/WrapITK/Python" >> "$PTH"
fi

# build PyBuffer
if [ "$BUILD_PYBUFFER" = "ON" ]; then
  cd "Wrapping/WrapITK/ExternalProjects/PyBuffer" || exit 1
  cmake -D CMAKE_INSTALL_PREFIX="$DESTDIR" \
        -D CMAKE_CXX_FLAGS="$SLKCFLAGS" \
        -D CMAKE_C_FLAGS="$SLKCFLAGS" \
        -D PYTHON_NUMARRAY_INCLUDE_DIR="/usr/lib/python2.6/site-packages/numpy/core/include/numpy" \
        -D WrapITK_DIR="${PKG}/usr/lib/InsightToolkit/WrapITK" \
        . || exit 1
  make || exit 1
  make install || exit 1
  cd "../../../.." || exit 1
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/InsightToolkit etc/ld.so.conf 1> /dev/null 2> /dev/null ; then
  echo "/usr/lib/InsightToolkit" >> etc/ld.so.conf
fi
if ! grep /usr/lib/InsightToolkit/WrapITK/lib etc/ld.so.conf 1> /dev/null 2> /dev/null ; then
  echo "/usr/lib/InsightToolkit/WrapITK/lib" >> 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/$PRGNAM-$VERSION
