#!/bin/sh

# copyright 2006 Holger Levsen
# released under the GPL version2
#
# attack of the ugly scripts! i think i want to learn about svn-buildpackage next...

if [ "$1" == "" ] ; then echo "upstream version missing, e.g. 0.97"; exit 1 ; fi
if [ "$2" == "" ] ; then echo "debian reversion missing, e.g. 1"; exit 1 ; fi

mkdir -p results
cd results


#
# checkout the code from svn if no local tuxmath_${1}.orig.tar.gz  exists
# (this allows local modifications to the debian directory)
#
if [ -e ./tuxmath_${1}.orig.tar.gz ] ; then
	#
	# use local files
	#
	tar xzvf tuxmath_${1}.orig.tar.gz

	cp -a ../tuxmath/debian ./tuxmath-$1/

	# why?? i use -I.svn below...
	find ./tuxmath-$1/ -name .svn -exec rm -rf \{\} \;
else
	#
	# checkout and prepare source from svn
	#
	svn export  http://svn.tux4kids.net/tuxmath/trunk ./tuxmath-$1
	#fake  begin
	#mkdir ./tuxmath-$1
	#cd ./tuxmath-$1
	#tar xf ../fake.tar
	#cd ..
	#fake end

	tar czvf tuxmath_${1}.orig.tar.gz ./tuxmath-$1

	cd tuxmath-$1
	svn export  http://svn.tux4kids.net/tuxmath/people/holger/tuxmath/debian
	cd ..
fi

#
# build source packages
#

cd ./tuxmath-$1
nice fakeroot dpkg-buildpackage -I.svn -uc -us -S
cd ..
rm ./tuxmath-$1 -r

# temp. for developing - use pbuilder for real
# (and make this conditional)
dpkg-source -x tuxmath_$1-$2.dsc 
cd tuxmath-$1
dpkg-buildpackage -uc -us -rfakeroot
# get previous version
cd debian
PREVIOUS_VERSION=`egrep '^[[:alnum:]]' changelog | sed -e 's/.*(\(.*\)).*/\1/' |  head -n2 | tail -1`
cd ..
cd ..

rm ./tuxmath-$1 -r

echo
for i in  tuxmath_$1-$2.dsc tuxmath_$1-$2_`dpkg-architecture -qDEB_BUILD_ARCH`.deb ; do {
	echo linda and lintian runs for $i
	linda $i
	lintian $i 
} ; done

# display and preserve debdiffs
debdiff tuxmath_${PREVIOUS_VERSION}.dsc tuxmath_$1-$2.dsc > tuxmath_${PREVIOUS_VERSION}--$1-$2.dsc.diff
debdiff tuxmath_${PREVIOUS_VERSION}.dsc tuxmath_$1-$2.dsc |less
debdiff tuxmath_${PREVIOUS_VERSION}_`dpkg-architecture -qDEB_BUILD_ARCH`.deb tuxmath_$1-$2_`dpkg-architecture -qDEB_BUILD_ARCH`.deb > tuxmath_${PREVIOUS_VERSION}--$1-$2_`dpkg-architecture -qDEB_BUILD_ARCH`.deb.diff
debdiff tuxmath_${PREVIOUS_VERSION}_`dpkg-architecture -qDEB_BUILD_ARCH`.deb tuxmath_$1-$2_`dpkg-architecture -qDEB_BUILD_ARCH`.deb|less

echo
echo now run 
echo pbuilder build tuxmath_$1-$2.dsc

