#!/bin/sh
#
# swi-prolog.SlackBuild
# 2014-01-15
#
# SWI-Prolog offers a comprehensive Free Software Prolog environment,
# licensed under the Lesser GNU Public License.
# Together with its graphics toolkit XPCE, its development started in 1987
# and has been driven by the needs for real-world applications.
# These days SWI-Prolog is widely used in research and education as well as
# for commercial applications.

PRGNAM=swi-prolog
VERSION=6.6.1

# 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.swi-prolog.org/"
SOURCE="pl-${VERSION}.tar.gz"
DOWNLOAD="${HOMEPAGE}download/stable/src/${SOURCE}"
MD5SUM="20d9d0d24279d2fd94b64f279dedf35e"

REFERENCE="SWI-Prolog-${VERSION}.pdf"
REFERENCE_DOWNLOAD="${HOMEPAGE}download/stable/doc/${REFERENCE}"
REFERENCE_MD5SUM="0bb80f85717436b5ffb6db9209c82018"

REQUIRES=""

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

DESC="SWI-Prolog

SWI-Prolog offers a comprehensive Free Software Prolog environment,
licensed under the Lesser GNU Public License.
Together with its graphics toolkit XPCE, its development started in
1987 and has been driven by the needs for real-world applications.
These days SWI-Prolog is widely used in research and education
as well as for commercial applications.

${HOMEPAGE}"

DOCS="$(pwd)/$0 $(pwd)/${REFERENCE} COPYING INSTALL README*"

# 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
if [ ! -f $CWD/$REFERENCE ]; then
  wget -O $CWD/$REFERENCE $REFERENCE_DOWNLOAD || exit 1
fi

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi
if [ "$(md5sum $CWD/$REFERENCE | cut -f 1 -d ' ')" != "$REFERENCE_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 pl-$VERSION || exit 1

# 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"
  LIBDIRSUFFIX=""
 elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
 elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
 else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

rm -rf $PKG
mkdir -p $PKG/install

cd src
CFLAGS="$SLKCFLAGS" CPPFLAGS="$SLKCFLAGS" \
  ./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBDIRSUFFIX \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  || exit 1
make || exit 1
make install DESTDIR=$PKG || exit 1
cd ..

cd packages
CFLAGS="$SLKCFLAGS" CPPFLAGS="$SLKCFLAGS" \
  ./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBDIRSUFFIX \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  || exit 1
make || exit 1
make install DESTDIR=$PKG || exit 1
cd ..

# 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

# 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/pl-$VERSION
