#! /bin/sh
########################################################################
#
# /u/sonmi/bin/qa_stat - /u/svbld/bin/init/nss/qa_stat
#
# this script is supposed to automatically run QA for NSS on all required
# Unix platforms - warning - will not run on Windows
#
# parameters
# ----------
#    nssversion (supported: 30b, 31, tip)
#    builddate  (default - today)
#
# options
# -------
#    -y answer all questions with y - use at your own risk...ignores warnings
#    -s silent (only usefull with -y)
#    -h, -? - you guessed right - displays this text
#    -d debug
#    -f <filename> - write the (error)output to filename
#    -m <mailinglist> - send filename to mailinglist (csl) only useful
#        with -f
#    -cron equivalient to -y -s -d -f $RESULTDIR/$HOST.qa_stat
#
########################################################################


O_OPTIONS=ON

. `dirname $0`/header

EARLY_EXIT=TRUE

DOCDIR=/u/sonmi/doc

# this file is used to deal with hanging rsh - a new shell is started 
# for each rsh, and a function is called after it is finished - they
# communicate with this file
RSH_FILE=$TMP/rsh.$$
echo >$RSH_FILE
TMPFILES="$TMPFILES $RSH_FILE $WARNING_FILE"
RSH_WAIT_TIME=60 #maximum time allowed for the 2 rsh to finish...
TOTAL_TESTS=106

Debug "NTDIST $NTDIST"
Debug "UXDIST $UXDIST"
Debug "TESTSCRIPTDIR $TESTSCRIPTDIR"
Debug "RESULTDIR $RESULTDIR"

############################### watch_rsh ##############################
# local shell function, deals with a hanging rsh (kills it...)
# this function is started as a backgroundprocess before the rsh is started,
# and writes info to the RSH_FILE, after the rsh is finished it writes finish
# info to the same file (this time called as a function, forground). 
# the backgroundprocess stays around for RSH_WAIT_TIME, if then the finish 
# information is not there attempts to kill the rsh
#
# watch_rsh start qa_computername &
# watch_rsh stop qa_computername 
#
########################################################################
watch_rsh()
{
    case $1 in
        start)
            echo "$2 started" >>$RSH_FILE
            sleep $RSH_WAIT_TIME
            O_ALWAYS_YES=ON # may modify global flags because this is a 
                            # forked off bg process - kill_by_name otherwise
                            # will ask the user if it really should be killed 
            grep "$2 finished" $RSH_FILE >/dev/null || kill_by_name "rsh $2"
            exit
            ;;
        stop)
            echo "$2 finished" >>$RSH_FILE
            ;;
    esac
}

############################### find_qa_systems ########################
# local shell function, tries to determine the QA operating system
########################################################################
find_qa_systems()
{

#the line below had a bug, which caused nssqa as being run when only 
#the .nssqa file existed
#FIXME remaining bugs: 
#when only debug but not opt is being run this is not recognized. 
#2000 and NT are reported the same way
#it should also examine the directory (.1) for files 

for w in `ls $RESULTDIR | grep \.1$ | sed -e "s/\..*//" | sort -u`
do
    NO_RSH="FALSE"
    QA_OS=""
    QA_RHVER=""
    IS_64=""
    IS_WIN=""

    grep WINDOWS-OS-LINE ${RESULTDIR}/${w}.nssqa && NO_RSH=TRUE

    if [ "$NO_RSH" = "TRUE" ]
    then
        grep WINDOWS-OS-LINE ${RESULTDIR}/${w}.nssqa | sed -e "s/ /_/g" \
             -e "s/WINDOWS-OS-LINE:_Windows/${w}/g" >>$TMP_PLATFORMLIST_FILE
        grep WINDOWS-OS-LINE ${RESULTDIR}/${w}.nssqa | sed -e "s/ /_/g" \
             -e "s/WINDOWS-OS-LINE:_Windows/${w}/g" 
    else
        watch_rsh start $w &
        QA_SYS=`rsh $w uname -sr`
        echo $QA_SYS | grep Linux && QA_RHVER=`rsh $w cat /etc/redhat-release`
        if [ -n "$QA_RHVER" ]
        then
            QA_OS=`echo $w $QA_RHVER | sed -e "s/Red Hat /RH /" \
                        -e "s/ release//"`
        else
            case $QA_SYS in
                *SunOS*5.8*)
                    IS_64=`rsh $w isainfo -v | grep 64 >/dev/null && \
                        echo 64 bit`
                    if [ -z "$IS_64" ] ; then IS_64="32 bit"; fi;
                    ;;
                *HP*)
                    IS_64=`rsh $w getconf KERNEL_BITS | grep 64 >/dev/null && \
                        echo 64 bit`
                    if [ -z "$IS_64" ] ; then IS_64="32 bit"; fi;
                    ;;
                *AIX*)
                    IS_64=`rsh $w lslpp -l | grep "bos.64bit"> /dev/null && \
                        echo 64 bit`
                    if [ -z "$IS_64" ] ; then IS_64="32 bit"; fi;
                    ;;
            esac
            watch_rsh stop $w 
            QA_OS=`echo "$w $QA_SYS $IS_64"`
        fi
        echo $QA_OS
        echo $QA_OS | sed -e "s/ /_/g" >>$TMP_PLATFORMLIST_FILE    # use later for 
                                                            # missing list
    fi
