#! /bin/sh

# kooldock.SlackBuild created by danix
# Heavily based on the Slackware 13.0 SlackBuild

# sources downloaded from "http://www.kde-look.org/content/show.php/kooldock?content=50910"

# Setting up initial variables:
CWD=`pwd`
TMP=${TMP:-/tmp/txz}
APP=kooldock
VERSION=0.4.7
ARCH=${ARCH:-i486}
BUILD=4dx
PKG=$TMP/package-$APP

# Setting up SLKCFLAGS
if [ "$ARCH" = "i386" ]; then
	SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
	SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "x86-64" ]; then
	SLKCFLAGS="-O2 -fPIC"
fi

# Extracting Sources
rm -rf $PKG
mkdir -p $PKG
rm -rf $TMP/$APP-$VERSION
cd $TMP || exit 1
tar -jxvf "$CWD"/$APP-$VERSION.tar.bz2 || exit 1
cd $APP || exit 1
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Apply patch for gcc-4.3.3
patch -p1 < $CWD/gcc433_patch.diff
# Configure and compile sources
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
	./configure \
	--prefix=/usr \
	--program-prefix= \
	--program-suffix= \
	--with-qt-dir=/opt/kde3/lib/qt3 \
	--with-qt-includes=/opt/kde3/lib/qt3/include \
	--with-qt-libraries=/opt/kde3/lib/qt3/lib \
	--with-extra-includes=/opt/kde3/include \
	--build=$ARCH-slackware-linux \
	--host=$ARCH-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1

# Install documentation
mkdir -p $PKG/usr/doc/$APP-$VERSION
cp -a AUTHORS COPYING ChangeLog README* INSTALL TODO /$PKG/usr/doc/$APP-$VERSION
find /$PKG/usr/doc/$APP-$VERSION -type f -exec chmod 644 {} \;
cat "$CWD"/$APP.SlackBuild > $PKG/usr/doc/$APP-$VERSION/$APP.SlackBuild

# Final tuning
mkdir -p $PKG/install
cat "$CWD"/slack-desc > $PKG/install/slack-desc
if [ -e "$CWD"/doinst.sh.gz ]; then
	zcat "$CWD"/doinst.sh.gz > $PKG/install/doinst.sh
fi
(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 objects" | grep ELF | cut -f 1 -d : | xargs strip --strip unneeded 2> /dev/null
)
if [ -d $PKG/usr/man ]; then
	(cd $PKG/usr/man
	find . -type f -exec gzip -9 {} \;
	for i in `find . -type l` ; do ln -s $( readlink $i ).gz $i.gz; rm $i; done
	)
fi
if [ -d $PKG/usr/info ]; then
	gzip -9 $PKG/usr/info/*.info
	rm -f $PKG/usr/info/dir
fi

# Building the package
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
/sbin/makepkg -l y -c n "$CWD"/$APP-$VERSION-$ARCH-$BUILD.txz
