#!/bin/bash
# Bash is required due to usage of associative arrays

MY_DIR=$(cd "$(dirname "$(which "$0")")" && pwd)
SAVE_DIR=`pwd`

TEST_SUITE="$1"
RESULTS_DIR="$2"
FULL_REP_URL="$3"
REV_NUMBER=$4

if [ -z "$FULL_REP_URL" ]; then
    FULL_REP_URL="full-report.html"
fi

if [ -z "$REV_NUMBER" ]; then
    REV_NUMBER=UNKNOWN
fi

MAX_LONELY_OK=50
REPORT_DIR="$RESULTS_DIR/report"
rm -rf "$REPORT_DIR"
RES_FILES=`mktemp`
find "$RESULTS_DIR" -mindepth 1 -type f | LC_ALL=C sort >$RES_FILES
BUGGY_FILE_DIR="$REPORT_DIR/sources"
mkdir -p "$BUGGY_FILE_DIR"
REPORT_FILE="$REPORT_DIR/brief-report.html"
FULL_REPORT_FILE="$REPORT_DIR/full-report.html"

COL_COUNT=2
PLATFORMS=
HAS_SUBTESTS=0
TOTAL_LAUNCHES=0
TOTAL_ERROR_LAUNCHES=0

while IFS= read -r f; do
    CUR_DEPTH=0
    TEST_SHORT_PATH=`basename "$f" .result`
    TMPSTR=`dirname "$f"`
    while [ "$TMPSTR" != "$RESULTS_DIR" ]; do
        CUR_DEPTH=$(( $CUR_DEPTH + 1 ))
        TEST_SHORT_PATH="$(basename "$TMPSTR")/$TEST_SHORT_PATH"
        TMPSTR=`dirname "$TMPSTR"`
    done
    IS_SUBTEST=0
    if [ ! -e "$TEST_SUITE/$TEST_SHORT_PATH" ]; then
        HAS_SUBTESTS=1
        IS_SUBTEST=1
    fi
    if [ $(( CUR_DEPTH + 2 )) -gt $COL_COUNT ]; then
        COL_COUNT=$(( $CUR_DEPTH + 2 ))
    fi
    if [ $IS_SUBTEST -eq 0 ]; then
        while IFS= read -r lin; do
            eval $lin
            if [ -z "$PLATFORMS" ]; then
                PLATFORMS=$PLATFORM
            else
                FOUND_FLAG=0
                for platf in $PLATFORMS; do
                    if [ $platf = $PLATFORM ]; then
                        FOUND_FLAG=1
                    fi
                done
                if [ $FOUND_FLAG -eq 0 ]; then
                    PLATFORMS="$PLATFORMS $PLATFORM"
                fi
            fi
            TOTAL_LAUNCHES=$(( $TOTAL_LAUNCHES + 1 ))
            if [ "$ERROR_LEVEL" != "0" ]; then
                TOTAL_ERROR_LAUNCHES=$(( $TOTAL_ERROR_LAUNCHES + 1 ))
            fi
        done <"$f"
    fi
done <$RES_FILES

CAT_COUNT=$(( COL_COUNT - 1 - HAS_SUBTESTS - 1 ))

exec 5>"$REPORT_FILE"
exec 6>"$FULL_REPORT_FILE"

echo "<html>" >& 5
echo "<html>" >& 6
echo "<head>" >& 5
echo "<head>" >& 6
echo "<title>Test results for DVM-system. Revision $REV_NUMBER.</title>" >& 5
echo "<title>Test results for DVM-system. Revision $REV_NUMBER.</title>" >& 6
echo "<style>" >& 6
cat "$MY_DIR/report.css" >& 6
echo "</style>" >& 6
echo "<script type='text/javascript'>" >& 6
cat "$MY_DIR/report.js" >& 6
echo "</script>" >& 6
echo "</head>" >& 5
echo "</head>" >& 6
echo "<body>" >& 5
echo "<body>" >& 6
echo "<h1 align=center>Test results for DVM-system. Revision $REV_NUMBER.</h1>" >& 5
echo "<h1 align=center>Test results for DVM-system. Revision $REV_NUMBER.</h1>" >& 6
echo "<h2 align=center>Tested on platforms: $PLATFORMS.</h2>" >& 5
echo "<h2 align=center>Tested on platforms: $PLATFORMS.</h2>" >& 6
echo "<h3 align=center>Full report can be seen on <a href='$FULL_REP_URL'>$FULL_REP_URL</a></h3>" >& 5
echo "<p align=center>Launches with errors: $TOTAL_ERROR_LAUNCHES / $TOTAL_LAUNCHES</p>" >& 5
echo "<p align=center>Launches with errors: $TOTAL_ERROR_LAUNCHES / $TOTAL_LAUNCHES</p>" >& 6
echo "<h3 align=center><a href='sources.tgz'>Download sources of buggy tests</a></h3>" >& 6
echo "<table border=1 cellspacing=0 align=center>" >& 5
echo "<table border=1 cellspacing=0 align=center>" >& 6
echo "<tr>" >& 5
echo "<tr>" >& 6
CUR_COL=0
while [ $CUR_COL -lt $CAT_COUNT ]; do
    echo "<th align=center>Category</th>" >& 5
    echo "<th>Category</th>" >& 6
    CUR_COL=$(( CUR_COL + 1 ))
