#!/bin/sh

### Exercice the "unit tests" contained in the Praat scritps
### distributed in the upstream sources.  For now, just the *.praat
### scripts in the test/* directories are executed.

### Allow specification of the Praat executable
PRAAT=${PRAAT:-praat}

### Take care of spaces in file names
IFS='
'

### Initialize counters
test_count=0
test_ok=0

### Loop over the directories in test/
cd test
for d in $(ls -d */) ; do

    ## Skip directory manually/
    if [ ${d%%/} != manually ] ; then

        ## Loop over the file in the selected directory
        for f in $(ls ${d%%/}/*.praat) ; do

            ## Skip scripts that cannot be run from the command line
	    if [ $f != sys/script2.praat		\
	         -a $f != "fon ExperimentMFC/experimentMFC.praat" ] ; then
		## Progress display
                echo "===== $f"
		## Increment test counter
                test_count=$(($test_count + 1))
		## Execute the test and increment ok counter if it succeeds
                xvfb-run -a $PRAAT --run $f			\
                    && test_ok=$(($test_ok + 1))	\
	            || true
            fi
        done
    fi
done

### Display test statistics
echo "$test_count tests passed, $test_ok ok"

### Exit with error in case of unit test failures
[ $test_count = $test_ok ] || exit 1
