#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://tunapie.sourceforge.net
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
#
# Required: wxPython.

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/tunapie
NAME=tunapie
VERSION=2.1.12
CHOST=i486
ARCH=${ARCH:-noarch}
BUILD=1as

SOURCE=http://heanet.dl.sourceforge.net/sourceforge/tunapie/$NAME-$VERSION.tar.gz

if [ ! -e $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-$VERSION

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 .

# Setting the "Censured mode by default" change 0 to 1 for "Adult" mode.
mkdir -p $PKG/etc
echo '0'>$PKG/etc/tunapie.config.new 

(cd src
 python compile
)

mkdir -p $PKG/usr/bin

install -m 755  tunapie $PKG/usr/bin

( cd $PKG/usr/bin
  sed -i "s|python /usr/local/share/tunapie/Tunapie.py|python /usr/share/tunapie/Tunapie.py|g" tunapie
)

mkdir -p $PKG/usr/share/tunapie
cp src/*.py src/*.pyc src/*.png $PKG/usr/share/tunapie

mkdir -p $PKG/usr/share/pixmaps
cp src/tplogo.xpm $PKG//usr/share/pixmaps

mkdir -p $PKG/usr/man/man1
cp tunapie.1 $PKG/usr/man/man1
find $PKG/usr/man -type f -exec gzip -9 {} \;

mkdir -p $PKG/usr/share/applications
cp tunapie.desktop $PKG/usr/share/applications


mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a COPYING CHANGELOG INSTALL README $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh  > $PKG/install/doinst.sh
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 1> /dev/null 2> /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 1> /dev/null 2> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh

cd $PKG
ADD=$(ls /var/log/packages/ | sed -ne 's/\(wxpython\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2,/p') \
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
