#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://www.openwengo.org
# Packagers Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it




CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/wengophone
NAME=wengophone
VERSION=2.1.1
RELEASE=2007-06-27
CHOST=i486
ARCH=${ARCH:-i686}
BUILD=1as
SOURCE=http://download.wengo.com/wengophone/release/$RELEASE/WengoPhone-$VERSION-linux-bin-x86.tar.bz2

if [ ! -e WengoPhone-$VERSION-linux-bin-x86.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/WengoPhone-$VERSION-linux-bin-x86.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 WengoPhone-$VERSION-minsizerel

mkdir -p $PKG/usr/share/$NAME

cp -a -r * $PKG/usr/share/$NAME

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a COPYING  \
$PKG/usr/doc/$NAME-$VERSION

mkdir -p  $PKG/usr/bin


cat << EOF > $PKG/usr/bin/$NAME
#!/bin/bash
cd /usr/share/$NAME
exec /usr/share/$NAME/wengophone.sh
EOF
chmod 755 $PKG/usr/bin/*



mkdir -p  $PKG/usr/share/applications

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Name=WengoPhone
Comment=Softphone which allows you to make free PC to PC video and voice calls,
GenericName=Softphone
Exec=wengophone
Type=Application
Encoding=UTF-8
Terminal=false
Icon=/usr/share/wengophone/pics/avatars/export33_96.png
Categories=Application;Network;
StartupNotify=true
EOF

( cd $PKG/usr/share/$NAME
sed -i "s|exec ./qtwengophone -style=plastique|exec esddsp ./qtwengophone -style=plastique|g" wengophone.sh )



( 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
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

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
find . -perm 777 -exec chmod 755 {} \;
find . -perm 700 -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 {} \;
find . -perm 600 -exec chmod 644 {} \;
chown -R root:root .
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
