#!/bin/bash

NEWSSERVER=
NEWSGROUP=
WARN_EXP="warning:"
EXCLUDED_WARNINGS="' has modification time in the future (
Clock skew detected.  Your build may be incomplete.
Symbol version dump"

CHANGEURL="http://ukbugdb/go/change:"
JOBURL="http://ukbugdb/?BugID="

NEWSPOST=false
CLEANBUILDS=false
SCRIPTTEST=false
FAKENEWSPOST=false
VERBOSE=false

while test $# -gt 0
do
    case "$1" in
    -n)
	NEWSPOST=true
	;;
    -c)
	CLEANBUILDS=true
	;;
    -t)
        SCRIPTTEST=true
        ;;
    -N)
	FAKENEWSPOST=true
	;;
    -v)
	VERBOSE=true
	;;
    -*)
	echo "Unknown option $1 Aborting."
	exit 1
	;;
    *)
	break
	;;
    esac
    shift
done

if $SCRIPTTEST
then
    cat <<EOF
make_daily_make_index test
Directory:            `pwd`
Newsgroup posting:    $NEWSPOST
News server:          $NEWSSERVER
Newsgroup:            $NEWSGROUP
Build was from clean: $CLEANBUILDS
Other options:        $@
EOF
    exit 0
fi

DATE=`basename \`pwd\``

pwd
BRANCHES=`ls {build,run,lint}_log*.log | sed 's/^.*:\(.*\):.*$/\1/' | sort -u`
RELEASES=`ls {build,run,lint}_log*.log | sed 's/^.*:.*:\(.*\)\.log$/\1/' | sort -u`

PREVIOUSCACHE=`ls .. 2>/dev/null | sed -n -e "/^$DATE$/q" -e 'p' | tail -1`
if test ! -z "$PREVIOUSCACHE" ; then
    HAVEPREVIOUSCACHE=true
    PREVIOUSCACHE="../$PREVIOUSCACHE"
else
    HAVEPREVIOUSCACHE=false
fi

find_previous_file()
{
    BASE=$1
    FILE=$2

    if $HAVEPREVIOUSCACHE && test -r "$PREVIOUSCACHE/$FILE"
    then
	PREV="$PREVIOUSCACHE/$FILE"
    else
	PREV=`ls ../*/$FILE 2>/dev/null | sed -n -e "/\/$DATE\//q" -e 'p' | \
	    tail -1`
    fi

    test ! -z "$PREV" && echo $PREV | sed 's,^.*/\(.*\)/.*$,\1,'
}

find_previous()
{
    find_previous_file $1 index.html
}

find_previous_match()
{
    BASE=$1
    FILE=$2
    MATCH=$3

    PREV=`ls ../*/$FILE 2>/dev/null | sed -n -e "/\/$DATE\//q" -e 'p' | \
	xargs grep -l "^$MATCH\$" | tail -1`

    test ! -z "$PREV" && echo $PREV | sed 's,^.*/\(.*\)/.*$,\1,'
}

singular_plural()
{
    COUNT=$1
    SINGULAR=$2
    PLURAL=$3

    if test $1 -eq 1
    then
	echo "$SINGULAR"
    else
	echo "$PLURAL"
    fi
}

singular_plural_builds()
{
    singular_plural "`echo $1 | wc -w`" "$2" "$3"
}

singular_plural_count()
{
    echo "$1 `singular_plural \"$1\" \"$2\" \"$3\"`"
}

unique_warnings()
{
    grep -i $WARN_EXP | fgrep -v "$EXCLUDED_WARNINGS" | \
      sed 's,\(\.\./builds/\)[^/]*/,\1.../,' | cat -n | sort -k 2 | uniq -f 1 | \
      sort -n | cut -f2-
}

html_escape()
{
    sed -e 's/\&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' "$@"
}

z_exists()
{
    test -r "$1" -o -r "$1.Z" -o -r "$1.gz" -o -r "$1.bz2"
}

