#!/bin/sh

# Build script for Beautifulsoup
# Author: Ejis <ejis@mailoo.org>

# Homepage: http://www.crummy.com/software/BeautifulSoup/


set -e


SOFTNAME=beautifulsoup
VERSION=3.1.0
ZENVER=64
BUILD=1

MARCH=i486
FLAGS="-O2 -march=$MARCH -mtune=i686"

DOTNEW=1

CWD=$(pwd)

PKG=$SOFTNAME-$VERSION-$MARCH-$ZENVER.$BUILD

SOFTDIR=$CWD/BeautifulSoup-$VERSION
PKGDIR=$CWD/$PKG

PKGFILE=$PKGDIR.txz
MD5=$PKGDIR.md5
SRC=$PKGDIR.src
DEP=$PKGDIR.dep
LOG=$PKGDIR.log

SRCFILE=BeautifulSoup-$VERSION.tar.gz
SRCURL='http://www.crummy.com/software/BeautifulSoup/download/3.1.x/'

SRCLINKS=($SRCURL/$SRCFILE)

DOC=(
	$SOFTDIR/PKG-INFO
)

EXTRADEPS=(
	'python'
)

LESSDEPS=(
)


# Check for source files and download it if necessary

if [ ! -f $CWD/$SRCFILE ]; then
	wget -c -t 0 -T 50 -w 0 $SRCURL/$SRCFILE
fi


# Erase the previous build files

rm -rf $SOFTDIR $PKGDIR $MD5 $SRC $DEP $LOG
mkdir -p $PKG


# Build

build () {
	
	echo ""
	echo "-----Building-----"
	echo ""
	
	tar -xvf $CWD/$SRCFILE

	cd $SOFTDIR

	python setup.py install --prefix=/usr --root=$PKGDIR

}


# Packaging

packaging () {
	
	echo ""
	echo "-----Packaging-----"
	echo ""
	
	( cd $PKG

	mkdir -p $PKGDIR/install
	mkdir -p $PKGDIR/usr/src/$SOFTNAME-$VERSION
	mkdir -p $PKGDIR/usr/doc/$SOFTNAME-$VERSION


           #|-----handy-ruler------------------------------------------------------|
	cat > $PKGDIR/install/slack-desc <<EOF
$SOFTNAME: $SOFTNAME
$SOFTNAME:
$SOFTNAME: Beautiful Soup is a Python HTML/XML parser designed for quick
$SOFTNAME: turnaround projects like screen-scraping.
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME:
EOF

	
	# Redundent
	
	if [ -d $PKGDIR/usr/share/doc ]; then
		mv $PKGDIR/usr/share/doc/$SOFTNAME/* $PKGDIR/usr/doc/$SOFTNAME-$VERSION/
		rm -rvf $PKGDIR/usr/share/doc
	fi
	
	if [ -d $PKGDIR/etc ]; then
		if [ $DOTNEW == 1 ]; then
			find $PKGDIR/etc -type f -exec \
				mv {} {}.new \;
		fi
	fi

	find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
		xargs strip --strip-unneeded 2> /dev/null || true
	find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
		xargs strip --strip-unneeded 2> /dev/null || true

	if [ -d $PKGDIR/usr/man ]; then
		
		( cd $PKGDIR/usr/man
			find . -type f -exec gzip -9 {} \;
			for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
		)
		
	fi

	if [ -d $PKGDIR/usr/info ]; then
		
		rm -f $PKGDIR/usr/info/dir
		gzip -9 $PKGDIR/usr/info/*.info*
		
	fi

	find . -name perllocal.pod \
		-o -name ".packlist" \
		-o -name "*.bs" \
		| xargs rm -f


	cp -v $CWD/build-$SOFTNAME.sh \
		$PKGDIR/usr/src/$SOFTNAME-$VERSION/

	cp -rv	${DOC[@]} $PKGDIR/usr/doc/$SOFTNAME-$VERSION/


	# fix permissions and ownership
	chown -R root:root .
	find . -perm 777 -exec chmod 755 {} \;
	find . -perm 775 -exec chmod 755 {} \;
	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 511 -exec chmod 755 {} \;
	find . -perm 711 -exec chmod 755 {} \;
	find . -perm 555 -exec chmod 755 {} \;


	/sbin/makepkg -l y -c n $PKGFILE )


	echo 'Creating .md5 file'
	md5sum $PKG.txz >> $MD5

	echo 'Creating .src file'
	for link in ${SRCLINKS[@]}; do
		echo $link >> $SRC
	done

	echo 'Checking for dependencies'
	deps=$(depfinder $PKGFILE)
	
	for dep in ${EXTRADEPS[@]}; do
		deps="$deps,$dep"
	done
	
	for dep in ${LESSDEPS[@]}; do
		deps=${deps/$dep/}
	done
	
	order=$(echo ${deps//,/ } | sort -d)
	echo ${order// /,} >> $DEP

	echo "Removing build directories"
	rm -rf $SOFTDIR $PKG
	
}


LANG=C build | tee $LOG
LANG=C packaging 2>&1 | tee -a $LOG
