#!/bin/sh

# Make sure $ADTTMP is available
if [ "${ADTTMP}"F = "F" ]; then
   echo "Error: expected environment variable ADTTMP is not set."
   exit 1
fi

# Make sure $ADT_ARTIFACTS is available
if [ "${ADT_ARTIFACTS}"F = "F" ]; then
   echo "Error: expected environment variable ADT_ARTIFACTS is not set."
   exit 1
fi

# Determine the test execution mode 
if [ "${1}"F = "F" ]; then
   MODE="autopkgtest"
elif [ "${1}" = "-r" ]; then
   MODE="debian/rules"
else 
   echo "usage: $0 [-r]\n-r Run in debian/rules mode instead of autopkgtest mode"
   exit 2
fi

# Determine locations of required tools
SOURCE_TREE="${PWD}"
if [ "${MODE}" = "debian/rules" ]; then
   # In debian/rules mode, use the library code from the source tree
   PYTHON="/usr/bin/python2"
   export PYTHONPATH="${SOURCE_TREE}"
else
   # In autopkgtest mode, use the installed library code in the system path
   PYTHON="/usr/bin/python2"
fi

# Print a test summary
echo ""
echo "========================================================================="
echo "Running ${0} in mode: ${MODE}"
echo "========================================================================="
echo "SOURCE_TREE..: ${SOURCE_TREE}"
echo "ADTTMP.......: ${ADTTMP}"
echo "ADT_ARTIFACTS: ${ADT_ARTIFACTS}"
echo "PYTHON.......: ${PYTHON}"
echo "PYTHONPATH...: ${PYTHONPATH}"
echo "========================================================================="
echo ""

# Always run tests from within $ADTTMP
cd ${ADTTMP}

# Copy the test suite into the working directory
cp ${SOURCE_TREE}/util/test.py .
cp -r ${SOURCE_TREE}/testcase .

# Run the test suite
# This will result in warnings that the CedarBackup2 code was not found in the expected location
${PYTHON} ./test.py 
if [ $? != 0 ]; then
   exit 1
fi

# Close the test
echo ""

