#!/bin/bash
# packager          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage			: http://www.feedparser.org/
# source location	: http://feedparser.googlecode.com/files/feedparser-4.1.zip
# dependency        : 
# last revised      : 23.09.2009

softname='feedparser'
softversion='4.1'
packageversion='1fb'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.zip"
src="$buildir/"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://feedparser.googlecode.com/files/$softname-$softversion.zip

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/doc/$softname-$softversion
mkdir -p $dest/usr/src/$softname-$softversion

# Slac-desc
cat <<EOF > $dest/install/slack-desc
        |-----handy-ruler------------------------------------------------------|
feedparser: feedparser (Parse RSS feeds in Python)
feedparser: 
feedparser: feedparser is a Python utility to parse RSS and Atom feeds
feedparser: 
feedparser:
feedparser: 
feedparser:
feedparser:
feedparser:
feedparser:
feedparser:
EOF

# check if source is present
if [ -f $softname-$softversion.zip ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
unzip $srcpkg
cd $src

# build
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

python setup.py build || exit 1

python setup.py install --root $dest || exit 1

# add 'default' files
cp -a \
   README LICENSE \
   $dest/usr/doc/$softname-$softversion
cp docs/*.html $dest/usr/doc/$softname-$softversion

# strip
( cd $dest
  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
)

# compress man pages
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

# SalixBuild
cp $buildir/build-$softname.sh $dest/$prefix/src/$softname-$softversion

# set target permissions
chown -R root:root $dest

cd $dest
find . -perm 664 -exec chmod 644 {} \;
find . -perm 640 -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 {} \;
find . -type d -exec chmod 755 {} \;

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .dep
echo -e "python" > $package.dep

# .src
rm -rf docs
rm -rf build
rm *.py 
rm README LICENSE
rm -rf $dest

# erase
echo http://zenwalk.pinguix.com/user-accounts/bip/salix/l/feedparser/build-feedparser.sh > $package.src
echo $source >>$package.src

# log
) 2>&1 | tee "$softname-$softversion-build.log"
