#!/bin/sh
#BHEADER**********************************************************************
# Copyright (c) 2006   The Regents of the University of California.
# Produced at the Lawrence Livermore National Laboratory.
# Written by the HYPRE team. UCRL-CODE-222953.
# All rights reserved.
#
# This file is part of HYPRE (see http://www.llnl.gov/CASC/hypre/).
# Please see the COPYRIGHT_and_LICENSE file for the copyright notice, 
# disclaimer, contact information and the GNU Lesser General Public License.
#
# HYPRE is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License (as published by the Free Software 
# Foundation) version 2.1 dated February 1999.
#
# HYPRE is distributed in the hope that it will be useful, but WITHOUT ANY 
# WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS 
# FOR A PARTICULAR PURPOSE.  See the terms and conditions of the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Revision: 1.25 $
#EHEADER**********************************************************************


#=============================================================================
# Function to display help information
#=============================================================================
help () 
{
   printf "Usage: $0 \n"
   printf "\n"
   printf "HYPRE script to create a summary file of all tests run each night.\n"
   printf "If a file named autotest_completed exists and is readable then the\n"
   printf "tests have completed and the error files are examined; otherwise\n"
   printf "test is listed as pending in the summary file.\n"
   printf "\n"
}


#==============================================================================
#    Mail error summary file 
#       Define mail recipients and mail program to be used
#       Send messages
#==============================================================================
hypre_mail_summary()
{

  HYPRE_ALL_EMAIL="rfalgout@llnl.gov\
                   abaker@llnl.gov\
                   painter1@llnl.gov\
                   lee123@llnl.gov\
                   kolev1@llnl.gov\
                   yang11@llnl.gov\
                   chtong@llnl.gov\
                   vhenson@llnl.gov"

#
# Define mail program to be used
   [ -x /usr/bin/Mail ] && HYPRE_MAIL=/usr/bin/Mail
   [ -x /usr/bin/mhmail ] && HYPRE_MAIL=/usr/bin/mhmail
   [ -x /usr/bin/mailx ] && HYPRE_MAIL=/usr/bin/mailx
   [ -x /usr/sbin/mailx ] && HYPRE_MAIL=/usr/sbin/mailx
#
#               mail summaries
   SUBJECT="Autotest Error Summary `date '+%D'`"

   ${HYPRE_MAIL} -s "$SUBJECT" $HYPRE_ALL_EMAIL < $HYPRE_TEST_SUMMARY_FILE

   echo "Autotest mail sent"
}


#=============================================================================
# Functions for filtering misleading error messages
#    Removes specified messages from error files that do not reflect actual
#    errors
#=============================================================================
process_filter ()
{
  ErrorFile=$1
  shift
  if (egrep "$@" $ErrorFile >/dev/null) ; then
     mv $ErrorFile $ErrorFile.tmp
     egrep -v "$@" $ErrorFile.tmp > $ErrorFile
     echo "-- applied filter:$@" >> $ErrorFile.orig
     cat $ErrorFile.tmp >> $ErrorFile.orig
  fi
}


apply_error_filters ()
{
  ErrorFile=$1
  if [ -s "${ErrorFile}" ] ; then
    HOST=`hostname`
    case $HOST in
      up*)
        for ii in \
          '===\ End\ of\ Compilation'\
          'cpu\ clock'\
          'wall\ clock'\
          'Compilation\ successful'\
          '^\ Guide\ \ \ '\
          ':\ 0\ errors\ in\ file'\
          'autoconf\ has\ been\ disabled'\
          'automake\ has\ been\ disabled'\
          'autoheader\ has\ been\ disabled'\
          'ltdl.c:'\
          'sidl'\
          '1501-050:\ \(I\)'\
          '1500-010:\ \(W\)'\
          'babel_config.h.in:\ No\ such\ file\ or\ directory'\
          'configure:\ WARNING:\ '
        do process_filter $ErrorFile "${ii}"
        done
        ;;
      alc* | mcr* | peng*)
        for ii in \
          '[A-Za-z0-9_]*\.[cCf]$'\
          '[A-Za-z0-9_]*\.cxx$'\
          '^[ \t]*$'\
          '^[0-9]*\ Lines\ Compiled$'\
          'autoconf\ has\ been\ disabled'\
          'automake\ has\ been\ disabled'\
          'autoheader\ has\ been\ disabled'\
          'ltdl.c:'\
          'sidl'\
          'cpu\ clock'\
          'wall\ clock'\
          'queued'\
          'allocated'\
          '\ remark:\ '
        do process_filter $ErrorFile "${ii}"
        done
        ;;
      *bgl*)
        for ii in \
          'readonly\ variable'\
          'BE_MPI'\
          'FE_MPI\ '
        do process_filter $ErrorFile "${ii}"
        done
        ;;
      thun*)
        for ii in \
          'queued'\
          'allocated'
        do process_filter $ErrorFile "${ii}"
        done
        ;;
      tux*)
        for ii in \
          'autoconf\ has\ been\ disabled'\
          'automake\ has\ been\ disabled'\
          'autoheader\ has\ been\ disabled'\
          'configure:\ WARNING:\ Configuration\ for'\
          'configure:\ WARNING:\ Skipping\ Java'\
          'cpu\ clock'\
          'wall\ clock'\
          'Error:\ PDF'\
          'babel_config.h.in:\ No\ such\ file\ or\ directory'\
          '../../../babel/runtime'\
          'ltdl.c:'\
          'sidl'\
          'Insure\ messages\ will\ be\ written\ to'\
          'Timeout\ in\ waiting\ for\ processes\ to'\
          'rsh\ program'\
          'problem'\
          'This\ is\ not\ a\ problem'\
          'environment'\
          'process\ termination'\
          'TCA\ log\ data\ will\ be\ merged\ with\ tca'\
          'PGC/x86\ Linux/x86\ 3.3-1:\ compilation\ completed\ with\ warnings'\
          'sed:\ Couldn.t\ close\ '
        do process_filter $ErrorFile "${ii}"
        done
        ;;
    esac
  fi
}


