#!/bin/sh -e

TOP_SRCDIR=`pwd`/`dirname $0`

CMD=`pwd`/"$1"

## Set up the test environment

export WREPORT_TABLES=$TOP_SRCDIR/tables
export WREPORT_TESTDATA=$TOP_SRCDIR/testdata

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

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

#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 [ "${BASE##*.}" = "py" ]
then
    CMD="$PYTHON $CMD"
fi
if [ ! -z "$DEBUGGER" ]
then
    if [ -x $DIR/.libs/lt-$BASE ]
    then
        CMD="$DIR/.libs/lt-$BASE"
    fi

        echo "Running $DEBUGGER $CMD $ARGS"
        RES=0
        if ! $DEBUGGER $CMD $ARGS
        then
                RES=$?
                echo "Failed with result $RES"
        fi
else
        echo "Running $CMD $ARGS"
        RES=0
        if ! $CMD $ARGS
        then
                RES=$?
                echo "Failed with result $RES"
        fi
fi


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

exit $RES
