#!/bin/sh
# Heavily based on the Slackware 13 SlackBuild
# http://kcheckgmail.sf.net/
# Written by alphamanitine for www.slacky.eu

CWD=$(pwd)
NAME=kcheckgmail
VERSION=0.6.0
TMP=${TMP:-/tmp/txz}
PKG=$TMP/package-$NAME
TARGET=i486
ARCH=${ARCH:-i486}
BUILD=1al

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
fi

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

cd $TMP
tar xvjf $CWD/$NAME-$VERSION.tar.bz2
cd $NAME-$VERSION 
chown -R root:root .

echo -e "\E[0;32m+-------------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start Compiling $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+-------------------------------------+\E[0;0m"
sleep 3

mkdir -p build
cd build
cmake \
-DCMAKE_C_FLAGS="$SLKCFLAGS" \
-DCMACKE_CXX_FLAGS="$SLKFLAGS" \
-DCMAKE_INSTALL_PREFIX=/usr \
-KDE4_ENABLE_HTMLHANDBOOK=ON \
-DLIB_SUFFIX=${LIBDIRSUFFIX} \
..
make
make install DESTDIR=$PKG || exit 1
cd -

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
xargs strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
xargs strip --strip-unneeded 2> /dev/null || true
)
find $PKG/usr/man -type f -exec gzip -9 {} \;
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS COPYING INSTALL NEWS README TODO \
$PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

cd $PKG
chown -R root:root .
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

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

















