#!/bin/bash

# Description: Script that runs gui tests after the command-line tests have been run
#
# Usage: build_and_run_all_gui_tests [jpeg2000] [pdf]
#
# where: jpeg2000 = run jpeg2000 test(s). Requires CONFIG+=jpeg2000 in qmake build
#        pdf      = run pdf test(s). Requires CONFIG+=pdf in qmake build

FILTERJPEG2000="skip"
FILTERPDF="skip"
while test $# -gt 0
do
    case "$1" in
	jpeg2000) FILTERJPEG2000=""
	    ;;
        pdf) FILTERPDF=""
            ;;
    esac
    shift
done

SCRIPTS=`find ../test -name "*.test.commandline" | sort`

for script in $SCRIPTS
do

    # Skip unwanted tests
    skip=0
    if [[ -n $FILTERJPEG2000 && $script =~ jpeg2000 ]]; then skip=1; fi
    if [[ -n $FILTERPDF      && $script =~ pdf      ]]; then skip=1; fi

    if [ $skip == 0 ]; then

	# Run regression test
	commandLine=`cat $script`
	../bin/engauge $commandLine

	# Compare actual and desired output files. There is one pair for each coordinate system
	scriptfile=`basename $script`
	root=${scriptfile/.test.commandline/}
	EXPECTED_FILES=`find ../test -name "$root.csv_expected_*"`
	COUNT=0
	COUNTER=1
	for EXPECTED in $EXPECTED_FILES
	do
	    let COUNT=COUNT+1;
	done
	for EXPECTED in $EXPECTED_FILES
	do
	    actual=${EXPECTED/expected/actual}
	    if [[ COUNT -gt 1 ]]; 
	    then
		COUNTERSTRING="[$COUNTER/$COUNT]";
	    else
		COUNTERSTRING="";
	    fi
	    if cmp --silent $EXPECTED $actual
	    then
		echo "PASS   : $root $COUNTERSTRING"
	    else
		echo "FAIL   : $root $COUNTERSTRING"
		diff -c2 $EXPECTED $actual
	    fi
	    let COUNTER=COUNTER+1
	done
    fi
done

