#!/bin/bash
# This is a script to build a Zenwalk package. Check the Zenwalk homepage: www.zenwalk.org
#
# SOFTWARE			: python-setuptools
# PROJECT HOMEPAGE 	: http://pypi.python.org/pypi/setuptools
# SOURCES          	: http://pypi.python.org/packages/source/s/setuptools/
#
# AUTHOR         	: skywatcher (s.skywatcher|AT|gmail|DOT|com, simone antoniucci)
# LATEST REVISION   : 2009-10-30 
#
# BUILD-ONLY DEPS	: -
# NOTES				: -

### Checking for unbound variables
set -u

### Setting main variables
NAME="python-setuptools"
VERSION="0.6c11"
ARCH="i486"
CPU="i686"
BUILD=64.1
EXT="txz"

NOME="setuptools"

HOMEPAGE="http://pypi.python.org/pypi/setuptools"
DESCRIPTION="enhancements to the python distutils"
SRCLNK="http://pypi.python.org/packages/source/s/setuptools/$NOME-$VERSION.tar.gz"

CWD=`pwd`
PKGDIR=$CWD/$NAME-install
PKG=$NAME-$VERSION-$ARCH-$BUILD
SRCPKG=`echo $SRCLNK | rev | cut -d/ -f1 | rev`
SRCDIR=$CWD/$NOME-$VERSION
#~ ICONDIR=$PKGDIR/usr/share/icons/hicolor/48x48/apps/		

URL=http://zenwalk.pinguix.com/user-accounts/skywatcher/$NAME/$VERSION
DOCS="PKG-INFO *.txt"

NJOBS="-j2"
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"

### Cleaning
rm -rf $PKGDIR
rm -rf $SRCDIR

### Mkdir
mkdir -p $PKGDIR
mkdir -p $PKGDIR/install
mkdir -p $PKGDIR/usr/doc/$NAME-$VERSION
mkdir -p $PKGDIR/usr/src/$NAME-$VERSION
#~ mkdir -p $PKGDIR/usr/share/applications
#~ mkdir -p $ICONDIR

### Checking if source is present. If it's not, download it
if [ -f $SRCPKG ]; then
	echo "Source file is already present, no need to download it";
else
 	echo "Downloading source..."
	wget -c -nv $SRCLNK
fi

### Extracting source
tar xfv $SRCPKG

### Configure, make, install
cd $SRCDIR
python setup.py build || exit 1
python setup.py install --root=$PKGDIR || exit 1

### Stripping binaries
cd $PKGDIR
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

### Docs, man, info
cd $SRCDIR
cp -a $DOCS $PKGDIR/usr/doc/$NAME-$VERSION
#~ find $PKGDIR/usr/man -type f -exec gzip -9 {} \;

### Adding build-file and other stuff to the package
cp -a $CWD/$0 $PKGDIR/usr/src/$NAME-$VERSION/build-$NAME.sh
chmod a+x $PKGDIR/usr/src/$NAME-$VERSION/build-$NAME.sh

### Additional stuff

### Slack-desc 
#    |-----handy-ruler------------------------------------------------------|
cat > $PKGDIR/install/slack-desc << EOF
$NAME: $NAME - $DESCRIPTION
$NAME:  
$NAME: Setuptools is a collection of enhancements to the Python distutils
$NAME: that allow you to more easily build and distribute Python packages,
$NAME: especially ones that have dependencies on other packages.
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: $HOMEPAGE
EOF

### Ownerships and permissions
cd $PKGDIR
chown -R root:root .
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 {} \;

### Making the package
makepkg -l y -c n $CWD/$PKG.$EXT

### md5 file
cd $CWD
md5sum $PKG.$EXT > $PKG.md5

### dep file
#~ depfinder $PKG.$EXT > $PKG.dep
echo "python" > $PKG.dep

### txt file
cat $PKGDIR/install/slack-desc > $PKG.txt

### src file
rm -fr $PKG.src
for link in $URL/$0 $SRCLNK
	do echo $link >> $PKG.src
done

### Cleaning
rm -rf $SRCDIR
rm -rf $PKGDIR

### Done. Ciao!