done

}
################################### set_files ##########################
# local shell function, sets the name of the resultfile to:
#    <filename> if option -f <filename>
#    $RESULTDIR/result if write permission 
#        (mozilla/tests_results/security/result)
#    $HOME/resultNSS${NSSVER}-${BUILDDATE} if no write permission in $RESULTDIR
########################################################################
set_files()
{
    if [ $O_FILE = ON -a $O_CRON = OFF ]    # if -f was specified write there 
    then
        RFILE=$FILENAME    
    else
        RFILE=${RESULTDIR}/result
        #RFILE=${RESULTDIR}/result.$$
        if [ ! -w $RESULTDIR ]
        then
            RFILE=$HOME/resultNSS${NSSVER}-${BUILDDATE}
            Debug "Using alternate resultfile $RFILE"
        elif [ $O_CRON = ON ]
        then
             find ${RESULTDIR} -exec chmod a+rw {} \;    #FIXME - umask 
                            #doesn't seem to work - this is a tmp workaround
        fi
    
        if [ ! -x $RESULTDIR -o ! -r  $RESULTDIR -o ! -w $RESULTDIR ]
        then
            glob_usage "$RESULTDIR does not have the right permissions `ls -l $RESULTDIR`"
        fi
        if [ -d $RESULTDIR ]
        then
            cd $RESULTDIR
        else
            glob_usage "$RESULTDIR does not exist"
        fi
    fi
    TMP_E_FILE=${RFILE}.E$$
    TMP_P_FILE=${RFILE}.P$$
    TMP_PLATFORMLIST_FILE=${RFILE}.platform.$$
    rm $TMP_PLATFORMLIST_FILE 2>/dev/null
    TMP_PERF_FILE=${RFILE}.perf.$$
    HTML_FILE=${RFILE}.html
    WARNING_FILE=${RFILE}.warning
    TMPFILES="$TMPFILES  $TMP_E_FILE $TMP_P_FILE $WARNING_FILE $TMP_PERF_FILE $TMP_PLATFORMLIST_FILE"

#FIXME rm the rest too - add them to the TMPFILES
    FILENAME=$RFILE        #we might want to mail it...later switch to html file
    O_FILE="ON"
    rm $HTML_FILE $RFILE $TMP_E_FILE $TMP_P_FILE 2>/dev/null
    cp $DOCDIR/QAheader.html $HTML_FILE
}

################################# html_eot #########################
# local shell function, writes end of the html table
####################################################################
html_eot()
{
echo '</table>'
}

################################# html_footer #########################
# local shell function, writes end of the html body
#######################################################################
html_footer()
{

echo '</body>'
echo '</html>'

}

