#!/usr/bin/env tclsh
#========================================================================
# Generate the framework of a system test report
#========================================================================
#------------------------------------------------------------------------
# This is information that will be part of a global resource file
#------------------------------------------------------------------------
namespace eval LDASCfg {

    proc SiteInit {id info} {
	global ::LDASCfg::SiteInfo

	foreach {key value} $info {
	    set SiteInfo($key,$id) $value
	}
    }

    #--------------------------------------------------------------------
    # Production Systems
    #--------------------------------------------------------------------
    SiteInit ldas-cit \
	[ list \
	      webserver ldas-cit.ligo.caltech.edu ]
    SiteInit ldas-lho \
	[ list \
	      webserver ldas.ligo-wa.caltech.edu ]
    SiteInit ldas-llo \
	[ list \
	      webserver ldas.ligo-la.caltech.edu ]
    SiteInit ldas-mit \
	[ list \
	      webserver ldas.mit.edu ]

    #--------------------------------------------------------------------
    # Proto Test Systems
    #--------------------------------------------------------------------
    SiteInit ldas-dev \
	[ list \
	      webserver ldas-dev.ligo.caltech.edu ]
    SiteInit ldas-test \
	[ list \
	      webserver ldas-test.ligo.caltech.edu ]

    #--------------------------------------------------------------------
    # Tandem Systems
    #--------------------------------------------------------------------
    SiteInit tandem-ii \
	[ list \
	      webserver ldas-suntest2.ligo.caltech.edu ]
    SiteInit tandem-iii \
	[ list \
	      webserver ldas-suntest3.ligo.caltech.edu ]
    SiteInit tandem-iv \
	[ list \
	      webserver ldas-suntest4.ligo.caltech.edu ]
    SiteInit tandem-v \
	[ list \
	      webserver ldas-suntest5.ligo.caltech.edu ]
}
#------------------------------------------------------------------------
# variable safe incr routine.
#------------------------------------------------------------------------
proc incr {varName {amount 1}} {
    upvar 1 $varName var
    if {[info exists var]} {
	set var [expr $var + $amount]
    } else {
	set var $amount
    }
    return $var
}
#------------------------------------------------------------------------
# Information about tests and how to retrieve their results
#------------------------------------------------------------------------
namespace eval TestDB {
    #--------------------------------------------------------------------
    # Establish the order of the tests
    #--------------------------------------------------------------------
    set id 0

    set DATASOCKET [ incr id ]
    set DATABASE_INSERTION [ incr id ]
    set DATABASE_EXCEPTION [ incr id ]
    set DESC_META_DATA [ incr id ]
    set DMT_LOOP [ incr id ]
    set FRAME_ACCESSORS [ incr id ]
    set FRAME_GAP [ incr id ]
    set FRAME_DT_CHECK [ incr id ]
    set FRAME_MULTI_TYPE_SIZE [ incr id ]
    set DBx_MATRIX [ incr id ]
    set WRAPPER_STATE [ incr id ]
    set DATA_PUT_STAND_ALONE_LOOPS [ incr id ]
    set MEMORY_CHECK [ incr id ]
    set DATA_PUT_STAND_ALONE [ incr id ]
    set PUT_STAND_ALONE_EXCEPTION [ incr id ]
    set COMPRESSION [ incr id ]
    set DC_TO_FRAME [ incr id ]
    set DATACOND_MDC [ incr id ]
    set FR_PROC_READ_WRITE [ incr id ]
    set CREATE_RDS [ incr id ]
    set CREATE_RDS_MERGED [ incr id ]
    set CREATE_RDS_LEVEL_2 [ incr id ]
    set RDS_VERIFICATION [ incr id ]
    set RDS_LOOPS [ incr id ]
    set CREATE_RDS_LOOPS_LEVEL_2 [ incr id ]
    set MERGED_RDS_IN_DATA_PIPELINE [ incr id ]
    set NDAS_IN_DATA_PIPELINE [ incr id ]
    set REGRESSION_TESTS [ incr id ]
    set LINK_CHECK [ incr id ]
    set DISKCACHE_MANGLE [ incr id ]
    set COMPLEX_DATA_IN_FRAME [ incr id ]
    set LSYNC_TEST [ incr id ]
    set GEO_IN_DATA_PIPELINE [ incr id ]
    set TEST_ALL_USER_COMMANDS [ incr id ]
    set FRAME_TO_ILWD [ incr id ]
    

