#!/bin/sh

# This is analyzer of output of NPB-style formed tests
# Requires variables: LAUNCH_EXIT_CODE, STDOUT_FN, STDERR_FN
# Produces variables: SUBTEST_COUNT, TEST_PASSED, RESULT_COMMENT, ERROR_LEVEL, TASK_CALC_TIME
# Produces functions: analyze_subtest

SUBTEST_COUNT=`grep 'Completed.' <"$STDOUT_FN" | wc -l`

if [ `grep -E 'Assertion' <"$STDERR_FN" | wc -l` -gt 0 ]; then
    TEST_PASSED=0
    RESULT_COMMENT="Assertion failed"
    ERROR_LEVEL=5
elif [ `grep -E 'RTS fatal' <"$STDERR_FN" | wc -l` -gt 0 ]; then
    TEST_PASSED=0
    RESULT_COMMENT="RTS fatal"
    ERROR_LEVEL=4
elif [ `grep -E 'RTS err' <"$STDERR_FN" | wc -l` -gt 0 ]; then
    TEST_PASSED=0
    RESULT_COMMENT="RTS err"
    ERROR_LEVEL=3
elif [ `grep "END OF" <"$STDOUT_FN" | wc -l` -eq 0 ]; then
    TEST_PASSED=0
    RESULT_COMMENT="Crash"
    ERROR_LEVEL=2
elif [ $LAUNCH_EXIT_CODE -ne 0 ]; then
    TEST_PASSED=0
    RESULT_COMMENT="Launch failure"
    ERROR_LEVEL=6
elif [ `grep ' SUCCESSFUL' <"$STDOUT_FN" | wc -l` -lt $SUBTEST_COUNT ]; then
    TEST_PASSED=0
    RESULT_COMMENT="Has failed subtests"
    ERROR_LEVEL=1
else
    TEST_PASSED=1
    RESULT_COMMENT="OK"
    ERROR_LEVEL=0
fi

if [ $SUBTEST_COUNT -eq 1 ]; then
    TASK_CALC_TIME=`grep 'Time in seconds' <"$STDOUT_FN" | awk '{ print $5 }'`
fi

analyze_subtest() {
    # Produces variables: SUBTEST_NAME, TEST_PASSED, RESULT_COMMENT, ERROR_LEVEL, TASK_CALC_TIME
    local SUBTEST_RES_START=`grep -n 'Completed.' <"$STDOUT_FN" | head -n $1 | tail -n 1 | sed 's/:.*//g'`
    local SUBTEST_RES_END=
    if [ $1 -lt $SUBTEST_COUNT ]; then
        SUBTEST_RES_END=`grep -n 'Completed.' <"$STDOUT_FN" | head -n $(( $1 + 1 )) | tail -n 1 | sed 's/:.*//g'`
        SUBTEST_RES_END=$(( SUBTEST_RES_END - 1))
    else
        SUBTEST_RES_END=`cat "$STDOUT_FN" | wc -l`
    fi
    local linecount=$(( SUBTEST_RES_END - SUBTEST_RES_START + 1 ))
    local tmp=`mktemp`
    cat "$STDOUT_FN" | head -n $SUBTEST_RES_END | tail -n $linecount >$tmp
    SUBTEST_NAME=`grep "Completed." <$tmp | head -n 1 | awk '{ print $1 }'`
    local CLASS_NAME=`grep "Class" <$tmp | head -n 1 | awk '{ print $3 }'`
    if [ -n "$CLASS_NAME" ]; then
        SUBTEST_NAME="$SUBTEST_NAME ($CLASS_NAME)"
    fi
    TEST_PASSED=`grep "Verification" <$tmp | head -n 1 | awk '{ print $3 }'`
    if [ "$TEST_PASSED" = "SUCCESSFUL" ]; then
        TEST_PASSED=1
        RESULT_COMMENT="OK"
        ERROR_LEVEL=0
    else
        TEST_PASSED=0
        RESULT_COMMENT="Subtest failed"
        ERROR_LEVEL=1
    fi
    TASK_CALC_TIME=`grep 'Time in seconds' <$tmp | awk '{ print $5 }'`
    rm $tmp
}
