#!/usr/bin/env bash

if [ -z "$SAGE_TESTDIR" -a -n "$SAGE_ROOT" ]; then
    SAGE_TESTDIR="$SAGE_ROOT"/tmp
fi
mkdir -p "$SAGE_TESTDIR"
SAGE_TEST_LOG="$SAGE_TESTDIR/test.log"
DSAGE_TEST_LOG="$SAGE_TESTDIR/test-dsage.log"
cd "$SAGE_TESTDIR"

cat "$SAGE_ROOT/local/bin/sage-banner" > "$SAGE_TEST_LOG"
echo `date` >> "$SAGE_TEST_LOG"

# Run DSAGE unit tests
sage-dsage-trial 2>&1 | tee "$DSAGE_TEST_LOG" | tee -a "$SAGE_TEST_LOG"

if [ -d $SAGE_ROOT/examples ]; then
   
   "$SAGE_ROOT"/examples/test_all | tee -a "$SAGE_TEST_LOG"
fi

# Run doctests on documentation and library, which end with a summary report.
# (We do this all in a single run of "sage -t" so we get a single summary.)
# We test three subdirectories of doc/, rather than testing the
# entirety of doc/, so that we can skip doc/output.
sage -t "$@" devel/doc/common devel/doc/en devel/doc/fr devel/sage/sage 2>&1 | tee -a "$SAGE_TEST_LOG"

# Print the small summary report for dsage unit tests, if necessary.
# TODO -- skip this for now.
grep "ERROR occurred" "$DSAGE_TEST_LOG" | tee -a "$SAGE_TEST_LOG"

echo "Please see $SAGE_TEST_LOG for the complete log from this test."

exit 0