#==============================================================================
#    Search given directory for non-zero length error files
#       directory is set just before calling this routine
#       run ls to get a list of error files
#       for each file in the list; if it exists and is readable;
#       apply the error filters then check to see if it still exists
#       with a non-zero length; if so, increment the error counter
#==============================================================================
search_error_files()
{
   errlist="`ls *.err`"
   for errfile in ${errlist}
   do
      if [ -r ${errfile} ]
      then
         apply_error_filters ${errfile}
         if [ -s ${errfile} ]
         then
            TOTERR=`expr $TOTERR + 1`
         fi
      fi
   done
}


#==============================================================================
#     get name of test that was run based on remote directory
#==============================================================================
get_hypre_test_name()
{
   case ${remote_dir} in
      *aix_5_64_fed/AUTOTEST)
         hypre_test_name="SECONDARY[uP]"
         ;;
      *chaos_3_x86_elan3/AUTOTEST)
         hypre_test_name="SECONDARY[ALC/MCR/Pengra]"
         ;;
      *chaos_3_ia64_elan4/AUTOTEST)
         hypre_test_name="SECONDARY[Thunder]"
         ;;
      *chaos_3_x86_64_ib/AUTOTEST)
         hypre_test_name="SECONDARY[vertex]"
         ;;
      *sles_9_ppc64/AUTOTEST)
         hypre_test_name="SECONDARY[ubgl]"
         ;;
      *i686-pc-linux-gnu/AUTOTEST)
         hypre_test_name="PRIMARY[default]"
         ;;
      *i686-pc-linux-gnu-insure/AUTOTEST)
         hypre_test_name="PRIMARY[insure++]"
         ;;
      *i686-pc-linux-gnu-pgi5.x/AUTOTEST)
         hypre_test_name="SECONDARY[PGI]"
         ;;
      *i686-pc-linux-gnu-gcc3.x/AUTOTEST)
         hypre_test_name="SECONDARY[Absoft/Lahey/Intel]"
         ;;
      *i686-pc-linux-gnu-kai/AUTOTEST)
         hypre_test_name="SECONDARY[KCC]"
         ;;
      *)
         hypre_test_name="LOCAL-TEST[default]"
         ;;
   esac
}

#==============================================================================
# Beginning of 'main program' of script.
# Create summary file of test errors
#    Set list of default tests and driver code directories
#    In the directory in which this script is running, create the output file
#    For each test in the default list, search the list of tests run for it
#    if the test was run, search the linear_solvers, test and driver directories
#    for error files of non-zero length.  If there are some error files; get the
#    name of the individual summary error file in the REMOTE_DIR.  
#    All printing is done in the directory in which this script is running.
#==============================================================================

#==============================================================================
#     Print usage information if requested by command line argument.
#==============================================================================
case $1 in
   -h|-help)
       help
       exit
       ;;
esac


HYPRE_SUMMARY_DIR="/usr/casc/hypre/AUTOTEST"
 
HYPRE_DEFAULT_REMOTE_DIRS="/usr/casc/hypre/i686-pc-linux-gnu/AUTOTEST\
                           /usr/casc/hypre/i686-pc-linux-gnu-insure/AUTOTEST\
                           /usr/casc/hypre/aix_5_64_fed/AUTOTEST\
                           /usr/casc/hypre/chaos_3_x86_elan3/AUTOTEST\
                           /usr/casc/hypre/chaos_3_ia64_elan4/AUTOTEST\
                           /usr/casc/hypre/chaos_3_x86_64_ib/AUTOTEST\
                           /usr/gapps/hypre/sles_9_ppc64/AUTOTEST\
                           /usr/casc/hypre/i686-pc-linux-gnu-pgi5.x/AUTOTEST\
                           /usr/casc/hypre/i686-pc-linux-gnu-kai/AUTOTEST\
                           /usr/casc/hypre/i686-pc-linux-gnu-gcc3.x/AUTOTEST"

