#!/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"

echo "---------------------" > "$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

echo "Testing SAGE documentation"

echo "Testing SAGE tutorial"
sage -t "$@" "$SAGE_ROOT"/devel/doc/tut/tut.tex 2>&1 | tee -a "$SAGE_TEST_LOG"

echo "Testing SAGE programming guide"
sage -t "$@" "$SAGE_ROOT"/devel/doc/prog/prog.tex 2>&1 | tee -a "$SAGE_TEST_LOG"

echo "Testing SAGE constructions guide"
sage -t "$@" "$SAGE_ROOT"/devel/doc/const/const.tex 2>&1 | tee -a "$SAGE_TEST_LOG"

# Run SAGE library doctests, which end with a summary report.
sage -t "$@" "$SAGE_ROOT"/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"

if [ "$SAGE_TESTDIR" = "$SAGE_ROOT/tmp" ]; then
    cat "$SAGE_TEST_LOG" >> "$SAGE_ROOT"/test.log
fi

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

exit 0