    set MAX_TEST [ incr id ]

    proc Init {id info} {
	global ::TestDB::Info

	foreach {key value} $info {
	    set Info($key,$id) $value
	}
    }

    proc UnknownStatus {id} {
	global ::TestDB::Info
	global ::SystemTestLog::SystemTestVersion
	global ::SystemTestLog::SystemTestDate
	global ::SystemTestLog::Tester

	set Info(status,$id) "unknown"
	set Info(version,$id) $SystemTestVersion
	set Info(date,$id) $SystemTestDate
	set Info(tester,$id) $Tester
    }

    Init $DATASOCKET \
	[ list \
	      name {Data Socket} \
	      url /doc/testing/datasock.html \
	      status_command \
	      {::SystemTestLog::SummaryDataSocket} \
	     ]

    Init $DATABASE_EXCEPTION \
	[ list \
	      name {Database Exception / Miscellaneous} \
	      url /doc/testing/miscdatabase.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::DATABASE_EXCEPTION \
		   [list dbException.log] \
	      } \
	     ]

    Init $DATABASE_INSERTION \
	[ list \
	      name {Database Insertion} \
	      url /doc/testing/metadata.html \
	      status_command \
	      {::SystemTestLog::SummaryDBPerformance}
	     ]

    Init $DESC_META_DATA \
	[ list \
	      name {descMetaData} \
	      url /doc/testing/descMetaData.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::DESC_META_DATA \
		   [list descMetaData.log] \
	       } \
	     ]

    Init $DMT_LOOP \
	[ list \
	      name {DMT Loop} \
	      url /doc/testing/dmtLoop.html \
	      status_command {::TestDB::UnknownStatus $::TestDB::DMT_LOOP} \
	     ]

    Init $FRAME_ACCESSORS \
	[ list \
	      name {Frame Accessors} \
	      url /doc/testing/frammeAccessors.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::FRAME_ACCESSORS \
		   [list fracc6.log] \
	       } \
	     ]

    Init $FRAME_GAP \
	[ list \
	      name {Frame Gap} \
	      url /doc/testing/frameGaps.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::FRAME_GAP \
		   [list frgap-16.log frgap-18.log frgap-48.log frgap-80.log] \
	       } \
	     ]

    Init $FRAME_DT_CHECK \
	[ list \
	      name {Frame dt Check} \
	      url /doc/testing/frameDTCheck.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::FRAME_DT_CHECK \
		   [list dtcheck.log] \
	       } \
	     ]

    Init $FRAME_MULTI_TYPE_SIZE \
	[ list \
	      name {Frame Multi-Type/Size} \
	      url /doc/testing/frameFrtest.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::FRAME_MULTI_TYPE_SIZE \
		   [list frtest.log] \
	       } \
	     ]

    Init $DBx_MATRIX \
	[ list \
	      name {db* Matrix} \
	      url /doc/testing/dbMatrix.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::DBx_MATRIX \
		   [list \
			dbntuple.log \
			dbqchannel.log \
			dbquery.log \
			dbspectrum.log \
		       ] \
	       } \
	     ]

    Init $WRAPPER_STATE \
	[ list \
	      name {Wrapper State} \
	      url /doc/testing/wrapperState.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::WRAPPER_STATE \
		   [list wrapperstate.log] \
	       } \
	     ]

    Init $DATA_PUT_STAND_ALONE_LOOPS \
	[ list \
	      name {data/putStandAlone Loops} \
	      url /doc/testing/dataPipeline.html \
	      status_command \
	      {::TestDB::UnknownStatus $::TestDB::DATA_PUT_STAND_ALONE_LOOPS} \
	     ]

    Init $MEMORY_CHECK \
	[ list \
	      name {Memory Check} \
	      url /doc/testing/dcmean.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::MEMORY_CHECK \
		   [list dcmean.log] \
	       } \
	     ]

    Init $DATA_PUT_STAND_ALONE \
	[ list \
	      name {data/putStandAlone} \
	      url /doc/testing/data-putStandAlone.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::DATA_PUT_STAND_ALONE \
		   [list putStandAlone.log] \
	       } \
	     ]

    #------------------------------------------------------------
    # This is wrapped up in the dataPutStandAlone test
    #------------------------------------------------------------
    Init $PUT_STAND_ALONE_EXCEPTION \
	[ list \
	      name {putStandAlone Exception} \
	      url /doc/testing/putStandAlone.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::PUT_STAND_ALONE_EXCEPTION \
		   [list putStandAlone.log] \
	       } \
	     ]

    Init $COMPRESSION \
	[ list \
	      name {Compression} \
	      url /doc/testing/compression.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::COMPRESSION \
		   [list compresstest.log] \
	       } \
	     ]

    Init $DC_TO_FRAME \
	[ list \
	      name {DC to Frame} \
	      url /doc/testing/dc2frame.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::DC_TO_FRAME \
		   [list dc2frame.log] \
	       } \
	     ]

    Init $DATACOND_MDC \
	[ list \
	      name {Datacond MDC} \
	      url /doc/testing/TestingDataCondMDC.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::DATACOND_MDC \
		   [list datacondMDC.log] \
	       } \
	     ]

    Init $FR_PROC_READ_WRITE \
	[ list \
	      name {FrProc Read/Write} \
	      url /doc/testing/.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::FR_PROC_READ_WRITE \
		   [list procReadWrite.log] \
	       } \
	     ]

    Init $CREATE_RDS \
	[ list \
	      name {createRDS} \
	      url /doc/testing/createRDS.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::CREATE_RDS \
		   [list createRDSTest_S3.log] \
	       } \
	     ]

    Init $CREATE_RDS_MERGED \
	[ list \
	      name {createRDS - merged} \
	      url /doc/testing/createRDS.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::CREATE_RDS_MERGED \
		   [list createRDSTest_S3.log] \
	       } \
	     ]

    Init $CREATE_RDS_LEVEL_2 \
	[ list \
	      name {createRDS - level 2} \
	      url /doc/testing/createRDS.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::CREATE_RDS_LEVEL_2 \
		   [list createRDSTest_S3.log] \
	       } \
	     ]

    Init $RDS_VERIFICATION \
	[ list \
	      name {RDS verification} \
	      url /doc/testing/verifyRDS.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::RDS_VERIFICATION \
		   [list RDSVerify.log] \
	       } \
	     ]

    Init $RDS_LOOPS \
	[ list \
	      name {RDS Loops} \
	      url /doc/testing/createRDS.html \
	      status_command {::TestDB::UnknownStatus $::TestDB::RDS_LOOPS} \
	     ]

    Init $CREATE_RDS_LOOPS_LEVEL_2 \
	[ list \
	      name {Level 2 Loops} \
	      url /doc/testing/createRDS.html \
	      status_command {::TestDB::UnknownStatus $::TestDB::CREATE_RDS_LOOPS_LEVEL_2} \
	     ]

    Init $MERGED_RDS_IN_DATA_PIPELINE \
	[ list \
	      name {Merged RDS in dataPipeline} \
	      url /doc/testing/createRDS.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::MERGED_RDS_IN_DATA_PIPELINE \
		   [list createRDSTest_S3.log] \
	       } \
	     ]

    Init $NDAS_IN_DATA_PIPELINE \
	[ list \
	      name {NDAS in dataPipeline} \
	      url /doc/testing/ndasframes.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::NDAS_IN_DATA_PIPELINE \
		   [list ndastest.log] \
	       } \
	     ]

    Init $REGRESSION_TESTS \
	[ list \
	      name {Regression tests} \
	      url /doc/testing/regression.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::REGRESSION_TESTS \
		   [list regression.log] \
	       } \
	     ]

    Init $LINK_CHECK \
	[ list \
	      name {Link Check} \
	      url /doc/testing/linkCheck.html \
	      status_command \
	      {::SystemTestLog::SummaryLinkCheck} \
	     ]

    Init $DISKCACHE_MANGLE \
	[ list \
	      name {Diskcache mangle} \
	      url /doc/testing/dcmangle.html \
	      status_command {::TestDB::UnknownStatus $::TestDB::DISKCACHE_MANGLE} \
	     ]

    Init $COMPLEX_DATA_IN_FRAME \
	[ list \
	      name {Complex data in frames} \
	      url /doc/testing/complex_data.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::COMPLEX_DATA_IN_FRAME \
		   [list complex_data.log] \
	       } \
	     ]

    Init $LSYNC_TEST \
	[ list \
	      name {Lsync Test} \
	      url /doc/testing/lsync.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::LSYNC_TEST \
		   [list lsyncTest.log] \
	       } \
	     ]

    Init $GEO_IN_DATA_PIPELINE \
	[ list \
	      name {GEO in dataPipeline} \
	      url /doc/testing/geotest.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::GEO_IN_DATA_PIPELINE \
		   [list geotest.log] \
	       } \
	     ]

    Init $TEST_ALL_USER_COMMANDS \
	[ list \
	      name {Test all user commands} \
	      url /doc/testing/user_commands.html \
	      status_command {::TestDB::UnknownStatus $::TestDB::TEST_ALL_USER_COMMANDS} \
	     ]

    Init $FRAME_TO_ILWD \
	[ list \
	      name {Frame to ilwd} \
	      url /doc/testing/frame2ilwd.html \
	      status_command \
	      {::SystemTestLog::Summary $::TestDB::FRAME_TO_ILWD \
		   [list fr2ilwd.log] \
	       } \
	     ]
}


