#!/bin/sh
# $FreeBSD: ports/Tools/portbuild/scripts/dopackagestats,v 1.13 2006/09/23 07:44:34 linimon Exp $
#
# create HTML showing numbers of packages vs errors.  Run this in a directory
# accessible to the web server.
#

# alpha is obsolete
SUPPORTED_ARCHS="amd64 i386 ia64 sparc64"
ROOT_DIRECTORY=/var/portbuild

OUTFILE=`basename $0 | sed -e "s/^do//"`".html"
TMPFILE=.${OUTFILE}

# stylesheet seems like overkill for something this simple
TABLEBGCOLOR="#F0F0F0"
THCOLOR="#E0E0FF"
TDCOLOR_DONE="lightgreen"
TDCOLOR_NOT_DONE="lightyellow"

# subroutines

write_header () {
  echo "<html>" > ${TMPFILE}
  echo "<head>" >> ${TMPFILE}
  echo "<title>FreeBSD package building statistics</title>" >> ${TMPFILE}
  echo "</head>" >> ${TMPFILE}

  echo "<body>" >> ${TMPFILE}
  echo "<h1>FreeBSD package building statistics</h1>" >> ${TMPFILE}
  echo "<p>as of `date`</p>" >> ${TMPFILE}
}

write_table_begin () {
  echo "<table border='1' cellpadding='4' cellspacing='1' bgcolor='$TABLEBGCOLOR'>" >> ${TMPFILE}
  echo "<tr>" >> ${TMPFILE}
  echo "<td align='left' width='80' bgcolor='$TABLEBGCOLOR'>&nbsp;</td>" >> ${TMPFILE}
  echo "<th width='60' bgcolor='$THCOLOR'>cvs date</th>" >> ${TMPFILE}
  echo "<th width='60' bgcolor='$THCOLOR'>latest log</th>" >> ${TMPFILE}
  echo "<th bgcolor='$THCOLOR'>INDEX</th>" >> ${TMPFILE}
  echo "<th bgcolor='$THCOLOR'>packages</th>" >> ${TMPFILE}
  echo "<th bgcolor='$THCOLOR'>errors</th>" >> ${TMPFILE}
  echo "<th bgcolor='$THCOLOR'>skipped</th>" >> ${TMPFILE}
  echo "<th bgcolor='$THCOLOR'>missing</th>" >> ${TMPFILE}
  echo "<th bgcolor='$THCOLOR'>done?</th>" >> ${TMPFILE}
  echo "</tr>" >> ${TMPFILE}
}

write_row () {
    # first, gather data

    arch=$1
    build=$2
    directory=${ROOT_DIRECTORY}/${arch}/${build}
    branch=`echo $build | sed -e "s/-exp//"`
    if [ "$branch" = "4" ]; then
      indexfile=$directory/ports/INDEX
    else
      indexfile=$directory/ports/INDEX-$branch
    fi

    # column: date of CVS checkout
    cvsdone="&nbsp;"
    if [ -f $directory/cvsdone ]; then
      cvsdone="$(cat $directory/cvsdone | awk '{printf("%s %s\n",$2,$3)}')"
      if [ -z "$cvsdone" ]; then
        cvsdone="&nbsp;"
      fi
    fi

    # column: datestamp and URL of latest log
    latest="&nbsp;"
    if [ -d $directory/logs ]; then
      latest_suffix="$(cd $directory/logs; ls -rtTl | grep '\.log' | tail -1 | awk '{printf("%s\">%s %s</a>\n",$10,$6,$7)}')"
      if [ "$latest_suffix" ]; then
        latest="<a href=\"http://pointyhat.freebsd.org/errorlogs/$arch-$build-latest-logs/$latest_suffix"
      else
        latest="&nbsp;"
      fi
    fi

    # column: INDEX count
    n_index=0
    if [ -f $indexfile ]; then
      n_index=`cat $indexfile | wc -l`
    fi

    # column: package count
    n_packages=0
    if [ -d $directory/packages/All ]; then
      n_packages=`find $directory/packages/All -name \*.tbz -o -name \*.tgz |wc -l`
    fi

    # column: error count
    n_errors=0
    if [ -d $directory/errors ]; then
      # XXX MCL why doesn't this work???
      #n_errors=`find $directory/errors -name \*.log -o -name \*.log.bz2 |wc -l`
      n_errors=`ls $directory/errors | grep '.log' | wc -l`
    fi

    # column: duds count
    n_duds=0
    if [ -f $directory/duds ]; then
      n_duds=`cat $directory/duds | wc -l`
    fi

    # column: missing count
    if [ $n_index -ne 0 ]; then
      n_missing=`expr $n_index - $n_packages - $n_errors - $n_duds`
    else  # index currently being rebuilt
      n_missing=0
    fi

    # column: done flag
    done_flag="N"
    if [ -f $directory/build.log ]; then
      if [ ! -z "`grep 'all done at ' $directory/build.log`" ]; then
        done_flag="Y"
      fi
    fi

    # decorate the row to make everything less "gray"
    if [ "$done_flag" = "Y" ]; then
      cellcolor=$TDCOLOR_DONE
    else
      cellcolor=$TDCOLOR_NOT_DONE
    fi

    # now write the row
    echo "<tr>" >> ${TMPFILE}

    echo "<th align='left' bgcolor='$THCOLOR'>$arch-$build</th>" >> ${TMPFILE}

    echo "<td align='left' bgcolor='$cellcolor'>" >> ${TMPFILE}
    echo "<a href='http://pointyhat.freebsd.org/errorlogs/$arch-$build-latest/cvsdone'>" >> ${TMPFILE}
    echo "$cvsdone</a></td>" >> ${TMPFILE}

    echo "<td align='left' bgcolor='$cellcolor'>$latest</td>" >> ${TMPFILE}

    # note: ports/INDEX-n is copied to a file called errorlogs/INDEX
    echo "<td align='left' bgcolor='$cellcolor'>" >> ${TMPFILE}
    echo "<a href='http://pointyhat.freebsd.org/errorlogs/$arch-$build-latest/INDEX'>" >> ${TMPFILE}
    echo "$n_index</a></td>" >> ${TMPFILE}

    echo "<td align='right' bgcolor='$cellcolor'>" >> ${TMPFILE}
    echo "<a href='http://pointyhat.freebsd.org/errorlogs/$arch-$build-latest-logs'>" >> ${TMPFILE}
    echo "$n_packages</a></td>" >> ${TMPFILE}

    echo "<td align='right' bgcolor='$cellcolor'>" >> ${TMPFILE}
    echo "<a href='http://pointyhat.freebsd.org/errorlogs/$arch-$build-latest'>" >> ${TMPFILE}
    echo "$n_errors</a></td>" >> ${TMPFILE}

    echo "<td align='right' bgcolor='$cellcolor'>$n_duds</td>" >> ${TMPFILE}

    echo "<td align='right' bgcolor='$cellcolor'>$n_missing</td>" >> ${TMPFILE}

    echo "<td align='center' bgcolor='$cellcolor'>$done_flag</td>" >> ${TMPFILE}

    echo "</tr>" >> ${TMPFILE}
}

