#!/bin/bash

mydir=$(dirname "$0")
case "$mydir" in /*);; *) mydir=$(pwd)/"$mydir";; esac

. "$mydir"/include.sh

plugindirs="$@"
if [ -z "$plugindirs" ]; then
    plugindirs=$(cat METADATA/repos.txt | grep -v vamp-plugin-sdk | grep -v vamp-plugin-tester | awk '{ print $1; }')
else 
    for dir in $plugindirs ; do
	if [ ! -d "$dir" ]; then
	    echo "ERROR: Directory $dir not found"
	    usage
	fi
    done
fi

platforms=$(echo REPORTS/[a-z]* | sed 's/REPORTS\///g')

cat <<EOF
<head>
<style type="text/css">
.good { color: blue; }
.dl { color: green; }
.bad { color: red; }
table { border: 1px solid black; }
tr { padding: 0px; background-color: #f0f0f0; }
tr.odd { background-color: #fafafa; }
td, th { text-align: center; min-width: 4em; margin: 0px; padding: 0.4em; }
td.pluginname { text-align: right; }
td.build, th.build { border-left: 1px solid black; }
th { border-bottom: 1px solid black; }
a, a:link, a:visited, a:hover, a:active { text-decoration: none; }
</style>
</head>
<body>
<table cellspacing=0>
<tr><th></th>
EOF

for p in $platforms ; do
    echo "<th colspan=4 class=build>$p</th>"
done

echo "</tr>"
echo "<tr><td></td>"

for p in $platforms ; do
    echo "<th class=build>Build</th>"
    echo "<th>Test</th>"
    echo "<th>Check</th>"
    echo "<th>Package</th>"
done

echo "</tr>"

yes="<span class=good>&#x2714;</span>"
no="<span class=bad>&#x2718;</span>"
unknown="<span class=unknown>?</span>"
missing=""
dl="<span class=dl>&#x21a7;</span>"

# The possible outcomes are:
#
# OK - built, passed tests, passed env checks
# BUILD_FAILED - build failed so unable to test
# TEST_FAILED - build succeeded, tests failed, env checks passed
# ENV_FAILED - build succeeded, tests passed, env checks failed
# TEST_FAILED ENV_FAILED - build succeeded, tests and env checks failed

emit_build() {
    reportdir="$1"
    dir="$2"
    outcome="$3"
    echo "<a href='$reportdir/$dir.build.txt'>"
    case "$outcome" in
	BUILD_FAILED) echo "$no";;
	*_FAILED*) echo "$yes";;
	OK) echo "$yes";;
	*) echo "$unknown";;
    esac
    echo "</a>"
}

emit_test() {
    reportdir="$1"
    dir="$2"
    outcome="$3"
    case "$outcome" in
	BUILD_FAILED) echo "$missing";;
	*)
	    echo "<a href='$reportdir/$dir.test.txt'>"
	    case "$outcome" in
		*TEST_FAILED*) echo "$no";;
		*_FAILED*) echo "$yes";;
		OK) echo "$yes";;
		*) echo "$unknown";;
	    esac
	    echo "</a>" ;;
    esac
}

emit_env() {
    reportdir="$1"
    dir="$2"
    outcome="$3"
    echo "<a href='$reportdir/$dir.envtest.txt'>"
    case "$outcome" in
	BUILD_FAILED) echo "$missing";;
	*)
	    echo "<a href='$reportdir/$dir.envtest.txt'>"
	    case "$outcome" in
		*ENV_FAILED*) echo "$no";;
		*FAILED*) echo "$yes";;
		OK) echo "$yes";;
		*) echo "$unknown";;
	    esac
	    echo "</a>" ;;
    esac
}

emit_package() {
    platform="$1"
    dir="$2"
    outcome="$3"
    case "$outcome" in
	OK)
	    id=$(vcs_id "$dir")
	    pubtag="$platform"
	    if [ "$pubtag" = "piper-js" ]; then
		pubtag=piper
	    fi
	    package=$(echo "PACKAGES/$platform/$dir-$pubtag-$id".*)
	    echo "package is $package" 1>&2
	    if [ -f "$package" ]; then
		echo "<a href='$package'>$dl</a>"
	    fi
	    ;;
	*) ;;
    esac
}

oddeven=odd

for dir in $plugindirs ; do
    dir=${dir%/*}
    echo "<tr class=$oddeven><td class=pluginname>$dir</td>"
    for p in $platforms ; do
	reportdir="REPORTS/$p"
	summary="$reportdir/$dir.summary.txt"
	if [ -f "$summary" ]; then
	    outcome=$(cat "$summary" | awk -F: '{ print $2; }' | sed 's/^ *//' | fmt -100)
	    echo "<td class=build>"
	    emit_build "$reportdir" "$dir" "$outcome"
	    echo "</td><td>"
	    emit_test "$reportdir" "$dir" "$outcome"
	    echo "</td><td>"
	    emit_env "$reportdir" "$dir" "$outcome"
	    echo "</td><td>"
	    emit_package "$p" "$dir" "$outcome"
	    echo "</td>"
	else
	    echo "<td class=build>$missing</td>"
	    echo "<td>$missing</td>"
	    echo "<td>$missing</td>"
	    echo "<td>$missing</td>"
	fi
    done
    echo "</tr>"
    if [ "$oddeven" = odd ]; then
	oddeven=even
    else
	oddeven=odd
    fi
done

echo "</table></body>"