################################# setQAsysvars #########################
# local shell function, sets system specific variables
########################################################################
setQAsysvars()
{
    if [ "$MACHINE" != "0" ]
    then
        TESTDATE=`ls -ld $MACHINE | awk '{ print $5, $6, $7 }'`
        TESTNUMBER=`echo $MACHINE | sed -e 's/.*\.//'`
        SYSNAME=`echo $MACHINE | sed -e 's/\..*//'`
        Debug "SYSNAME= $SYSNAME"
        QA_SYS_OS=`grep $SYSNAME $TMP_PLATFORMLIST_FILE |sed -e 's/
//' | \
            sort | uniq | sed  -e "s/$SYSNAME//" \
            -e "s/WINNT_WIN95/Windows NT/"  \
            -e "s/WINNT_WINNT/Windows NT/"  \
            -e "s/Windows_NT_WIN95/Windows NT/"  \
            -e "s/Windows_NT_WINNT/Windows NT/"  | sort | uniq`
        Debug "QA_SYS_OS= $QA_SYS_OS"
    fi
    BUILD_SYS=`echo $BUILDPLATFORM | sed -e 's/\.OBJ//' -e 's/_DBG/ Debug/' \
            -e 's/_OPT/ Optimized/'  -e 's/_64/ 64bit/' -e 's/_glibc_PTH//' \
            -e 's/_/ /'`
    Debug "BUILD_SYS=$BUILD_SYS"
    if [ -f "${RESULTDIR}/${MACHINE}/results.html" ] 
    then
        RESULT="http://cindercone${RESULTDIR}/${MACHINE}/results.html"
        Debug "RESULT=$RESULT"
    else
        RESULT="0"
        Debug "no resultfile"
    fi

    if [ -f "${RESULTDIR}/${MACHINE}/output.log" ] 
    then
        LOG="http://cindercone${RESULTDIR}/${MACHINE}/output.log"
        Debug "LOG=$LOG"
    else
        LOG="0"
        Debug "no logfile"
    fi
}

HTML_ERRORCOLOR=\"#FF0000\"
HTML_ERRORMSG=Failed

HTML_MISSINGCOLOR=\"#FFFFCC\"
HTML_MISSINGMSG=Missing

HTML_INCOMPLETECOLOR=$HTML_MISSINGCOLOR
HTML_INCOMPLETEMSG=Incomplete

HTML_PASSEDCOLOR=\"#66FF99\"
HTML_PASSEDMSG=Passed
################################# html_line() #########################
# local shell function, writes a line in the html table
########################################################################
html_line()
{
  echo '<tr NOSAVE>'
  echo '<td NOSAVE>'$BUILD_SYS'</td>'
  echo ''
  echo '<td NOSAVE>'$QA_SYS_OS'</td>'
  echo ''
  echo '<td>'$SYSNAME'</td>'
  #echo '<td>'$SYSNAME $TESTNUMBER $TESTDATE'</td>'
  echo ''
  if [ "$1" = "failed" ]
  then
      echo '<td BGCOLOR='$HTML_ERRORCOLOR' NOSAVE><b>'$HTML_ERRORMSG'</b></td>'
  elif [ "$1" = "passed" ]
  then
      echo '<td BGCOLOR='$HTML_PASSEDCOLOR' NOSAVE>'$HTML_PASSEDMSG'</td>'
  elif [ "$1" = "incomplete" ]
  then
      echo '<td BGCOLOR='$HTML_INCOMPLETECOLOR' NOSAVE>'$HTML_INCOMPLETEMSG'</td>'
  else
      echo '<td BGCOLOR='$HTML_MISSINGCOLOR' NOSAVE>'$HTML_MISSINGMSG'</td>'    
  fi
  
  if [ "$RESULT" = "0" ]
  then
      echo '<td BGCOLOR='$HTML_MISSINGCOLOR' NOSAVE>'$HTML_MISSINGMSG'</td>'
  else
      echo '<td>&nbsp;<a href="'$RESULT'">result</a>&nbsp;</td>'
  fi
  echo ''
  if [ "$LOG" = "0" ]
  then
      echo '<td BGCOLOR='$HTML_MISSINGCOLOR' NOSAVE>'$HTML_MISSINGMSG'</td>'
  else
      echo '<td>&nbsp;<a href="'$LOG'">log</a>&nbsp;</td>'
  fi
  echo ''
  if [ "$1" = "failed" ]
  then
      echo '<td>&nbsp;<a href="#errorlist">error</a>&nbsp;</td>'
  else
      echo '<td></td>'
  fi
  echo '<td>'$TESTDATE $TESTNUMBER'</td>'
  echo '</tr>'
}

