#!/bin/sh
exec 2>&1
set -ex

# the default user in the adt env is "ubuntu"
USER=ubuntu

echo "Running as uid $(id -u)"
unset TMPDIR

# we don't like running as root in the adt environment
if $(id $USER >/dev/null); then
    if [ "$(id -u)" = "0" ]; then
        # create a apt-xapian-index for the tests
        update-apt-xapian-index
        # the ADT basedir is 0700 so non-root have a hard time
        ADT_BASEDIR=$ADTTMP/../..
        echo "FIXING owner in ${ADT_BASEDIR} to user $USER"
        chown -R $USER ${ADT_BASEDIR}
        echo "Switching to user $USER"
        su -lc "cd $PWD; sh $0" $USER
    fi
fi

if ! xvfb-run ./run-tests.sh; then
    # show all the failures for easier analyzing
    cat ./tests/output/*.FAIL
    exit 1
fi

