#!/bin/sh
# vim: ts=4 sw=4 expandtab

set -x

max_num_trace_counts=50

exitstatus=0
echo RUNNING TEST $this_dir/$test_full $params_msg | tee $test_full.log
echo "MERCURY_OPTIONS=$MERCURY_OPTIONS"
trace_count=false
case "$MERCURY_OPTIONS" in
    *trace-count*)
        trace_count=true
        /bin/rm .mercury_trace_counts.* > /dev/null 2>&1
        ;;
esac

case $test_full in
    *-nodepend)
        rm -f $test_full.failed
        ;;
    *)
        rm -f $test_full.failed
        ( mmake $test.depend 2>&1 || touch $test_full.failed ) \
            | tee -a $test_full.log
        ;;
esac

if test -f $test_full.failed
then
    echo FAILED TEST $this_dir/$test_full $params_msg | tee -a $test_full.log
    echo $test_full >> FAILED_TESTS
    echo $this_dir/$test_full $params_msg >> $tests_dir/FAILED_TESTS_SUMMARY
    exitstatus=1
else
    ( mmake $test.runtest 2>&1 || touch $test_full.failed ) \
        | tee -a $test_full.log
    if test -f $test_full.failed
    then
        rm -f $test_full.failed
        case $GRADE in
            csharp*|java*|erlang*) ;;
            *)
                if test -f $test
                then
                    rm -f $test.gz $test.exe.gz
                    gzip $test || gzip $test.exe
                fi
                ;;
        esac
        echo FAILED TEST $this_dir/$test_full $params_msg \
            | tee -a $test_full.log
        echo $test_full >> FAILED_TESTS
        echo $this_dir/$test_full $params_msg \
            >> $tests_dir/FAILED_TESTS_SUMMARY
        tcdir=FAILED_TC_DIR
        exitstatus=1
    else
        rm -f $test_full.log $test.out* $test.*res*
        mmake $test.realclean > /dev/null 2>&1
        echo PASSED TEST $this_dir/$test_full $params_msg
        tcdir=PASSED_TC_DIR
    fi
    case "$trace_count" in
        true)
            n=`cat $tests_dir/$tcdir/NEXT_NUMBER`
            for counts_file in .mercury_trace_counts.*
            do
                if test -s "$counts_file"
                then
                    mv "$counts_file" "$tests_dir/$tcdir/trace_counts.$n"
                    n=`expr $n + 1`
                fi
            done
            if test "$n" -gt "$max_num_trace_counts"
            then
                ${SLICE_DIR}mtc_union -o $tests_dir/$tcdir/tmp_summary \
                    $tests_dir/$tcdir/trace_counts.*
                /bin/rm -f $tests_dir/$tcdir/trace_counts.*
                mv $tests_dir/$tcdir/tmp_summary \
                    $tests_dir/$tcdir/trace_counts.0

                echo 1 > $tests_dir/$tcdir/NEXT_NUMBER
            else
                echo $n > $tests_dir/$tcdir/NEXT_NUMBER
            fi
            ;;
    esac
fi

case "$BOOTCHECK_TEST_PROGRESS" in
    yes)
        touch .date.$test
        ;;
esac

exit $exitstatus