#------------------------------------------------------------------------
# Collect information about the system on which we are running
#------------------------------------------------------------------------
set ::LDASName unknown

if {[file readable /etc/ldasname]} {
    #--------------------------------------------------------------------
    # In the case that there is a ldasnaem file, use the name
    #--------------------------------------------------------------------
    set fd [open /etc/ldasname]
    set ::LDASName [read -nonewline $fd]
    close $fd
}
#------------------------------------------------------------------------
# Find the last system report and gather information from it
#------------------------------------------------------------------------
namespace eval SystemTestLog {
    #--------------------------------------------------------------------
    # 
    #--------------------------------------------------------------------
    set Tester {Edward Maros}

    proc Summary {id keys} {
	global ::SystemTestLog::SystemTestVersion
	global ::SystemTestLog::SystemTestDate
	global ::SystemTestLog::Stats
	global ::SystemTestLog::Tester

	global ::TestDB::Info

	set total 0
	foreach key $keys {
	    foreach type [list total pass skip fail] {
		catch {incr $type $Stats($type,$key)} err
	    }
	}
	if { $total == 0 } {
	    set status "unknown"
	} elseif { $fail > 0 } {
	    set status "fail"
	    set comment "<b>NOTE:</b> $fail of $total job(s) Failed<br>"
	} elseif { $skip == $total } {
	    set status "skip"
	} else {
	    set status "pass"
	}

	set Info(status,$id) $status
	set Info(version,$id) $SystemTestVersion
	set Info(date,$id) $SystemTestDate
	set Info(tester,$id) $Tester
	if {[info exists comment]} {
	    if {[info exists Info(comment,$id)]} {
		set Info(comment,$id) "$Info(comment,$id)$comment"
	    } else {
		set Info(comment,$id) "$comment"
	    }
	}
    }