################################# qa_errorlist #########################
# local shell function, finds problems in the previously run QA
########################################################################
qa_errorlist()
{
    if [ "$1" = "1" ]
    then
        echo "    R e s u l t        E r r o r l i s t"
        lline
    fi

    grep "bgcolor=red" */results.html | 
        sed -e 's/.results.html:<TR><TD>/ /' -e 's/<[^>]*>/ /g'
    if [ "$1" = "1" ]
    then
        lline
        echo "    O u t p u t        E r r o r / W a r n i n g l i s t"
        lline
    fi

    grep 'cache hits; .* cache misses, .* cache not reusable' */output.log | 
        grep -v selfserv |
        grep -v '0 cache hits; 1 cache misses, 0 cache not reusable' | 
        grep -v '0 cache hits; 0 cache misses, 0 cache not reusable' | 
        grep -v ' cache hits; 1 cache misses, 0 cache not reusable'
    grep -i error */output.log |
        grep -vi "write to SSL socket" |
        grep -vi "HDX PR_Read returned error" |
        grep -vi "no error" |
        grep -vi "12285"
    grep -i failed */output.log | 
        grep -vi "write to SSL socket" | 
        grep -vi "peer cannot verify" | 
        grep -vi "error" |
        grep -vi "fatal" |
        grep -vi "TCP connection reset"
    grep -i fatal */output.log
        #grep -v '999 cache hits; 1 cache misses, 0 cache not reusable'
    #if [ "$1" = "1" ]
    #then
        #lline
        #echo "    I n c o m p l e t e    Q A     L i s t"
        #lline
    #fi
    if [ "$1" = "1" ]
    then
        lline
        echo "    C o r e l i s t "
        lline
    fi
    find . -name core -print 2>/dev/null

}

platformlist()
{
    lline
    echo "    P l a t f o r m l i s t "
    
    grep Platform */results.html | 
        sed -e 's/.results.html:<H4>Platform:/    /' \
            -e 's/<BR>//' |
    while read MACHINE BUILDPLATFORM
    do
        grep $MACHINE $1 >/dev/null
        ret=$?
        setQAsysvars
        if [ $ret -eq 0 ]
        then
            echo "Failed $MACHINE $BUILDPLATFORM"
            html_line failed >>$HTML_FILE
            #echo "1 $MACHINE $BUILDPLATFORM Failed 2" 
        else
            echo "Passed $MACHINE $BUILDPLATFORM"
            html_line passed >>$HTML_FILE
            #echo "1 $MACHINE $BUILDPLATFORM passed 2"
        fi
    done 

}

############################ check_platforms ###########################
# local shell function, finds out if we ran on all required platforms
########################################################################
check_platforms()
{
    #lline
    #echo "    M i s s i n g  P l a t f o r m s"
    #lline
    QA_MISSING="QA report missing"
    MACHINE="0"

    for BUILDPLATFORM in `cat $TESTSCRIPTDIR/platformlist`
    do
        grep $BUILDPLATFORM $TMP_PLATFORMLIST_FILE > /dev/null || {
            echo "$BUILDPLATFORM not tested: $QA_MISSING" >>$TMP_P_FILE
            setQAsysvars
            html_line missing >>$HTML_FILE
        }
    done

    for w in `ls */results.html`
    do
        grep bgcolor=red $w || {
        PASSED_LINES=""
        PASSED_LINES=`grep bgcolor=lightGreen $w | wc -l`
        if [ -n "$PASSED_LINES" -a "$PASSED_LINES" -lt "$TOTAL_TESTS" ] ; then
            BUILDPLATFORM=`grep Platform $w | sed -e 's/<H4>Platform:/    /'                   -e 's/<BR>//'` 
            MACHINE=`echo $w | sed -e "s/\.[0-9]*.results.html//"`
            setQAsysvars
            html_line incomplete >>$HTML_FILE
        elif [ "$PASSED_LINES" -gt "$TOTAL_TESTS" ] ; then
            echo "WARNING - more tests than expected on $w ($PASSED_LINES)" >>$WARNING_FILE
	fi
        }
    done
}

