#!/bin/sh
# echo 'processlogs: at '`date`', begin'

# establish which directory the logfiles live in (leave out for
# backwards compatibility)
buildlogdir=$(realpath .)
if [ "$2" != "" ]; then buildlogdir="$2"; fi

# establish which directory INDEX lives in (leave out for
# backwards compatibility)
indexlogdir=$(realpath .)
if [ "$3" != "" ]; then indexlogdir="$3"; fi

# allow this script to be run from anywhere in the tree
scriptdir=$(dirname $0)
errorscript=$scriptdir/processonelog

# create a name for the tempfile
of=.index.html

# delete lockfiles if more than a day old
find $of .lock -mmin +60 -delete 2>/dev/null

# exit if another instance of this program is running
if [ -f $of -o -f .lock ]; then exit; fi

# if there are no new logfiles, there is nothing to do here.
if [ -e .stamp -a $(echo $(find -f $buildlogdir -maxdepth 1 -newer .stamp -type f -name '*.log' 2>/dev/null | wc -l)) = "0" ]; then exit; fi

touch .stamp
touch .lock

# get the list of buildlogs.
set $buildlogdir/*.log
> .logs

#
# Read the log-files and write summaries to .logs in the format
#    $filename|$portname|$affected|$logsize|$dir|$maintainer|\
#    $reason|$tag|$broken|$datetime
#

# echo 'processlogs: at '`date`', begin processing log files'

if [ $# != 1 -o "x$1" != "x`dirname $1`/*.log" ]; then
  while [ ! -z "$1" ]; do
    filename=$(basename $1)
    #echo 'processlogs: at '`date`', examining '$filename'

    $errorscript $filename $indexlogdir >> .logs
    shift
  done
fi
# echo 'processlogs: at '`date`', end processing log files'

# XXX Sometimes log entries get doubled up for some reason
uniq .logs > .logs2
mv .logs2 .logs

num=$(wc -l < .logs)

header() {
  echo "<html><head><title>Package building errors</title>" >$of
  echo "</head><body><h1>Package building errors</h1>" >>$of
  echo "<p>View by " >>$of
  echo "[ <a href=\"index.html\">port</a> " >>$of
  echo "| <a href=\"index-maintainer.html\">maintainer</a> " >>$of
  echo "| <a href=\"index-category.html\">category</a> " >>$of
  echo "| <a href=\"index-reason.html\">error</a> " >>$of
  echo "| <a href=\"index-builddate.html\">builddate</a> " >>$of
  echo "]</p>" >>$of

  if [ $num = "0" ]; then
    echo "No errors (yet)" >>$of
  else
    if [ -s cvsdone ]; then
      echo "CVS update finished at: $(cat cvsdone)<br>" >> $of
    fi
    latest=$(cd $buildlogdir; ls -rtTl *.log | tail -1 | awk '{printf("%s %s %s %s\n",$6,$7,$8,$9)}';)
    echo "Timestamp of newest log: $latest<br><br>" >> $of
    echo "\"Aff.\" is number of ports that depend on this one<br>" >> $of
    echo "\"<font color=\"red\">[B]</font>\" indicates port is marked BROKEN<br><br>" >> $of
    echo "<p>$num errors</p>" >> $of
    echo "<table border=1>" >>$of
    echo "<tr>$1</tr>" >>$of
  fi
}

footer() {
  echo "</table>" >>$of
  echo "</body>" >>$of
  echo "</html>" >>$of
}

# Now reread the .logs file and create the reports.  If the .logs file
# has blanks in any field, this code won't work.  Therefore, the
# code above has to guarantee that that won't happen, and the code
# below has to catch the metatoken.

# echo 'processlogs: at '`date`', create default output'
#
# Create "default" output, sorted on portname
#
header "<th>Port</th><th>Aff.</th><th>Size</th><th>CVS</th><th>Maintainer</th><th>Reason</th><th>Build date</th>"

for i in `cat .logs | sort`; do
    set $(echo "$i" | tr \| " " | sed -e "s@NONE@\\&nbsp;@g")
    mailto="$6"
    if [ "$6" != "&nbsp;" ] ; then
       mailto="<a href=\"mailto:$6\">$6</a>"
    fi

    echo "<tr>" >> $of

    echo "<td><a href=\"$1\">$2</a></td>" >> $of

    affby=$3
    test $affby = "0" -o $affby = "-1" && affby="&nbsp;"
    echo "<td align=\"right\">$affby</td><td align=\"right\">$4 Kb</td>" >> $of
    echo "<td><a href=\"http://www.FreeBSD.org/cgi/cvsweb.cgi/ports/$5\">$5</a></td>" >> $of
    echo "<td>$mailto</td>" >> $of
    echo "<td>" >> $of

    test "$9" = "broken" && echo "<font color=\"red\">[B]</font>" >> $of
    reason=$(echo $7 | tr '_' ' ')
    echo "<a href=\"http://bento.freebsd.org/#$8\">$reason</a>" >> $of
    echo "</td>" >> $of

    date=$(echo ${10} | tr '_' ' ')
    echo "<td>$date</td>" >> $of

    echo "</tr>" >> $of
done
footer ""
mv -f $of index.html

# echo 'processlogs: at '`date`', create output sorted by category'
#
# Create output by category
#
header "<th>CVS</th><th>Aff.</th><th>Size</th><th>Port</th><th>Maintainer</th><th>Reason</th><th>Build date</th>"

for i in `cat .logs | sort -t \\| +4`; do
    set $(echo "$i" | tr \| " " | sed -e "s@NONE@\\&nbsp;@g")
    mailto="$6"
    if [ "$6" != "&nbsp;" ] ; then
       mailto="<a href=\"mailto:$6\">$6</a>"
    fi

    echo "<tr>" >> $of

    echo "<td><a href=\"http://www.FreeBSD.org/cgi/cvsweb.cgi/ports/$5\">$5</a></td>" >> $of

    affby=$3
    test $affby = "0" -o $affby = "-1" && affby="&nbsp;"
    echo "<td align=\"right\">$affby</td><td align=\"right\">$4 Kb</td>" >> $of
    echo "<td><a href=\"$1\">$2</a></td>" >> $of
    echo "<td>$mailto</td>" >> $of

    echo "<td>" >> $of
    test "$9" = "broken" && echo "<font color=\"red\">[B]</font>" >> $of
    reason=$(echo $7 | tr '_' ' ')
    echo "<a href=\"http://bento.freebsd.org/#$8\">$reason</a>" >> $of
    echo "</td>" >> $of

    date=$(echo ${10} | tr '_' ' ')
    echo "<td>$date</td>" >> $of

    echo "</tr>" >> $of
done
footer ""
mv -f $of index-category.html

# echo 'processlogs: at '`date`', create output sorted by maintainer'
#
# Create output by maintainer
#
header "<th>Maintainer</th><th>Port</th><th>Aff.</th><th>Size</th><th>CVS</th><th>Reason</th><th>Build date</th>"

for i in `cat .logs | sort -t \\| +5`; do
    set $(echo "$i" | tr \| " " | sed -e "s@NONE@\\&nbsp;@g")
    mailto="$6"
    if [ "$6" != "&nbsp;" ] ; then
       mailto="<a href=\"mailto:$6\">$6</a>"
    fi

    echo "<tr>" >> $of

    echo "<td>$mailto</td>" >> $of
    echo "<td><a href=\"$1\">$2</a></td>" >> $of

    affby=$3
    test $affby = "0" -o $affby = "-1" && affby="&nbsp;"
    echo "<td align=\"right\">$affby</td><td align=\"right\">$4 Kb</td>" >> $of
    echo "<td><a href=\"http://www.FreeBSD.org/cgi/cvsweb.cgi/ports/$5\">$5</a></td>" >> $of

    echo "<td>" >> $of
    test "$9" = "broken" && echo "<font color=\"red\">[B]</font>" >> $of
    reason=$(echo $7 | tr '_' ' ')
    echo "<a href=\"http://bento.freebsd.org/#$8\">$reason</a>" >> $of
    echo "</td>" >> $of

    date=$(echo ${10} | tr '_' ' ')
    echo "<td>$date</td>" >> $of

    echo "</tr>" >> $of
done
footer ""
mv -f $of index-maintainer.html

# echo 'processlogs: at '`date`', create output sorted by error'
#
# Create output by error
#
header "<th>Reason</th><th>Port</th><th>Aff.</th><th>Size</th><th>CVS</th><th>Maintainer</th><th>Build date</th>"

for i in `cat .logs | sort -t \\| +7`; do
    set $(echo "$i" | tr \| " " | sed -e "s@NONE@\\&nbsp;@g")
    mailto="$6"
    if [ "$6" != "&nbsp;" ] ; then
       mailto="<a href=\"mailto:$6\">$6</a>"
    fi

    echo "<tr>" >> $of

    echo "<td>" >> $of
    test "$9" = "broken" && echo "<font color=\"red\">[B]</font>" >> $of
    reason=$(echo $7 | tr '_' ' ')
    echo "<a href=\"http://bento.freebsd.org/#$8\">$reason</a>" >> $of
    echo "</td>" >> $of

    echo "<td><a href=\"$1\">$2</a></td>" >> $of

    affby=$3
    test $affby = "0" -o $affby = "-1" && affby="&nbsp;"
    echo "<td align=\"right\">$affby</td><td align=\"right\">$4 Kb</td>" >> $of
    echo "<td><a href=\"http://www.FreeBSD.org/cgi/cvsweb.cgi/ports/$5\">$5</a></td>" >> $of
    echo "<td>$mailto</td>" >> $of

    date=$(echo ${10} | tr '_' ' ')
    echo "<td>$date</td>" >> $of

    echo "</tr>" >> $of
done
footer ""
mv -f $of index-reason.html

# echo 'processlogs: at '`date`', create output sorted by builddate'
#
# Create output by builddate
#
header "<th>Build date</th><th>Port</th><th>Aff.</th><th>Size</th><th>CVS</th><th>Maintainer</th><th>Reason</th>"

for i in `cat .logs | sort -t \\| +9`; do
    set $(echo "$i" | tr \| " " | sed -e "s@NONE@\\&nbsp;@g")
    mailto="$6"
    if [ "$6" != "&nbsp;" ] ; then
       mailto="<a href=\"mailto:$6\">$6</a>"
    fi

    echo "<tr>" >> $of

    date=$(echo ${10} | tr '_' ' ')
    echo "<td>$date</td>" >> $of

    echo "<td><a href=\"$1\">$2</a></td>" >> $of

    affby=$3
    test $affby = "0" -o $affby = "-1" && affby="&nbsp;"
    echo "<td align=\"right\">$affby</td><td align=\"right\">$4 Kb</td>" >> $of
    echo "<td><a href=\"http://www.FreeBSD.org/cgi/cvsweb.cgi/ports/$5\">$5</a></td>" >> $of
    echo "<td>$mailto</td>" >> $of

    echo "<td>" >> $of
    test "$9" = "broken" && echo "<font color=\"red\">[B]</font>" >> $of
    reason=$(echo $7 | tr '_' ' ')
    echo "<a href=\"http://bento.freebsd.org/#$8\">$reason</a>" >> $of
    echo "</td>" >> $of

    echo "</tr>" >> $of
done
footer ""
mv -f $of index-builddate.html

# echo 'processlogs: at '`date`', create maintainer list'
#
# Get list of maintainers.
#
for i in `cat .logs | sort -t \\| +9`; do
    set $(echo "$i" | tr \| " " | sed -e "s@NONE@\\&nbsp;@g")
    mailto="$6"
    if [ "$6" != "&nbsp;" ] ; then
       mailto="<a href=\"mailto:$6\">$6</a>"
    fi

    maints="$maints $6"
done

echo $maints | sed -e 's/ /\
/g' | sort -fu > maintainers

# now remove the lockfile for this invocation, and exit
rm .lock
# echo 'processlogs: at '`date`', end'
