#! /bin/csh -f
if ($#argv > 0) then
    if ("$1" == "-help") then
        echo "$0 file [ -rc ]"
	echo "Extracts error messages from a install log file.  This script"
	echo "is under construction and should not be counted on to be"
	echo "perfect.  "
	echo " "
	echo "if -rc is set, there is no output.  Instead, the return code "
	echo "is set to 0 if no errors were found, and to the number of "
	echo "errors otherwise.  This may be used by scripts to determine"
	echo "if the install completed correctly."
	echo " "
	echo "Also extracts test suite results (examples/test)."
	echo "(By default, only shows results of test suite, not run"
	echo "information that includes time and host.)
        exit 1
    endif
endif
#
# rs6000 error strings:
# "filename", line 000.00: 1506-000 (S/W/E) string.
# Look for `"' as first character
# ar: 0000-000 string
# Look for "ar:" as leading string
# Look for "xlc:" as leading string
# Look for "digits- ERROR: (^[-0-9 ]*ERROR:)
#
# sun4 error strings:
# "filename", line 000: string
# Look for `"' as first character
# Also,
# Does filename: ddd: string: error
# for cpp errors.  Look for "string:"
#
# Intel error strings:
# PGC-...
# Look for `PGC-' as leading string
#
# Convex (c2mp) error strings:
# cc: ...
#
# Cray error strings:
# cc-... cc: ...\n<message>\n
# cft77-.. cf77: ...\n<message>\n
#
# IRIX error strings:
# accom: ...
# /usr..../...:
#
# 386BSD:
# cc: ...
# Error on line ... of ...:
# filename:linenumber:
#
# LINUX:
# make[*]: *** ....
# Exit status ....
# <arb text>: <arb text>
#
# Things to exclude:
# Note that X11R5 definitions do NOT include prototypes (!); some R6 versions
# have the same problems.  Reject strings containing
#    /X11R5/.../Xlib.h: ... function declaration ... a prototype
#
# We also need to accept /..../mpicc and /.../mpif77 as names for the
# compilers, since this is what is used in the tests.  gcc is also a
# valid compiler name.
#
# Some systems have error messages of the form:
#  ld:
#  Unresolved:
#  ... list of names
#  blank line
# Find these with sed -n -e '/Unresolved:/,/^.*$/p'
set ISRC = 0
set RC   = 0
set LARCH = ""
if ( -e bin/tarch ) set LARCH = `bin/tarch`
if ($LARCH == cray) then
    if ($ISRC) then
        set RC = `egrep '^cc-[0-9]* cc:|^Make:|^ cft77-[0-9]* cf77:' $1 | wc -l`
    else
        egrep '^cc-[0-9]* cc:|^Make:|^ cft77-[0-9]* cf77:' $1
    endif
    exit($RC)
endif
if ($LARCH == "386BSD") then
    # was ^[a-zA-Z0-9]*\.c: [1-9]
    if ($ISRC) then
        set RC = `egrep '^"|^Error on line|syntax error|^ar:|^mv:|^ld:|^Archive:|^make:|^xlc:|^PGC\-|^[a-zA-Z0-9_][a-zA-Z0-9_]*\.c:|^cc:' $1 | \
     egrep -v '/X11R5/.*/Xlib.h:.*function declaration.*a prototype' | \
     egrep -v 'if \[ .* \] ; then ln -s .* fi' |\
     egrep -v '/X11/Xlib.h:.*function declaration.*a prototype' | \
             wc -l`
    else
        egrep '^"|^Error on line|syntax error|^ar:|^mv:|^ld:|^Archive:|^make:|^xlc:|^PGC\-|^[a-zA-Z0-9_][a-zA-Z0-9]*\.c:|^cc:' $1 | egrep -v '/X11R5/.*/Xlib.h:.*function declaration.*a prototype' | \
     egrep -v 'if \[ .* \] ; then ln -s .* fi' |\
     egrep -v '/X11/Xlib.h:.*function declaration.*a prototype' 
    endif
    exit($RC)
endif
#
if ($LARCH == "LINUX") then
    # LINUX has very hard to detect error messages.
    if ($ISRC) then
    # was ^[a-zA-Z0-9]*\.c: [1-9]
    set RC = `egrep '^"|^\.|^\*\#|^/usr.*:|^Error|syntax error|^ar:|^mv:|^ld:|^Archive:|^make:|^make\[|^[a-zA-Z0-9_]*\.c:|^[a-zA-Z0-9_][a-zA-Z0-9_]*\.o.*:|^cc:|^f77:' $1 | egrep -v 'Warning .*declared EXTERNAL but never used' | \
 egrep -v 'if \[ .* \] ; then ln -s .* fi' |\
 wc -l`
    set RC1 = `egrep 'No space left|^[-0-9 ]*ERROR:|Connection timed' $1 | \
		wc -l`
    if ($RC1 != 0) set RC = $RC1
else
    egrep '^"|^\.|^\*\#|^/usr.*:|^Error|syntax error|^ar:|^mv:|^ld:|^Archive:|^make:|^make\[|^[a-zA-Z0-9_]*\.c:|^[a-zA-Z0-9_][a-zA-Z0-9_]*\.o.*:|^cc:|^f77:|^[-0-9 ]*ERROR:' $1 | \
    egrep -v 'Warning .*declared EXTERNAL but never used' |\
    egrep -v 'if \[ .* \] ; then ln -s .* fi'
    egrep 'No space left|Connection timed' $1
endif
    exit($RC)
endif
#
# This is the fall-through case 
#
if ($ISRC) then
    # was ^[a-zA-Z0-9_]*\.c: [1-9]
    set RC = `egrep '^"|^\\.|^\*\#|^accom:|^/usr.*:|^Error|syntax error|^ar:|^mv:|^ld:|^Archive:|^make:|^xlc:|^cfe:|^PGC\-|^[a-zA-Z0-9_][a-zA-Z0-9_]*\.c:|^cc:|^f77:' $1 | \
	egrep -v '/X11R5/.*/Xlib.h:.*function declaration.*a prototype' \
        egrep -v '/X11/Xlib.h:.*function declaration.*a prototype' | \
        egrep -v '^ar: creating|^ar: writing' | \
        egrep -v 'Warning .*declared EXTERNAL but never used' | \
        egrep -v 'if \[ .* \] ; then ln -s .* fi' |\
	egrep -v 'Type long double has the same' |\
	| wc -l`
    set RC1 = `egrep 'No space left|^[-0-9 ]*ERROR:|Connection timed' $1 | \
		wc -l`
    if ($RC1 != 0) set RC = $RC1
else
    /bin/rm -f .tmp
    egrep '^"|^\\.|^\*\#|^accom:|^/usr.*:|^Error|syntax error|^ar:|^mv:|^ld:|^Archive:|^make:|^xlc:|^cfe:|^PGC\-|^[a-zA-Z0-9_][a-zA-Z0-9_]*\.c:|^cc:|^f77:|^[-0-9 ]*ERROR:' $1 | \
    egrep -v '/X11R5/.*/Xlib.h:.*function declaration.*a prototype' |\
    egrep -v '/X11/Xlib.h:.*function declaration.*a prototype' | \
    egrep -v '^ar: creating|^ar: writing' | \
    egrep -v 'Warning .*declared EXTERNAL but never used' | \
    egrep -v 'if \[ .* \] ; then ln -s .* fi' |\
    egrep -v 'Type long double has the same' >& .tmp
    if ($?MPICH_WARNING_SEP) then
       /bin/rm -f .tmp2
       sed -e '/>'"$MPICH_WARNING_SEP"/,'/<'"$MPICH_WARNING_SEP"/d .tmp > .tmp2
       /bin/rm -f .tmp
       mv .tmp2 .tmp
    endif
    cat .tmp
    egrep 'No space left|Connection timed' $1
endif
#
# Try for test suite.  Print out the head line and the summary for the 
# directory.
# Grumble.  IRIX can abort, flushing the 'End of testing' message.
sed -n -e '/^Making testing/p' -e '/\*\*\* Checking for diff/,/^End of testing/p' $1 | grep -v '^.*/mpicc' | \
	grep -v '^.*/mpif77'
#
# Try for ld messages
sed -n -e '/Unresolved:/,/^[ ]*$/p' $1
exit($RC)
