## Process this fill with automake to generate Makefile.in
# 
# Copyright (C) 2009, 2010 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

load_lib "dejagnu.exp"


# If tracing has been enabled at the top level, then turn it on here
# too.
if $tracelevel {
    strace $tracelevel
}

# After these many seconds of execution the test script is aborted with a failure.
# This is to handle deadlocks. We don't reset the timeout when a match is
# found to avoid hanging in case of a testcase sending matches in an infinite loops.
# (not unlikely as it seems, think about flash movies...)
#
# Expressed in seconds.
#
set timeout 600
set file all
set params ""

global env
set env(LANG) en_US.UTF-8
set env(LANGUAGE) en_US.UTF-8
set env(LC_ALL) en_US.UTF-8

set gprocessor [lookfor_file $objdir utilities/gprocessor]

set testcases [glob -nocomplain *.swf]

# testcases is set by the Makefile in the site.exp data file.
foreach file $testcases {

    verbose "Running test $gprocessor -v $file"

    # spawn the executable and look for the DejaGnu output messages from the
    # test case.

    # this version of the call allows use of 'wait' to check return code
    # -open [open "|cmd" "r"] doesn't work for that
  
    # Ignore SIGHUP or we'd get a lot of them on Debian stable
    spawn -noecho -ignore SIGHUP $gprocessor -v ./$file 

    expect {
	-re "^\[^\n]*NOTE:\[^\n]*\n" {
	    regsub ".*NOTE: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    verbose "${file} $output" 
            # notes tipically come from the test runner, so we'll trust it to mean 
            # things are someone not too bad...
	    # -continue_timer
	    exp_continue
	}
	-re "^\[^\n]*XPASSED:\[^\n]*\n" {
	    regsub ".*XPASSED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    xpass "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*PASSED:\[^\n]*\n" {
	    regsub ".*PASSED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    pass "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*XFAILED:\[^\n]*\n" {
	    regsub ".*XFAILED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2] 
	    xfail "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*FAILED:\[^\n]*\n" {
	    regsub ".*FAILED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2] 
	    fail "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*UNTESTED:\[^\n]*\n" {
	    regsub ".*UNTESTED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    untested "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*UNRESOLVED:\[^\n]*\n" {
	    regsub ".*UNRESOLVED: " $expect_out(0,string) "" output
	    set output [string range $output 0 end-2]
	    unresolved "${file}: $output"
	    exp_continue -continue_timer
	}
	-re "^\[^\n]*\n" {
            # just remove non-matching lines!
            exp_continue -continue_timer
	}
	eof {
	    #	    unresolved "${file} died prematurely"
	    #	    catch close
	    #	    return "${file} died prematurely"
	}
	timeout {
		fail "Test case ${file} still running after ${timeout} seconds, killing it (deadlock?)"
		catch close
		continue;
	}
   }

	# wait for the process to coplete to
	# check return code
	set retcode [wait]

	# debugging
	#set i 0; foreach j $retcode { print "${file} wait($i) $j"; incr i }

	# This snippet catches segfaults and aborts.
	# Would also catch SIGHUP, but we're ignoring them
	# as on Debian Stable we unexpectedly get them for no apparent reason
	#
	if { [ llength $retcode ] > 5 } {
		fail "${file} died prematurely ([lindex $retcode 6])"
	}

	# This snippet catches non-zero returns
	if { [ lindex $retcode 3 ] != 0 } {
		fail "${file} exited with non-zero code ([lindex $retcode 3])"
	}



    # force a close of the executable to be safe.
    catch close
}


