#!/bin/bash

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

plugindirs="$@"
if [ -z "$plugindirs" ]; then
    plugindirs=$(cat .hgsub | 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; }
.bad { color: red; }
a, a:link, a:visited, a:hover, a:active { text-decoration: none; }
</style>
</head>
<body>
<table>
<tr><th>Plugin&nbsp;Library</th>
EOF

for p in $platforms ; do
    echo "<th>$p</th>"
done

echo "</tr>";

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

for dir in $plugindirs ; do
    dir=${dir%/*}
    echo "<tr><td>$dir</td>"
    for p in $platforms ; do
	reportdir="REPORTS/$p"
	summary="$reportdir/$dir.summary.txt"
	echo "<td>"
	if [ -f "$summary" ]; then
	    outcome=$(cat "$summary" | awk -F: '{ print $2; }' | sed 's/^ *//')
	    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>"
	    echo "<a href='$reportdir/$dir.test.txt'>"
	    case "$outcome" in
		BUILD_FAILED) echo "$no";;
		*TEST_FAILED*) echo "$no";;
		*_FAILED*) echo "$yes";;
		OK) echo "$yes";;
		*) echo "$unknown";;
	    esac
	    echo "</a>"
	    echo "<a href='$reportdir/$dir.envtest.txt'>"
	    case "$outcome" in
		*FAILED*) echo "$no";;
		OK) echo "$yes";;
		*) echo "$unknown";;
	    esac
	    echo "</a>"
	else
	    echo "$unknown"
	fi
	echo "</td>"
    done
    echo "</tr>"
done

echo "</table></body>"