HYPRE_STD_TEST_DIRS="TEST_ams TEST_fac TEST_ij TEST_sstruct TEST_struct TEST_examples"

cd ${HYPRE_AUTOTEST_EXECUTE_DIR}
HYPRE_TEST_SUMMARY_FILE="autotest_summary.`date '+%Y%m%d'`.html"

printf "<html>\n" > $HYPRE_TEST_SUMMARY_FILE
printf "<head> </head>\n" >> $HYPRE_TEST_SUMMARY_FILE
printf "<PRE>\n" >> $HYPRE_TEST_SUMMARY_FILE
printf "<body>\n" >> $HYPRE_TEST_SUMMARY_FILE
printf "%s<br>\n" "       Autotest Results for `date '+%D'` " >> $HYPRE_TEST_SUMMARY_FILE
printf "%s<br>\n" "--------------------------------------------" >> $HYPRE_TEST_SUMMARY_FILE

for remote_dir in ${HYPRE_DEFAULT_REMOTE_DIRS}
do
   TOTERR=0
   found_test=0

   if [ -r ${remote_dir}/autotest_start ]
   then
      found_test=1
      if [ ! -r ${remote_dir}/autotest_done ]
      then
         TOTERR=-999
      else
         cd ${remote_dir}/linear_solvers
         search_error_files

         cd ${remote_dir}/linear_solvers/docs
         search_error_files

         cd ${remote_dir}/linear_solvers/test
         search_error_files

         for dir in ${HYPRE_STD_TEST_DIRS}
         do
            cd ${remote_dir}/linear_solvers/test/${dir}
            search_error_files
         done

         cd ${remote_dir}
         archfile=`ls autotest.*.html`
         if [ -d ${remote_dir}/log ]
         then
            resultsfile="${remote_dir}/log/${archfile}"
            mv -f ${archfile} ${remote_dir}/log
         else
            resultsfile="${remote_dir}/${archfile}"
         fi

         rm -rf ${remote_dir}/autotest_start ${remote_dir}/autotest_done
      fi
   fi

   cd ${HYPRE_AUTOTEST_EXECUTE_DIR}
   get_hypre_test_name
   if [ "${found_test}" = "1" ]
   then
      if [ "${TOTERR}" = "0" ]
      then
         printf "%-29s: " $hypre_test_name >> $HYPRE_TEST_SUMMARY_FILE
         printf "  `date '+%D'`" >> $HYPRE_TEST_SUMMARY_FILE
         printf "    PASSED" >> $HYPRE_TEST_SUMMARY_FILE
         printf "  <a href=\"%s\">%s</a><br>\n" ${resultsfile} ${archfile} >> $HYPRE_TEST_SUMMARY_FILE
      elif [ "${TOTERR}" = "-999" ]
      then
         printf "%-29s: " $hypre_test_name >> $HYPRE_TEST_SUMMARY_FILE
         printf "              PENDING<br>\n" >> $HYPRE_TEST_SUMMARY_FILE

      else
         printf "%-29s: " $hypre_test_name >> $HYPRE_TEST_SUMMARY_FILE
         printf "  `date '+%D'`" >> $HYPRE_TEST_SUMMARY_FILE
         printf "%5d Errors" ${TOTERR} >> $HYPRE_TEST_SUMMARY_FILE
         printf "  <a href=\"%s\">%s</a><br>\n" ${resultsfile} ${archfile} >> $HYPRE_TEST_SUMMARY_FILE
      fi
   else
      printf "%-29s: " $hypre_test_name >> $HYPRE_TEST_SUMMARY_FILE
      printf "                N/A<br>\n" >> $HYPRE_TEST_SUMMARY_FILE
   fi

done
printf "</body>\n" >> $HYPRE_TEST_SUMMARY_FILE
printf "</PRE>\n" >> $HYPRE_TEST_SUMMARY_FILE
printf "</html>\n" >> $HYPRE_TEST_SUMMARY_FILE


#==============================================================================
#    Set permissions
#==============================================================================
chmod -fR a+rX,ug+w,o-w ${HYPRE_SUMMARY_DIR}
chgrp -fR hypre ${HYPRE_SUMMARY_DIR}


#==============================================================================
#    Mail summary file
#==============================================================================
hypre_mail_summary


#==============================================================================
#    Store summary file in permanent location
#==============================================================================
mv -f autotest_summary* ${HYPRE_SUMMARY_DIR}
