#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://www.jabbin.com
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
# Required: Speex.

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package
NAME=jabbin
VERSION=2.0beta2a
ARCH=${ARCH:-i686}
BUILD=1as
SOURCE=http://mesh.dl.sourceforge.net/sourceforge/jabbin/$NAME-$VERSION.tar.bz2

if [ ! -e $NAME-$VERSION.tar.bz2 ]; then
wget -c $SOURCE
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

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

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

chown -R root:root .

chmod +x ./configure
./configure

qmake

make

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/share/jabbin
mkdir -p $PKG/usr/share/applications

( cd src
  install -m 755 jabbin $PKG/usr/bin
)

cp -a -r certs    $PKG/usr/share/jabbin
cp -a -r iconsets $PKG/usr/share/jabbin
cp -a -r sound    $PKG/usr/share/jabbin

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Jabbin
GenericName=Jabber Client
Exec=jabbin -caption "%c" %i %m
Icon=jabbin
Terminal=0
Type=Application
X-KDE-StartupNotify=true
Categories=Qt;Application;Network;
EOF

mkdir -p  $PKG/usr/share/icons/default.kde/16x16/apps
mkdir -p  $PKG/usr/share/icons/default.kde/32x32/apps
mkdir -p  $PKG/usr/share/icons/default.kde/48x48/apps

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

( cd voip/iconsets/system/default
  cp -a icon_32.png $PKG/usr/share/icons/default.kde/32x32/apps/jabbin.png
  cp -a icon_16.png $PKG/usr/share/icons/default.kde/16x16/apps/jabbin.png
  cp -a icon_48.png $PKG/usr/share/icons/default.kde/48x48/apps/jabbin.png
)

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a CHANGES* COPYING INSTALL README $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
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
