#!/bin/sh
# -*- tcl -*- \
exec tclsh "$0" ${1+"$@"}

# Extract result (was, should have been aka is/expect)
# from a testsuite result log.

set log [lindex $argv 0]
set log [open $log r]
set log [split [read $log][close $log] \n]

set logbase [file rootname [lindex $argv 0]]

set test {}
set buffer {}
set resis 0
set resex 0

foreach line $log {
    if {$resis} {
	if {[string match {---- Result should*} $line]} {
	    set resis 0
	    set resex 1
	    set    out [open $logbase.${test}.is w]
	    puts  $out [join $buffer \n]
	    close $out
	    set buffer {}

	    puts \tGet\ expected\ result\ ...
	    continue
	}
	lappend buffer $line
	continue
    }
    if {$resex} {
	if {[string match "==== ${test}*" $line]} {
	    set resis 0
	    set resex 0
	    set    out [open $logbase.${test}.ex w]
	    puts  $out [join $buffer \n]
	    close $out
	    set buffer {}

	    puts Completing\ $test\ ...
	    continue
	}
	lappend buffer $line
	continue
    }
    if {[string match ====* $line] && [string match *FAILED $line]} {
	set test [lindex [split $line] 1]

	puts Extracting\ $test\ ...
	continue
    }
    if {[string match {---- Result was*} $line]} {
	# Collect actual result
	set resis 1

	puts \tGet\ actual\ result\ ...
	continue
    }
    # ignore line ...
}