z_cat()
{
    if test -r "$1" ; then
        cat "$1"
    elif test -r "$1.Z" ; then
        uncompress -c "$1"
    elif test -r "$1.gz" ; then
        gunzip -c "$1.gz"
    elif test -r "$1.bz2" ; then
        bunzip2 -c "$1.bz2"
    else
        echo "Can't find file $1 or compression thereof." 1>&2
        exit 1
    fi
}

NEW_FAILURES=
OLD_FAILURES=

exec >index.html

CSS="\
<style type="text/css">
.pass    { background-color: #88ff88 }
.warn    { background-color: #ffff88 }
.fail    { background-color: #ff8888 }
.unknown { background-color: #8888ff }
</style>
"

cat << EOF
<html>
<head>
<title>Build results for $DATE</title>
$CSS
</head>
<body>
<h1>Build results for $DATE</h1>
<table BORDER=1>
<tr ALIGN=CENTER><td>
EOF

for BRANCH in $BRANCHES
do
    cat > $BRANCH.html <<EOF
<html>
<head>
<title>Detailed results for branch $BRANCH on $DATE</title>
$CSS
</head>
<body>
<h1>Detailed results for branch $BRANCH on $DATE</h1>
EOF
    echo "<th><A HREF="$BRANCH.html">$BRANCH</A>"
done

for RELEASE in $RELEASES
do
    echo "<tr ALIGN=CENTER><th ALIGN=LEFT>$RELEASE"
    for BRANCH in $BRANCHES
    do
        $VERBOSE && echo "Processing $RELEASE on $BRANCH" 1>&2
        if echo $RELEASE | grep '_run$' 1>/dev/null 2>&1 ; then
 	    PREFIX=run_log:$BRANCH:$RELEASE
            TYPE="Test run"
	elif echo $RELEASE | grep '_lint$' 1>/dev/null 2>&1 ; then
 	    PREFIX=lint_log:$BRANCH:$RELEASE
            TYPE="Lint"
	else
 	    PREFIX=build_log:$BRANCH:$RELEASE
            TYPE="Build"
	fi
	OUTPUT=$PREFIX.log
	PRETTY=$PREFIX.html
	SUMMARY=$PREFIX.result
	ERRORS=$PREFIX.errors
	if echo $RELEASE | grep singleproc 1>/dev/null 2>&1; then
	    USAGE=usage:$BRANCH:$RELEASE.du
	else
	    USAGE=usage:$BRANCH:p0_${RELEASE}.du
	fi
	if test -r $OUTPUT ; then
	    (
		cat<<EOF
<html>
<head>
<title>$TYPE log for $RELEASE on $BRANCH at $DATE</title>
<style type=text/css>
.error    { color: #cc0000 }
.warning  { color: #0000cc }
</style>
</head>
<body>
<h1>$TYPE log for $RELEASE on $BRANCH at $DATE</h1>
<pre>
EOF
	    html_escape $OUTPUT |
		awk '{pre = ""; post = ""} \
		    /Description/ {pre = lpre; post = lpost} \
		    /: *[Ee]rror/ || /[Ee]rror *:/ || /\*\*\*.*too/ || \
		    /forbidden/ || /failed/ || /^make.*\*\*\*.*Stop\./ { \
			pre = "<span class=\"error\">"; post = "</span>"; \
		    } \
		    pre == "" && /[Ww]arning:/ { \
			pre = "<span class=\"warning\">"; post = "</span>"; \
		    } \
		    {print pre $0 post; lpre = pre; lpost = post;}'
	    cat<<EOF
</pre>
</body>
</html>
EOF
	    ) > $PRETTY
	    if test -r $SUMMARY ; then
		RESULT=`cat $SUMMARY`
	    else
		RESULT="Script failure (no result)"
	    fi
	    FAIL=true
	    PASS=false
	    WARN=false
	    case "$RESULT" in
	    "Pass")
		RESULT="PASS"
		CLASS="pass"
		FAIL=false
		PASS=true
		;;
	    "Fail")
		RESULT="FAIL"
		CLASS="fail"
		;;
	    *)
		RESULT="Unknown result $RESULT"
		CLASS="unknown"
		;;
	    esac
	    if $CLEANBUILDS
	    then
		if $PASS
		then
		    WARNINGS=`unique_warnings < $OUTPUT | sort -u | wc -l`
		    RESULT="$RESULT (`singular_plural_count \
					$WARNINGS warning warnings`)"
		    test $WARNINGS -gt 0 && CLASS="warn"
		    WARN=true
		fi
	    fi
	    echo "<td class=\"$CLASS\"><A HREF=\"$PRETTY\">$RESULT</A><br>"
	    cat >> $BRANCH.html <<EOF
<div class="$CLASS">
<h2><A HREF="$PRETTY">$RELEASE</A></h2>
<P>$RESULT</P>
EOF
	    if $FAIL
	    then
		if PREV=`find_previous_match $DATE $SUMMARY Pass`
		then
		    echo "<A HREF=\"../$PREV/\">Previous pass ($PREV)</A><br>"
		else
		    echo "No previous passing build<br>"
		fi
		awk '/Description/ && e != 0 {out = out "\n" $0} \
		    {e = 0} \
		    /: *[Ee]rror/ || /[Ee]rror *:/ || /\*\*\*.*too/ || \
		    /forbidden/ || /failed/ || /^make.*\*\*\*.*Stop\./ \
			{if (p != "") out = out "\n" p; \
			out = out "\n" $0; e = 1} \
		    !/^ *$/ && !/^[a-zA-Z0-9_.]*\([0-9]*\): *$/ {p = ""}  \
		    /\.PACK/ {p = $0} /Trying full build/ {out = ""} \
		    END {if (out == "") \
			    print "Unable to identify any error messages"; \
			    else print substr(out, 2);}' $OUTPUT > $ERRORS
		echo "<PRE>" >> $BRANCH.html
		html_escape $ERRORS >> $BRANCH.html
		echo "</PRE>" >> $BRANCH.html
	    else
		if $WARN
		then
		    echo "<PRE>" >> $BRANCH.html
		    unique_warnings < $OUTPUT >> $BRANCH.html
		    echo "</PRE>" >> $BRANCH.html
		fi
		if z_exists $USAGE
		then
		    echo "<PRE>" >> $BRANCH.html
		    z_cat $USAGE | grep '^[ 0-9]*\./[^/]*$' >> $BRANCH.html
		    echo "</PRE>" >> $BRANCH.html
		fi
	    fi
	    if PREV=`find_previous_file $DATE $SUMMARY`
	    then
		PREV_RESULT=`cat ../$PREV/$SUMMARY`
		echo "<A HREF=\"../$PREV/\">Previous build</A>"
	    else
		PREV_RESULT="None"
		echo "No previous build"
	    fi
	    if $FAIL
	    then
		if test "$PREV_RESULT" != "Fail"
		then
		    NEW_FAILURES="$NEW_FAILURES $BRANCH/$RELEASE"
		else
		    OLD_FAILURES="$OLD_FAILURES $BRANCH/$RELEASE"
		fi
	    fi
	    echo "</div>" >> $BRANCH.html
	else
	    echo "<td>"
	fi
    done
done

NEED_HEADER=true
HEADER="<tr ALIGN=CENTER><th ALIGN=LEFT>Changes"
for BRANCH in $BRANCHES
do
    FILE=changes_$BRANCH.log
    if test -r $FILE
    then
	if $NEED_HEADER
	then
	    echo $HEADER
	    NEED_HEADER=false
	fi
	HTML_FILE=changes_$BRANCH.html
	(
	    cat <<EOF
<html>
<head>
<title>Changes on $BRANCH for $DATE</title>
</head>
<body>
<h1>Changes on $BRANCH for $DATE</h1>
<pre>
EOF
            sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g;' \
		-e '2,$s/^Change/<hr>&/' \
  -e "s,\([Cc]hange *\([0-9][0-9]*\)\),<a href=\"$CHANGEURL\2\">\1</a>,g" \
  -e "s,^\([^ ][^ ]*\)\(  *on  *[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9]*  *by\),<a href=\"$JOBURL\1\">\1</a>\2," $FILE
	    cat <<EOF
</pre>
</body>
EOF
	) > $HTML_FILE
	echo "<td><A HREF=\"$HTML_FILE\">Summary</A>"
    else
	if $NEED_HEADER
	then
	    HEADER="$HEADER<td>"
	else
	    echo "<td>"
	fi
    fi
done

if $CLEANBUILDS
then
    echo "<tr ALIGN=CENTER><th ALIGN=LEFT>Warnings"
    for BRANCH in $BRANCHES
    do
	FILE=warnings_$BRANCH.log
	cat {run,build,lint}_log:$BRANCH:*.log | unique_warnings > $FILE
	echo "<td><A HREF=\"$FILE\">`cat $FILE | wc -l`</A>"
    done
fi

echo "</table>"

for BRANCH in $BRANCHES
do
    cat >> $BRANCH.html <<EOF
</body>
</html>
EOF
done

if $CLEANBUILDS
then
    FILE=warnings.log
    cat {run,build,lint}_log:*.log | unique_warnings > $FILE
cat<<EOF
<p>Total number of unique warnings across all branches:
<A HREF="$FILE">`cat $FILE | wc -l`</A>
EOF
else
    echo "<p>Firmware not built from clean so can't give warning count."
fi

echo '<p><A HREF="main_log.log">Script log file</A>'
if PREVIOUS=`find_previous $DATE`
then
    echo "<p><A HREF=\"../$PREVIOUS/\">Previous results</A>"
fi

cat << EOF
<p><A HREF="../..">Index of results</A>
<p><A HREF="http://wiki/Hydra_drivers/Daily_builds">Wiki page</A>
</body>
</html>
EOF

exec >&2

if $NEWSPOST && test ! -z "$NEW_FAILURES"
then
    (
    sleep 2
    echo "POST"
    sleep 2
    cat <<EOF
From: Daily make <softies@csr.com>
Newsgroups: $NEWSGROUP
Subject: Daily make report for $DATE: Newly failing builds
Date: `(date -R || date -u +"%a, %d %b %Y %T +0000") 2>/dev/null`
Message-ID: <`date +"%Y%m%d%H%M%S"`$$@`hostname`.`domainname`>

Daily make report for $DATE.

The following \
`singular_plural_builds "$NEW_FAILURES" "build has" "builds have"` \
just failed:

EOF
    for FAILURE in $NEW_FAILURES
    do
	echo "  $FAILURE"
    done
    if test ! -z "$OLD_FAILURES"
    then
	cat<<EOF

The following \
`singular_plural_builds "$OLD_FAILURES" "build was" "builds were"` \
already failing:

EOF
	for FAILURE in $OLD_FAILURES
	do
	    echo "  $FAILURE"
	done
    fi
    if pwd | grep 'public_html' > /dev/null
    then
	cat<<EOF

The results can be found at:

  `pwd | sed 's,^/home/\([^/]*\)/public_html/,http://noo-noo/~\1/,'`/
EOF
    fi

    echo
    echo "Error summary for newly failing builds:"
    for FAILURE in $NEW_FAILURES
    do
	echo
	echo "$FAILURE"
	echo
	sed -e 's/^/<%>/' build_log:`echo $FAILURE | sed 's,/,:,'`.errors | \
	    fmt -s -w 70 | sed -e 's/^/    /' -e 's/^    <%>/  /'
    done
    echo "."
    sleep 2
    echo "QUIT"
    sleep 2
    ) | ( if $FAKENEWSPOST ; then
	cat
    else
	telnet $NEWSSERVER 119 > news-post.log 2>&1
    fi )
fi