write_table_end () {
  echo "</table>" >> ${TMPFILE}
  echo "<br>" >> ${TMPFILE}
}

write_footer () {
  echo "<p>explanation of columns:</p>" >> ${TMPFILE}
  echo "<ul>" >> ${TMPFILE}
  echo "<li><b>latest log</b> is the date of the latest logfile.</li>" >> ${TMPFILE}
  echo "<li><b>cvs date</b> is the date of the latest CVS checkout done by the script.  It may be inaccurate if a manual checkout was done later.</li>" >> ${TMPFILE}
  echo "<li><b>INDEX</b> is number of ports in the INDEX file built from the latest cvs checkout.</li>" >> ${TMPFILE}
  echo "<li><b>packages</b> is number of packages successfully built.</li>" >> ${TMPFILE}
  echo "<li><b>errors</b> is number of packages that failed.</li>" >> ${TMPFILE}
  echo "<li><b>skipped</b> is number of packages that were skipped due to NO_PACKAGE, IGNORE, BROKEN, FORBIDDEN, and so forth (\"duds\" file).</li>" >> ${TMPFILE}
  echo "<li><b>missing</b> is the INDEX column minus the others.  These are packages that have not been built for one reason or another.</li>" >> ${TMPFILE}
  echo "<li><b>done</b> is whether that run terminated normally or not.</li>" >> ${TMPFILE}
  echo "</ul>" >> ${TMPFILE}

  echo "<p>notes:</p>" >> ${TMPFILE}
  echo "<ul>" >> ${TMPFILE}
  echo "<li>on the -exp builds, editors/openoffice.org* are skipped to save time.</li>" >> ${TMPFILE}
  echo "</ul>" >> ${TMPFILE}

  echo "</body>" >> ${TMPFILE}
  echo "</html>" >> ${TMPFILE}
}

# main

write_header

# display all the mainstream builds first
for arch in ${SUPPORTED_ARCHS}; do

  builds=`ls ${ROOT_DIRECTORY}/${arch} | grep '^[1-9]$' | sort`
  if [ ! -z "$builds" ]; then
    write_table_begin

      for build in ${builds}; do
        write_row ${arch} ${build}
      done

    write_table_end
  fi
done

# then display all -exp builds (probably only of interest to portmgr;
#   would break up the logical flow of the above)
for arch in ${SUPPORTED_ARCHS}; do

  builds=`ls ${ROOT_DIRECTORY}/${arch} | grep '^[1-9]-exp$' | sort`
  if [ ! -z "$builds" ]; then
    write_table_begin

      for build in ${builds}; do
        write_row ${arch} ${build}
      done

    write_table_end
  fi
done

write_footer

mv -f ${TMPFILE} ${OUTFILE}
