#!/bin/sh

cd /project/projectdirs/visit/www

# update top-level index.html file
rm -f index.html
echo "<html><head><title>VisIt Regression Results</title></head><body bgcolor=\"#a0a0f0\">" > index.html
echo "<H1>Results of VisIt Regression Tests</H1>" >> index.html
echo "<table border>" >> index.html
echo "<tr>" >> index.html
echo "   <td><b>Timestamp</b></td>" >> index.html
echo "   <td><b>Mode</b></td>" >> index.html
echo "   <td><b>Result</b></td>" >> index.html
echo "</tr>" >> index.html

ls -1t /project/projectdirs/visit/www/tests/*/*/index.html | while read testLine; do

    theMode=`echo $testLine | cut -d'/' -f8`
    theRelDir=`echo $testLine | cut -d'/' -f6-`
    theRelDir="./${theRelDir}"
    theTimeStamp=`ls -l --time-style=+"%A_%b_%d_%I:%M%p" $testLine | tr -s ' ' | cut -d' ' -f6`

    echo "<tr>" >> index.html
    echo "<td>${theTimeStamp}</td>" >> index.html
    echo "<td><a href=\"${theRelDir}\">${theMode}</a></td>" >> index.html

    hasFailed=`grep -m 1 Failed $testLine 2>/dev/null`
    if test "$hasFailed" = ""; then
        hasFailed=`grep -m 1 Unacceptable $testLine 2>/dev/null`
    fi
    if test "$hasFailed" = ""; then
        hasFailed=`grep -m 1 OS-Killed $testLine 2>/dev/null`
    fi
    hasSkips=`grep -m 1 Skipped $testLine 2>/dev/null`
    if test "$hasFailed" = ""; then
        if test "$hasSkips" = ""; then
            echo "<td bgcolor="#00ff00"><b>Passed</b></td>" >> index.html
        else
            echo "<td bgcolor="#00ff00"><b>Passed w/Skips</b></td>" >>index.html
        fi
    else
        echo "<td bgcolor="#ff0000"><b>Failed</b></td>" >> index.html
    fi
    echo "</tr>" >> index.html
done

echo "</table>" >> index.html
quota=`prjquota visit | grep visit | tr -s ' '`
quota_gb_used=`echo $quota | cut -d ' ' -f2`
quota_gb_quota=`echo $quota | cut -d ' ' -f3`
quota_in_used=`echo $quota | cut -d ' ' -f5`
quota_in_quota=`echo $quota | cut -d ' ' -f6`
echo "<h2> Quota Info </h2>" >> index.html
echo "<table border=1>" >> index.html 
echo "<tr>" >> index.html
echo "<td>Space used (GB)</td><td>Space Quota (GB)</td>" >> index.html
echo "<td>Inodes used</td><td>Inode Quota</td>"  >> index.html
echo "</tr>"  >> index.html
echo "<tr>"  >> index.html
echo "<td>$quota_gb_used</td><td>$quota_gb_quota</td>"  >> index.html
echo "<td>$quota_in_used</td><td>$quota_in_quota</td>"  >> index.html
echo "</tr>"  >> index.html
echo "</table>"  >> index.html

chgrp -R visit index.html
chmod -R g+rwX,o+rX index.html
