#!/bin/bash

CONFIGURE_PARAMS="" #"--enable-mgl2"

#apply all quilt patches, recording current status
export QUILT_PATCHES=debian/patches
if [ ! -f ${QUILT_PATCHES}/series ] ; then

	echo "Quilt series file, $QUILT_PATCHES missing "
	exit 1
fi

QUILT_APPLIED=`quilt applied`
quilt push -a 

#If manually running outside autopkgtest, then
# just use /tmp
if [ x"$ADTDMP" == x"" ] ; then
	if [ x"$TMP" == x"" ] ; then
		ADTDMP=/tmp/
	else
		ADTDMP=$TMP
	fi
fi

REBUILD_DIR=$ADTDMP/3depict-debug/

#Go to top level dir
#---
TOP_LEVEL=` git rev-parse --show-toplevel`
if [ $? -ne 0 ] ; then
	TOP_LEVEL=""
else
	pushd $TOP_LEVEL
fi



rm -rf $REBUILD_DIR || exit 1;
mkdir  $REBUILD_DIR || exit 1;
cp -R ./ $REBUILD_DIR 

pushd $REBUILD_DIR

#Rebuild program with debugging enabled.
# Run tests in both single and multithreaded modes
#----
#First run may not have built per Debian bug #730100  
if [ -f Makefile ] ; then
	make clean ||  exit 1;
fi
./configure  $CONFIGURE_PARAMS ||  exit 1;   
make -j2  ||  exit 1;
./src/3Depict -t || exit 1; 

make clean || exit 1;
./configure $CONFIGURE_PARAMS --enable-openmp-parallel
make -j2
./src/3Depict -t || exit 1;
make clean
#----

rm -rf $REBUILD_DIR/

popd 

#Restore patches to previous state
quilt pop -a
for i in $QUILT_APPLIED
do
	quilt push
done

if [ x"$TOP_LEVEL" != x"" ] ; then
	popd
fi