done
echo "<th align=center>Test name</th>" >& 5
echo "<th>Test name</th>" >& 6
if [ $HAS_SUBTESTS -ne 0 ]; then
    echo "<th>Subtest</th>" >& 6
fi
echo "<th align=center>Test result</th>" >& 5
echo "<th>Test result</th>" >& 6
echo "</tr>" >& 5
echo "</tr>" >& 6

output_cat_recursive()
{
    if [ `basename "$1"` != "$1" ]; then
        output_cat_recursive `dirname "$1"`
    fi
    if [ $TO_BRIEF -ne 0 ]; then
        echo "<td align=center>" >& 5
        basename "$1" >& 5
        echo "</td>" >& 5
    fi
    echo "<td>" >& 6
    basename "$1" >& 6
    echo "</td>" >& 6
    FILLED_COLS=$(( FILLED_COLS + 1 ))
    if [ $FILLED_COLS -eq 1 -a `basename "$1"` = "Performance" ]; then
        FORCE_TABLE=1
    fi
}

output_cat()
{
    FILLED_COLS=0
    output_cat_recursive "$1"
    while [ $FILLED_COLS -lt $CAT_COUNT ]; do
        if [ $TO_BRIEF -ne 0 ]; then
            echo "<td>&nbsp;</td>" >& 5
        fi
        echo "<td>&nbsp;</td>" >& 6
        FILLED_COLS=$(( FILLED_COLS + 1 ))
    done
}

nextDetailsId=1

while IFS= read -r f; do
    CUR_DEPTH=0
    TEST_SHORT_PATH=`basename "$f" .result`
    TMPSTR=`dirname "$f"`
    while [ "$TMPSTR" != "$RESULTS_DIR" ]; do
        CUR_DEPTH=$(( $CUR_DEPTH + 1 ))
        TEST_SHORT_PATH="$(basename "$TMPSTR")/$TEST_SHORT_PATH"
        TMPSTR=`dirname "$TMPSTR"`
    done
    SUBTEST_NAME=
    if [ ! -e "$TEST_SUITE/$TEST_SHORT_PATH" ]; then
        SUBTEST_NAME=`basename "$TEST_SHORT_PATH"`
        TEST_SHORT_PATH=`dirname "$TEST_SHORT_PATH"`
    fi
    HAS_FAILS=0
    if [ `grep "TEST_PASSED=0" <"$f" | wc -l` -gt 0 ]; then
        HAS_FAILS=1
        if [ ! -e "$BUGGY_FILE_DIR/$TEST_SHORT_PATH" ]; then
            mkdir -p `dirname "$BUGGY_FILE_DIR/$TEST_SHORT_PATH"`
            cp -ur "$TEST_SUITE/$TEST_SHORT_PATH" "$BUGGY_FILE_DIR/$TEST_SHORT_PATH"
        fi
    fi
    TO_BRIEF=1
    if [ -n "$SUBTEST_NAME" -o $HAS_FAILS -eq 0 ]; then
        TO_BRIEF=0
    fi
    if [ $TO_BRIEF -ne 0 ]; then
        echo "<tr>" >& 5
    fi
    echo "<tr>" >& 6
    FORCE_TABLE=0
    output_cat `dirname "$TEST_SHORT_PATH"`
    if [ $TO_BRIEF -ne 0 ]; then
        echo "<td align=center>" >& 5
        echo `basename "$TEST_SHORT_PATH"` >& 5
        echo "</td>" >& 5
    fi
    if [ -n "$SUBTEST_NAME" ]; then
        echo "<td>" >& 6
        echo `basename "$TEST_SHORT_PATH"` >& 6
        echo "</td>" >& 6
        echo "<td>" >& 6
        echo "$SUBTEST_NAME" >& 6
        echo "</td>" >& 6
    else
        echo "<td colspan=$((1 + HAS_SUBTESTS))>" >& 6
        echo `basename "$TEST_SHORT_PATH"` >& 6
        echo "</td>" >& 6
    fi
    ERROR_LEVELS=$(
    while IFS= read -r lin; do
        eval $lin
        if [ -z "$ERROR_LEVEL" ]; then
            ERROR_LEVEL=0
        fi
        echo $ERROR_LEVEL
    done <"$f" | sort -unr)
    if [ $TO_BRIEF -ne 0 ]; then
        echo "<td align=center>" >& 5
    fi
    echo "<td>" >& 6
    LAUNCH_COUNT=`wc -l <"$f"`
