#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://www.emesene.org/trac/wiki/WikiStartIT
# Packager Michelino Chionchio - Thanks to www.slacky.eu

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-emesene
NAME=emesene
VERSION=1.0.1
ARCH=${ARCH:-i486}
BUILD=1mch

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

mkdir -p $PKG/usr/share
cd $PKG/usr/share
tar xvzf $CWD/$NAME-$VERSION.tar.gz
cd $NAME-$VERSION

chown -R root:root .

mkdir -p $PKG/usr/bin
cat << EOF > $PKG/usr/bin/emesene
#!/bin/sh

cd /usr/share/emesene-1.0.1/ && ./emesene
EOF
chmod 755 $PKG/usr/bin/emesene

mkdir -p  $PKG/usr/share/applications
cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=emesene
GenericName=MSN(tm) Client
GenericName[it]=Client MSN(tm)
Comment=Windows Live Messenger (tm) network multiplatform client
Comment[it]=Client multipiattaforma per la rete di Windows Live Messenger(tm)
Icon=/usr/share/emesene-1.0/smilies/default/Messenger.png
Exec=emesene
Terminal=false
Categories=Application;Network;
EOF

mkdir -p $PKG/usr/doc/$NAME-$VERSION
mv $PKG/usr/share/$NAME-$VERSION/{COPYING,*GPL,PSF}  $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/install
echo "if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database 1> /dev/null &> /dev/null
fi" >> $PKG/install/doinst.sh
   echo  >> $PKG/install/doinst.sh
   echo "if [ -x /usr/bin/gtk-update-icon-cache ]; then
  /usr/bin/gtk-update-icon-cache  -f -q /usr/share/icons/hicolor &> /dev/null
fi" >> $PKG/install/doinst.sh
   echo  >> $PKG/install/doinst.sh

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
