#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# http://www.die-offenbachs.de/detlev/eric.html
# Packager Andrea Sciucca  (gohanz at infinito.it)
# http://www.slacky.it
#
# 
# Required: pyenchant, pyxml
#
# Exit on most errors
set -e

CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=eric4
PKG=$TMP/package/$NAME
VERSION=4.4.4a
ARCH=${ARCH:-i486}
BUILD=1as

SOURCE=http://surfnet.dl.sourceforge.net/sourceforge/eric-ide/$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 xzvf $CWD/$NAME-$VERSION.tar.gz

echo -e "\E[0;32m+------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild Eric4 |\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 .

python install.py -b /usr/bin -i $PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a LICENSE.GPL3 README* THANKS changelog $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/usr/share/applications

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Eric4
Name[it]=Eric4
GenericName=PyQt4 Python IDE
GenericName[it]=PyQt4 Python IDE
Comment=Python and Ruby editor and IDE, written in Python
Comment[it]=Editor e IDE Python e Ruby, scritto in Python
Exec=eric4
Type=Application
Icon=/usr/share/pixmaps/Eric4.png 
StartupNotify=true
Terminal=false
Categories=Qt;Application;Development;
EOF

mkdir -p $PKG/usr/share/pixmaps
cp -a $CWD/Eric4.png  $PKG/usr/share/pixmaps/Eric4.png 

( 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/SlackBuild/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/$NAME.SlackBuild



	echo "if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database &> /dev/null
fi" >> $PKG/install/doinst.sh


cd $PKG
ADD=$(ls /var/log/packages/ | sed -ne 's/\(pyenchant\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2,/p' \
-e 's/\(pyxml\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2/p') \
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/eric4-$VERSION-$ARCH-$BUILD.txz

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