#    echo "$LAUNCH_COUNT total" >& 5
#    echo "$LAUNCH_COUNT total" >& 6
    if [ -n "$ERROR_LEVELS" ]; then
        for el in $ERROR_LEVELS; do
            unset countByComment
            unset passedByComment
            declare -A countByComment
            declare -A passedByComment
            while IFS= read -r lin; do
                eval $lin
                if [ -z "$ERROR_LEVEL" ]; then
                    ERROR_LEVEL=0
                fi
                if [ "$ERROR_LEVEL" = "$el" ]; then
                    if [ -z "${countByComment["$RESULT_COMMENT"]}" ]; then
                        countByComment["$RESULT_COMMENT"]=0
                    fi
                    countByComment["$RESULT_COMMENT"]=$(( countByComment["$RESULT_COMMENT"] + 1 ))
                    passedByComment["$RESULT_COMMENT"]=$TEST_PASSED
                fi
            done <"$f"
            for cmt in "${!countByComment[@]}"; do
                if [ ${passedByComment["$cmt"]} -ne 0 ]; then
                    DIV_CLASS=passed
                    DIV_COLOR=green
                else
                    DIV_CLASS=failed
                    DIV_COLOR=red
                fi
                if [ $TO_BRIEF -ne 0 ]; then
                    echo "<div style='color: $DIV_COLOR'>" >& 5
                    echo "${countByComment[$cmt]} $cmt" >& 5
                    echo "</div>" >& 5
                fi
                echo "<div class=$DIV_CLASS>" >& 6
                if [ $HAS_FAILS -ne 0 -o $LAUNCH_COUNT -le $MAX_LONELY_OK -o $FORCE_TABLE -ne 0 ]; then
                    echo "<a href='#' class='details $DIV_CLASS' onclick='{ toggleElem(\"det$nextDetailsId\"); return false; }'>" >& 6
                    echo "<span class=details>${countByComment[$cmt]} $cmt</span>" >& 6
                    echo "</a>" >& 6
                    echo "<table border=1 cellspacing=0 class=details$FORCE_TABLE id='det$nextDetailsId'>" >& 6
                    echo "<tr>" >& 6
                    echo "<th>Platform</th>" >& 6
                    echo "<th>noH</th>" >& 6
                    echo "<th>autoTfm</th>" >& 6
                    echo "<th>Grid</th>" >& 6
                    echo "<th>CPUs</th>" >& 6
                    echo "<th>GPUs</th>" >& 6
                    echo "<th>Time</th>" >& 6
                    echo "</tr>" >& 6
                    while IFS= read -r lin; do
                        eval $lin
                        if [ -z "$ERROR_LEVEL" ]; then
                            ERROR_LEVEL=0
                        fi
                        if [ "$ERROR_LEVEL" = "$el" -a "$RESULT_COMMENT" = "$cmt" ]; then
                            echo "<tr>" >& 6
                            echo "<td>$PLATFORM</td>" >& 6
                            if [ $NOH_FLAG -ne 0 ]; then
                                echo "<td>+</td>" >& 6
                            else
                                echo "<td>-</td>" >& 6
                            fi
                            if [ $AUTOTFM_FLAG -ne 0 ]; then
                                echo "<td>+</td>" >& 6
                            else
                                echo "<td>-</td>" >& 6
                            fi
                            if [ -n "$PROC_GRID" ]; then
                                echo "<td>$PROC_GRID</td>" >& 6
                            else
                                echo "<td>N/A</td>" >& 6
                            fi
                            if [ -n "$CPUS_PER_PROC" ]; then
                                echo "<td>$CPUS_PER_PROC</td>" >& 6
                            else
                                echo "<td>N/A</td>" >& 6
                            fi
                            if [ -n "$CUDAS_PER_PROC" ]; then
                                echo "<td>$CUDAS_PER_PROC</td>" >& 6
                            else
                                echo "<td>N/A</td>" >& 6
                            fi
                            if [ -n "$CALC_TIME" ]; then
                                echo "<td>$CALC_TIME</td>" >& 6
                            else
                                echo "<td>N/A</td>" >& 6
                            fi
                            echo "</tr>" >& 6
                        fi
                    done <"$f"
                    echo "</table>" >& 6
                    nextDetailsId=$(( nextDetailsId + 1 ))
                else
                    echo "${countByComment[$cmt]} $cmt" >& 6
                fi
                echo "</div>" >& 6
            done
        done
    else
        if [ $TO_BRIEF -ne 0 ]; then
            echo "&nbsp;" >& 5
        fi
        echo "&nbsp;" >& 6
    fi
    if [ $TO_BRIEF -ne 0 ]; then
        echo "</td>" >& 5
        echo "</tr>" >& 5
    fi
    echo "</td>" >& 6
    echo "</tr>" >& 6
done <$RES_FILES

echo "</table>" >& 5
echo "</table>" >& 6
echo "</body>" >& 5
echo "</body>" >& 6
echo "</html>" >& 5
echo "</html>" >& 6

exec 5>&-
exec 6>&-

cd "$REPORT_DIR"
tar -czf "sources.tgz" "sources"
cd "$SAVE_DIR"

rm $RES_FILES