    proc SummaryDataSocket { } {
	#----------------------------------------------------------------
	# local variables
	#----------------------------------------------------------------
	set id $::TestDB::DATASOCKET
	#----------------------------------------------------------------
	# Collect the general summary information
	#----------------------------------------------------------------
	Summary $id [list datasockTest.log]
	#----------------------------------------------------------------
	# Retrieve additional information for performance table
	#----------------------------------------------------------------
	global ::SystemTestLog::Month
	global ::SystemTestLog::Day
	global ::TestDB::Info
	set logdir "/ldas_outgoing/tmp/datasock/results-${Month}${Day}"

	set overall_stat_files \
	    [glob -- $logdir/*/overall.stats]
	set comment ""
	foreach f $overall_stat_files {
	    set fd [open $f]
	    set contents [read $fd]
	    close $fd
	    set count 0
	    if { [regexp {.*/190/.*} $f] } {
		set v "1.9.0 (2007.07.18)"
	    } elseif { [regexp {.*/180/.*} $f] } {
		set v "1.8.0 (2005.10.31)"
	    } elseif { [regexp {.*/170/.*} $f] } {
		set v "1.7.0 (2005.07.27)"
	    } else {
		set v "Unknown"
	    }

	    set comment "$comment<font size=\"-1\">\n<table border=\"1\">\n"
	    set comment "$comment<caption>Compared to $v</caption>\n"
	    set comment "$comment<tr><th><th>New Avg<th>Old Avg<th>Analysis\n</tr>"

	    foreach line [split $contents \n] {
		incr count
		if { $count <= 4} {
		    continue
		}
		if { $count <= 7 } {
		    if { [regexp {^[[:space:]]*(.*)[[:space:]]+([.[:digit:]]+)[[:space:]]+([.[:digit:]]+)[[:space:]]+([.[:digit:]]+%.*)$} $line match desc na oa a] } {
			set comment "$comment<tr><th>$desc<td align=\"right\">$na<td align=\"right\">$oa<td align=\"right\">$a</tr>\n"
		    }
		    continue
		}
		break
	    }
	    set comment "${comment}</table>\n</font>\n"
	}
	if {[info exists Info(comment,$id)]} {
	    set Info(comment,$id) "$Info(comment,$id)<br>$comment"
	} else {
	    set Info(comment,$id) "$comment"
	}

    }

    proc SummaryDBPerformance { } {
	#----------------------------------------------------------------
	# local variables
	#----------------------------------------------------------------
	set id $::TestDB::DATABASE_INSERTION
	#----------------------------------------------------------------
	# Collect the general summary information
	#----------------------------------------------------------------
	Summary $id \
	    [list \
		 dbperformance-run.lo \
		 dbperformance-stats. \
		 dbperformance-valida \
		]

	#----------------------------------------------------------------
	# Retrieve additional information
	#----------------------------------------------------------------
	global ::SystemTestLog::SystemTestDirectory
	global ::TestDB::Info

	if { [file readable "${SystemTestDirectory}/dbperformance_stats.html"] } {
	    set fd [open "${SystemTestDirectory}/dbperformance_stats.html"]
	    set comment [read $fd]
	    close $fd
	    if {[info exists Info(comment,$id)]} {
		set Info(comment,$id) "$Info(comment,$id)<br>$comment"
	    } else {
		set Info(comment,$id) "$comment"
	    }
	}
    }

    proc SummaryLinkCheck { } {
	#----------------------------------------------------------------
	# local variables
	#----------------------------------------------------------------
	set id $::TestDB::LINK_CHECK
	#----------------------------------------------------------------
	# Collect the general summary information
	#----------------------------------------------------------------
	Summary $id [list crawlink.log]

	#----------------------------------------------------------------
	# Retrieve additional information
	#----------------------------------------------------------------
	global ::TestDB::Info

	set comment ""
	set logdir "/ldas_outgoing/logs"

	if { [file readable "$logdir/crawlink.log"] } {
	    set comment "${comment}<a href=\"$logdir/crawlink.log\">List of bad links</a><br />\n"
	}
	if { [file readable "$logdir/crawlink.html"] } {
	    set comment "${comment}<a href=\"$logdir/crawlink.html\">Alternate report</a><br />\n"
	}
	if { [file readable "$logdir/crawlink.diff"] } {
	    set comment "${comment}<a href=\"$logdir/crawlink.diff\">Different from last week</a><br />\n"
	}
	if {[info exists Info(comment,$id)]} {
	    set Info(comment,$id) "$Info(comment,$id)<br>$comment"
	} else {
	    set Info(comment,$id) "$comment"
	}
    }

    #--------------------------------------------------------------------
    # Gather the stats information
    #--------------------------------------------------------------------
    proc stats {filename} {
	global ::SystemTestLog::Stats

	set fd [open $filename]
	set log {[[:space:]]*([^[:space:]:]+)}
	set num {[[:space:]]+([[:digit:]]+)}
	set time {[[:space:]]+([[:digit:]]+:[[:digit:]]+:[[:digit:]]+)}
	set pattern "^${log}:${num}${num}${num}${num}${time}"
	foreach line [split [read $fd] \n] {
	    regexp "^$pattern" $line \
		match \
		key total pass skip fail wall_time
	    if { [info exists match] } {
		incr Stats(total,$key) $total
		incr Stats(pass,$key) $pass
		incr Stats(skip,$key) $skip
		incr Stats(fail,$key) $fail
	    }
	}
	close $fd
    }

    #--------------------------------------------------------------------
    # Specialized sort to put the latest system testing file at the
    #   front of the list
    #--------------------------------------------------------------------
    proc stsort {a b} {
	global ::SystemTestLog::pattern

	regexp $pattern $a \
	    amatch \
	    amajor aminor apatch \
	    ayear amonth aday \
	    ahour amin asec
	regexp $pattern $b \
	    bmatch \
	    bmajor bminor bpatch \
	    byear bmonth bday \
	    bhour bmin bsec

	trim_zeroes \
	    ayear amonth aday ahour amin asec \
	    byear bmonth bday bhour bmin bsec

	if { $amajor != $bmajor } {
	    return [expr $bmajor - $amajor]
	}
	if { $aminor != $bminor } {
	    return [expr $bminor - $aminor]
	}
	if { $apatch != $bpatch } {
	    return [expr $bpatch - $apatch]
	}
	if { $ayear != $byear } {
	    return [expr $byear - $ayear]
	}
	if { $amonth != $bmonth } {
	    return [expr $bmonth - $amonth]
	}
	if { $aday != $bday } {
	    return [expr $bday - $aday]
	}
	if { $ahour != $bhour } {
	    return [expr $bhour - $ahour]
	}
	if { $amin != $bmin } {
	    return [expr $bmin - $amin]
	}
	return [expr $bsec - $asec]
    }

    #--------------------------------------------------------------------
    # Trim leading zeroes from decimal numbers
    #--------------------------------------------------------------------
    proc trim_zeroes { args } {
	foreach v $args {
	    upvar $v a
	    set a [ string trimleft $a 0 ]
	    if { [ string length $a ] == 0 } {
		set a 0
	    }
	}
    }

    #--------------------------------------------------------------------
    # Check for the system test directory
    #--------------------------------------------------------------------
    if { [llength $::argv] > 0 } {
	#----------------------------------------------------------------
	# User supplied on command line
	#----------------------------------------------------------------
	if { [file isdir [lindex $::argv 0] ] } {
	    set file_list [ list [lindex $::argv 0] ]
	} else {
	    error {[lindex $::argv 0] is not a system test direcory}
	}
    } else {
	#----------------------------------------------------------------
	# Get the latest from the default location
	#----------------------------------------------------------------
	set file_list [glob /ldas_outgoing/tmp/SystemTest-*]
    }

    set ver_pat {([0-9]+)[.]([0-9]+)[.]([0-9]+)}
    set date_pat {([0-9]+)[.]([0-9]+)[.]([0-9]+)}
    set time_pat {([0-9]+)[:]([0-9]+)[:]([0-9]+)}
    set pattern "^.*/SystemTest-${ver_pat}-${date_pat}.${time_pat}"

    set file_list [lsort -command stsort $file_list]

    set SystemTestDirectory [lindex $file_list 0]
    regexp $pattern $SystemTestDirectory \
	match \
	major minor patch \
	Year Month Day \
	hour min sec

    set SystemTestVersion "${major}.${minor}.${patch}"
    set SystemTestDate "${Month}/${Day}/${Year}"

    if { ! [file readable "[lindex $file_list 0]/summary_report.txt"] } {
	error {No read access for file [lindex $file_list 0]/summary_report.txt}
    }
    stats "[lindex $file_list 0]/summary_report.txt"
}