lline()
{
    echo
    echo "------------------------------------------------------------------"
    echo
}

header()
{
    lline
    echo "QA results for NSS $NSSVER builddate $QAYEAR $BUILDDATE "
    echo "today's date `date`"
    lline
    echo "testing in $RESULTDIR"
}

rsaperf()
{
    grep RSAPERF */output.log | grep -v "_DBG" > $TMP_PERF_FILE
    
    echo '&nbsp;'
    echo '<br>&nbsp;'
    echo '<center>'
    echo '<h1>'
    echo '<a NAME="Performance list"></a>Performance list</h1></center>'
    echo '&nbsp;'
    echo '<table BORDER WIDTH="100%" NOSAVE >'
    echo '<tr NOSAVE>'
    echo '<td NOSAVE><b><font size=+1>Build-OS and version</font></b></td>'
    echo ''
    echo '<td><b><font size=+1>Systemname</font></b></td>'
    echo ''
    echo '<td><b><font size=+1># of iterations</font></b></td>'
    echo ''
    echo '<td><b><font size=+1>average for one op</font></b></td>'
    echo ''
    echo '<td><b><font size=+1>Total</font></b></td>'
    echo ''
    echo '<td><b><font size=+1>QA time / #</font></b></td>'
    echo '</tr>'
    cat $TMP_PERF_FILE | while read MACHINE BUILDPLATFORM no_iter t1 t2 total total_unit t3 t4 t5 average average_unit
    do
        BUILD_SYS=`echo $BUILDPLATFORM | sed -e 's/\.OBJ//' \
            -e 's/_DBG/ Debug/' \
            -e 's/_OPT/ Optimized/'  -e 's/_64/ 64bit/' -e 's/_glibc_PTH//' \
            -e 's/_/ /'`
        TESTNUMBER=`echo $MACHINE | sed -e 's/[^\.]*\.//' -e 's/\/.*//'`
        MACHINE=`echo $MACHINE | sed -e 's/\..*//'`
        TESTDATE=`ls -ld ${MACHINE}.${TESTNUMBER} | awk '{ print $5, $6, $7 }'`
        echo '<tr>'
        echo '<td>'$BUILD_SYS'</td>'
        echo ''
        echo '<td>'$MACHINE'</td>'
        echo ''
        echo '<td>'$no_iter'</td>'
        echo ''
        echo '<td>'$average' '$average_unit'</td>'
        echo ''
        echo '<td>'$total' '$total_unit'</td>'
        echo ''
        echo '<td>'$TESTDATE $TESTNUMBER'</td>'
        echo ''
        echo '</tr>'
    done
    echo '</table>'
}

set_files

find_qa_systems 2>/dev/null

#header > $RFILE

qa_errorlist 1 > $TMP_E_FILE

cat $TMP_E_FILE

platformlist $TMP_E_FILE > $RFILE

cat $RFILE

echo "" >$TMP_P_FILE

check_platforms $RFILE 

html_eot >>$HTML_FILE


rsaperf >>$HTML_FILE

echo '<a NAME="errorlist"></a>' >> $HTML_FILE

qa_errorlist 2  | sed -e "s/^/<br>/" >>$HTML_FILE
cat $WARNING_FILE   2>/dev/null | sed -e "s/^/<br>/" >>$HTML_FILE 2>/dev/null

html_footer >>$HTML_FILE

EARLY_EXIT=FALSE
FILENAME=$HTML_FILE        #we might want to mail it...
Exit
