#!/bin/sh -e

TOP_SRCDIR=`pwd`/`dirname $0`/..

CMD=`pwd`/"$1"

## Set up the test environment

TESTDIR="`mktemp -d`"
cd "$TESTDIR"

tar -C "$TOP_SRCDIR/tests" --mode=ug+w -cf - maildir mbox mboxgz test-config.txt make-links | tar -C "$TESTDIR" -xf -
(cd $TESTDIR && ./make-links)

## Clean up the test environment at exit unless asked otherwise
cleanup() {
	test -z "$PRESERVE" && rm -r "$TESTDIR"
}
trap cleanup EXIT

## Run the tests

#id=`date +%y%m%d%H%M%S`
#$DEBUGGER $BIN $ARGS 2>&1 | tee `pwd`/testrun-$id
#echo Output saved in `pwd`/testrun-$id

# Try to debug the libtool executable, if present
DIR=`dirname $CMD`
BASE=`basename $CMD`
if [ ! -z "$DEBUGGER" ] && [ -x $DIR/.libs/lt-$BASE ]
then
	echo "Running $DEBUGGER $DIR/.libs/lt-$BASE $ARGS"
	$DEBUGGER $DIR/.libs/lt-$BASE $ARGS
	RES=$?
else
	echo "Running $DEBUGGER $CMD $ARGS"
	$DEBUGGER $CMD $ARGS
	RES=$?
fi


if [ ! -z "$PAUSE" ]
then
	echo "Post-test inspection requested."
	echo "Exit this shell to cleanup the test environment."
	bash
fi

exit $RES