#------------------------------------------------------------------------
# Print the report Header
#------------------------------------------------------------------------
puts {
<?xml version="1.0" encoding="us-ascii"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>LDAS Test Results</title>
    <meta name="generator" content="HTML Tidy for Solaris (vers 1st May 2003), see www.w3.org" />
    <link rel="StyleSheet" href="style.css" type="text/css" />
    <link rel="StyleSheet" href="results.css" type="text/css" />
    <style type="text/css">
    <!--
     -->
    </style>

</head>

<body>
    <h1 >LDAS Test Results <br /></h1>
    <table cellspacing="0" cellpadding="0" border="0" summary="">
        <tr> <!-- Header -->
            <th>Test Name</th>
            <th>Status</th>

            <th>Date</th>
            <th>System</th>
            <th>Version</th>
            <th>Tester</th>
            <th>Notes</th>
        </tr>
}
#------------------------------------------------------------------------
# Print the report body
#------------------------------------------------------------------------
for {set i 1} {$i < $::TestDB::MAX_TEST} {incr i} {
    set name $::TestDB::Info(name,$i)
    set url $::TestDB::Info(url,$i)
    set status unknown
    set statuscode unknown
    set date unknown
    set site $::LDASName
    set site_url "http://$::LDASCfg::SiteInfo(webserver,$site)"
    set version unknown
    set tester unknown
    set comment {}

    if { [info exists ::TestDB::Info(status_command,$i)] } {
	eval $::TestDB::Info(status_command,$i)
    }
    catch {set status [string toupper $::TestDB::Info(status,$i)]} err
    catch {set version $::TestDB::Info(version,$i)} err
    catch {set date $::TestDB::Info(date,$i)} err
    catch {set statuscode [string tolower $status]} err
    catch {set tester $::TestDB::Info(tester,$i)} err
    catch {set comment $::TestDB::Info(comment,$i)} err

    puts        {        <!-- =========================================================== -->}
    puts [subst {        <tr><!-- $name -->}]
    puts [subst {            <!-- Test Name --><td class="name"><a href='$url'>$name</td>}]
    puts [subst {            <!-- Status    --><td class="$statuscode">$status</td>} ]
    puts [subst {            <!-- Date      --><td>$date</td>}]
    puts [subst {            <!-- System    --><td><a href="$site_url">$site</a></td>}]

    puts [subst {            <!-- Version   --><td>$version</td>}]
    puts [subst {            <!-- Tester    --><td>$tester</td>}]
    puts [subst {            <!-- Comments  --><td class="comments">$comment</td>}]
    puts {        </tr>}
}
#------------------------------------------------------------------------
# Print the report Footer
#------------------------------------------------------------------------
puts {
    </table>

    <!--#include virtual="../../footer.html" -->
</body>
</html>
}
