#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://kdesvn.alwins-world.de/trac.fcgi/downloads/
# Packagers Gohanz ( gohanz@infinito.it)
# http://www.slacky.eu

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-kdesvn
NAME=kdesvn
VERSION=1.0.5
ARCH=${ARCH:-i486}
BUILD=1as

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

cd $TMP
tar xjvf $CWD/$NAME-$VERSION.tar.bz2

echo "+-------------------------------------+"
echo "| Starting SlackBuild  $NAME-$VERSION   |"
echo "+-------------------------------------+"

cd $NAME-$VERSION
find . -perm 777 -exec chmod 755 {} \;
chown -R root:root .
mkdir -p build
cd build
  cmake \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DMAN_INSTALL_DIR=/usr/man ..
make || exit 1
make install DESTDIR=$PKG || exit 1

( cd $PKG 
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd ..
cp -a AUTHORS COPYING* ChangeLog GPL.txt INSTALL-cmake NEWS README TODO $PKG/usr/doc/$NAME-$VERSION

mv $PKG/usr/share/man $PKG/usr/
find $PKG/usr/man -type f -exec gzip -9 {} \;

cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
fi
