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

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

### Take care of spaces in file names
IFS='
'
run() {
    ## Increment test counter
    test_count=$(($test_count + 1))
    ## Execute the test and increment ok counter if it succeeds
    xvfb-run -a $PRAAT --run $1		\
        && test_ok=$(($test_ok + 1))	\
        || failed="$failed\n    $1"
}

### Initialize counters
test_count=0
test_ok=0
failed=

### Copy lacking file for unit test unicode.praat
cp debian/éééürtüéŋəü.wav test/kar

### 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

            ## Progress display
	    echo "===== $f"

            ## Skip scripts that cannot be run from the command line
	    ## or that crash on unstable as on 2018-01-27 (see Bug#887685),
	    ## or that crash on some architectures.
	    if [ $f != sys/script2.praat				\
	         -a $f != "fon ExperimentMFC/experimentMFC.praat"	\
		 -a $f != num/fisherQ.praat				\
		 -a $f != dwtools/Discriminant.praat			\
		 -a $f != dwtools/EditCostsTable.praat			\
		 -a $f != fon/resample16_8.praat			\
		 -a $f != fon/resample22_8.praat			\
		 -a $f != fon/resample24_8.praat			\
		 -a $f != fon/resample32_8.praat			\
		 -a $f != fon/resample44_8.praat			\
		 -a $f != fon/resample48_8.praat			\
		 -a $f != fon/resample96_8.praat			\
		 -a $f != fon/Spectrum_draw.praat			\
		 -a $f != kar/MacRoman.praat				\
		 -a $f != sys/graphics.praat				\
		 -a $f != sys/graphicsText.praat			\
		 -a $f != sys/graphicsTextSpeed.praat ] ; then
		run $f
            else
	        echo "Test skipped"
            fi
        done
    fi
done

### Loop over the scripts in dwtest/
cd ../dwtest

## Loop over the file in the selected directory
for f in $(ls *.praat) ; do

    ## Progress display
    echo "===== $f"

    ## Skip scripts that cannot be run from the command line
    ## or that crash on unstable as on 2018-01-27
    ## (see Bug#887685 and Bug#889703).
    if [ $f != runAllTests.praat				\
	 -a $f != KlattGrid_test.praat				\
	 -a $f != speechsynthesizer_test.praat			\
	 -a $f != test_bss_twoSoundsMixed.praat			\
	 -a $f != test_Discriminant.praat			\
	 -a $f != test_MixingMatrix.praat			\
	 -a $f != test_sigma_ellipse.praat			\
	 -a $f != test_Sound_draw_where.praat			\
	 -a $f != test_SpeechSynthesizer.praat			\
	 -a $f != test_SpeechSynthesizer_alignment.praat	\
	 -a $f != test_Sound_paint_where.praat			\
	 -a $f != test_spectrogramTypes.praat			\
	 -a $f != test_TextGrid_extensions.praat ] ; then
        run $f
    fi
done

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

### Exit with error in case of unit test failures
if [ $test_count != $test_ok ] ; then
    echo "Failed test(s):\n$failed"
    exit 1
fi
