#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://deluge-torrent.org/
# Packager Andrea Sciucca (gohanz at infinito.it)
# http://www.slacky.it
# Required: Boost-1.35.0, Dbus-python, Pygtk, PyXdg, Pygobgect, Pycairo



CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-deluge
NAME=deluge
VERSION=0.5.8.9
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=1as
PYVER=$(python -c "import sys; print sys.version[:3]")
SOURCE=http://download.deluge-torrent.org/source/$VERSION/$NAME-$VERSION.tar.gz

if [ ! -e $CWD/$NAME-$VERSION.tar.gz ]; then
 wget -c $SOURCE
fi


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



cd $TMP
tar xvzf $CWD/$NAME-$VERSION.tar.gz

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-torrent-$VERSION
#sed -i 's/.$//' setup.py
patch -p1  < $CWD/setup.py.patch 

find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

chown -R root:root .

python setup.py build || exit 1

python setup.py install || exit 1

( mkdir -p $PKG/usr/bin/
  mv /usr/bin/deluge $PKG/usr/bin/ )
  
( mkdir -p $PKG/usr/lib/python${PYVER}/site-packages/
  mv /usr/lib/python${PYVER}/site-packages/deluge/ $PKG/usr/lib/python${PYVER}/site-packages/ )
  
( mv /usr/lib/python${PYVER}/site-packages/deluge-*.egg-info $PKG/usr/lib/python${PYVER}/site-packages/ )

( mkdir -p $PKG/usr/share/deluge/
  mv /usr/share/deluge/ $PKG/usr/share/ )

( for i in $(find /usr/share/locale/ -name deluge.mo | sed  s/deluge.mo//); do mkdir -p $PKG/$i ; done )
( for i in $(find /usr/share/locale/ -name deluge.mo) ; do mv $i $PKG/$i ; done )

( mkdir -p $PKG/usr/share/applications/
  mv /usr/share/applications/deluge.desktop $PKG/usr/share/applications/ )

( mkdir -p $PKG/usr/share/pixmaps/
  mv /usr/share/pixmaps/deluge.png $PKG/usr/share/pixmaps/deluge.png )


mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
 LICENSE README*  \
  $PKG/usr/doc/$NAME-$VERSION



( 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

	
	echo "if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database /usr/share/mime &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	echo "if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database &> /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


cd $PKG
ADD="pyxdg >= 0.15-noarch-1as,pygtk >= 2.12.1-i486-1sl,pygobject >= 2.14.1-i486-1sl,pycairo >= 1.4.12-i686-1as" \
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz


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