#!/bin/bash 
# Contrib path
CPATH=`echo $0 | sed "s/\/build_contrib//"`
cd $CPATH
CPATH=`pwd`
echo building contrib package from $CPATH ...

DPATH=$CPATH

if [ $# == 1 ]
then
	DPATH=$1
	rm -rf $DPATH/* 2>/dev/null
	install -d $DPATH
	cp -r * $DPATH
	cd $DPATH
fi

mbuild()
{
	make -j2 &> ../build.log  		&&
	make install &> ../build.log

	ERROR_CODE=$?
	if [ $ERROR_CODE != 0 ]
	then
		echo
		echo -------------------------------------------
		echo Error: Could not build the contrib package!
		echo Please have a look into $DPATH/build.log
		echo -------------------------------------------
		echo
		exit $ERROR_CODE
	fi
}

echo
echo extracting packages to $DPATH ...
for i in ./*.gz; do tar xzf $i; done;
for i in ./*.tgz; do tar xzf $i; done;
#for i in ./*.zip; do unzip -q $i; done;

touch build.log
install -d include lib

echo
echo building fftw ...
echo
cd fftw*
./configure --enable-double -q --prefix=$DPATH 2&> ../build.log
mbuild
cd ..

echo
echo building glew ...
echo
cd glew*
export GLEW_DEST=$DPATH
mbuild
cd ..

echo
echo building sip ...
echo
cd sip*
python configure.py -b $DPATH/bin -d $DPATH/lib -e $DPATH/include 2&> ../build.log
mbuild
cd ..

echo
echo building gsl ...
echo
cd gsl*
./configure --prefix=$DPATH 2&> ../build.log
mbuild
cd ..

echo
echo building Qt ...
echo
cd qt*
./configure -no-qt3support -silent -no-nis -confirm-license --prefix=$DPATH 2&> ../build.log
mbuild
cd ..

echo
echo cleanup ...
echo
rm -rf fftw* glew* sip* gsl* qt* mkspecs phrasebooks man plugins translations demos examples info share build.log

echo
echo Finished building contrib package
echo
