#!/bin/sh
#
# OpenFOAM.SlackBuild
# 2013-10-16
#
# OpenFOAM is a free, open source CFD software package produced by a
# commercial company, OpenCFD Ltd. It has a large user base across most areas
# of engineering and science, from both commercial and academic organisations.

PRGNAM=OpenFOAM
VERSION=2.2.2

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

HOMEPAGE="http://www.openfoam.com/"
SOURCE="${PRGNAM}-${VERSION}.tgz"
DOWNLOAD="http://downloads.sourceforge.net/foam/${SOURCE}"
MD5SUM="e91d0f7add42aaba846b1081f28145b2"

REQUIRES=""

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

DESC="Open Field Operation and Manipulation

OpenFOAM is a free, open source CFD software package produced by
a commercial company, OpenCFD Ltd. It has a large user base across
most areas of engineering and science, from both commercial and
academic organisations. OpenFOAM has an extensive range of
features to solve anything from complex fluid flows involving
chemical reactions, turbulence and heat transfer,
to solid dynamics and electromagnetics.

${HOMEPAGE}"

DOCS="$(pwd)/$0 COPYING README.* 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
cd $PRGNAM-$VERSION || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .
find . -perm -u+x -exec chmod +x {} \;

# setting environment variables
FOAM_INST_DIR=$TMP source ./etc/bashrc

# compile the source
./Allwmake

# fix FOAM_INST_DIR
cat ./etc/bashrc \
  | sed 's/foamInstall=$HOME\/$WM_PROJECT/# foamInstall=$HOME\/$WM_PROJECT/' \
  | sed 's/# foamInstall=\/opt\/$WM_PROJECT/foamInstall=\/opt\/$WM_PROJECT/' \
  > ./etc/bashrc.new && mv ./etc/bashrc.new ./etc/bashrc

# install everything into the package directory
rm -rf $PKG
mkdir -p $PKG/install
mkdir -p $PKG/opt/OpenFOAM
cd $TMP
mv $PRGNAM-$VERSION $PKG/opt/OpenFOAM || exit 1
cd $PKG/opt/OpenFOAM/$PRGNAM-$VERSION

# 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

# 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

# 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
