#!/bin/sh
# Copyright 1999-2002 Carnegie Mellon University.  
# Portions Copyright 2002 Sun Microsystems, Inc.  
# Portions Copyright 2002 Mitsubishi ElectricResearch Laboratories.
# All Rights Reserved.  Use is subject to license terms.
# 
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL 
# WARRANTIES.
#
# This script parses the output of S4 and generates a log entry
#
# usage: parseS4LiveOutput logfile label [testStatus]
#
# if testStatus is not given, we assume that we are running
# interactively and will generate an OK testStatus and will use the
# current date and time.
#

gawk '

BEGIN {
	
    if (ARGC == 4) {
	status = ARGV[3];
	ARGV[3] = "";
	# use date/time that the test was started so we can
	# more easily track down CVS history
	getline date < ".startDate";
	getline time < ".startTime";
    } else if (ARGC == 3) {
        status = "OK";
	"date +%Y-%m-%d" | getline date;
	 "date +%T" | getline time;
    } else {
        print "Usage: parseS4Output  logfile label [status]"
	exit
    }

    testName = ARGV[2];
    ARGV[2] = "";

# Here is a sample of what we are parsing
#
# ------------- Summary Statistics -------------
#   Accuracy: 60.227%    Errors: 45  (Sub: 31  Ins: 10  Del: 4)
#   Words: 88   Matches: 53    WER: 51.136%
#   Sentences: 1   Matches: 0   SentenceAcc: 0.000%
#   Total Time Audio: 44.56s  Proc: 31.73s  Speed: 0.71 X real time
#   Response Time:  Avg: 0.021125s  Max: 0.285s  Min: 0.0020s
#   Mem  Total: 126.62 Mb  Free: 107.87 Mb
#   Used: This: 18.75 Mb  Avg: 13.46 Mb  Max: 18.75 Mb
#   Utterances:  Actual: 24  Found: 24
#   Gap Insertions: 10



    while (getline x < ARGV[1] > 0) {
	gsub(/\(/, "", x);
	gsub(/\)/, "", x);
	split(x, arry);
	
	if (summaryFound == 0) {
	    if (arry[3] == "Summary") {
		summaryFound = 1;
	    } 
	} else {
	    if (arry[3] == "Audio:") {
	        audioTime = arry[4];
	        procTime = arry[6];
			gsub(/s/, "", audioTime);
			gsub(/s/, "", procTime);
	    } else if (arry[1] == "Used:" && arry[2] == "This:") {
	        heapSize = arry[6];
	    } else if (arry[1] == "Words:") {
	        words = arry[2];
	    } else if (arry[1] == "Accuracy:") {
	        substitutions = arry[6];
	        insertions = arry[8];
	        deletions = arry[10];
	    } else if (arry[1] == "Sentences:") {
	        sentences = arry[2];
	        correctSentences = arry[4];
	    } else if (arry[1] == "Utterances:") {
                actualUtterances = arry[3];
                foundUtterances = arry[5];
            } else if (arry[1] == "Gap" && arry[2] == "Insertions:") {
                gapInsertions = arry[3];
            } else if (arry[1] == "Response") {
                avgResponseTime = arry[4];
                maxResponseTime = arry[6];
                minResponseTime = arry[8];
                gsub(/s/, "", avgResponseTime);
                gsub(/s/, "", maxResponseTime);
                gsub(/s/, "", minResponseTime);
            }
	}
    }
    close(ARGV[1]);
}

END {
    # "date +%D" | getline date;
    #"date +%Y-%m-%d" | getline date;
    # "date +%T" | getline time;


    "./getHostname" | getline machine;
    "uptime" | getline uptime;
    "whoami" | getline who;

    count = split(uptime, arr, " ");
    loadAverage = arr[count]

    printf("live_test" "|");
    printf(date  "|");
    printf(time  "|");
    printf(machine "|");
    printf("s4" "|");
    printf(testName "|");
    printf(who "|");
    printf(status "|");
    printf(audioTime "|");
    printf(procTime "|");
    printf(words "|");
    printf(insertions "|");
    printf(deletions "|");
    printf(substitutions "|");
    printf(sentences "|");
    printf(correctSentences "|");
    printf(heapSize "|");
    printf(loadAverage "|");
    printf(actualUtterances "|");
    printf(foundUtterances "|");
    printf(gapInsertions "|");
    printf(avgResponseTime "|");
    printf(maxResponseTime "|");
    printf(minResponseTime "|");
    printf("\n");
}

'  $*
