#!/bin/sh
#
# sage.SlackBuild
# 2014-09-24
#
# Sage is a free open-source mathematics software system licensed under the
# GPL. It combines the power of many existing open-source packages into a
# common Python-based interface.

PRGNAM=sage
VERSION=6.3

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  ARCH=$(uname -m)
fi

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

HOMEPAGE="http://www.sagemath.org/"
SOURCE="${PRGNAM}-${VERSION}.tar.gz"
DOWNLOAD="http://mirror.switch.ch/mirror/sagemath/src/${SOURCE}"
MD5SUM="668f080be70ffea8d67592ca00d161eb"

REQUIRES=""

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

DESC="Sage: Open Source Mathematics Software

Sage is a free open-source mathematics software system
licensed under the GPL. It combines the power of many existing
open-source packages into a common Python-based interface.

${HOMEPAGE}"

DOCS="$(pwd)/$0 *.txt src/doc/output/html"

# 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

rm -rf .git
find . -type f -name .gitignore -delete

# compile the source
# It is inadvisable to build Sage as root
# http://www.sagemath.org/doc/installation/source.html#general-procedure
# Warning: Sage simply does not build on every machine (scaling_governor?)
# http://www.sagemath.org/doc/installation/source.html#atlas
chown -R nobody:nogroup .
su nobody -s /bin/sh -c "HOME=$TMP make" || exit 1

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

# install everything into the package directory
# http://www.sagemath.org/doc/installation/source.html#system-wide-install
rm -rf $PKG
mkdir -p $PKG/install
# marked as "Experimental use only!"
#make install DESTDIR=$PKG || exit 1
cd $TMP
mkdir -p $PKG/opt
mv $TMP/$PRGNAM-$VERSION $PKG/opt/$PRGNAM || exit 1
cd $PKG/opt/$PRGNAM
mkdir -p $PKG/usr/bin

# set SAGE_ROOT
sed 's/^#SAGE_ROOT=.*$/SAGE_ROOT="\/opt\/sage"/' $PKG/opt/$PRGNAM/sage \
  > $PKG/usr/bin/sage
chmod +x $PKG/usr/bin/sage

# 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
echo "exit" | usr/bin/sage
EOF

# build the package
cd $PKG
makepkg -l y -p -c n \
  $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz} \
  || exit 1

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