#! /bin/sh
#
no_tar=no
no_varwarn=no
filename=$1
for arg in "$@" ; do 
    shift

    case $arg in
    -nowarn) MPICH_WARNING_SEP='MPICH Warning' 
    ;;
    
    -echo) set -x
    ;;

    -notar)  
    # special command to skip any step that extracts and builds the mpich
    # tar file.
    no_tar=yes
    ;;

    -novarwarn)
    # Special command to skip "The variable ... is set but never used"
    no_varwarn=yes
    ;;

    *) 
    filename=$arg
    break
    ;;
    esac
done

# Some systems have limits on the number of pipe stages.  We break this into
# steps of no more than 10 stages, using .tmp as the intermediate
/bin/rm -f .tmp
sed -e '/^Making testing/,/\*\*\* Checking for diff/d' $filename | \
sed -e '/^--->/,/^---</d' | \
sed -e '/^\*# --->/,/^\*# ---</d' | \
egrep -v '^[ 	]*\(cd .* clean\)|^[ 	]*\(cd [/\.a-zA-Z0-9]*[;] ' | \
egrep -v 'if \[ .* \] ; then ln -s .* fi' |\
egrep -v '^checking|^rm |^/bin/rm|^creating|^cleaning|^Creating|^cp ' | \
egrep -v '^[ 	]rm -rf |^[ 	]rm -f ' | \
egrep -v '^cc |^gcc |^Copying |^chmod |^cat |^U |^Making |^for file' | \
egrep -v '^[ 	]*cc|^[ 	] *gcc|^Configuring |^setting |^ar |^ranlib |^ *if \[' | \
egrep -v '^[ 	]*xlc |^[ 	] *xlC |^[ 	]*(pg)?f77|^[ 	]*(pg)?f9[05]|^[ 	]*g77 ' > .tmp 2>&1
if [ -n "$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
fi
#
/bin/rm -f .tmp2
egrep -v '^[ 	]*mpcc |^[ 	] *mpCC ' .tmp |
egrep -v '^[- 	a-zA-Z0-9/_\.]*mpicc |^making|^ *$|^[	 ]*ln -s|^ *done$' | \
egrep -v '^[- 	a-zA-Z0-9/_\.]*mpif77 |^[	 ]ar |^a - |^s - ' | \
egrep -v '^[- 	a-zA-Z0-9/_\.]*mpif90[ 	]|^[0-9-]*[ 	]*ar ' | \
egrep -v '^[- 	a-zA-Z0-9/_\.]*mpiCC |^building pro|^compiling ROMIO|^(gnu)?make ' | \
egrep -v '/X11R5/.*/Xlib.h:.*function declaration.*a prototype' | \
egrep -v '/X11/Xlib.h:.*function declaration.*a prototype' | \
egrep -v '/X11/Xlib.h:.*type defaults to' |\
egrep -v '/X11/Xutil.h:.*type defaults to' |\
egrep -v '^ar: creating|^ar: writing|^true |^Setting |^C integer' | \
egrep -v 'Warning .*declared EXTERNAL but never used' | \
egrep -v '/usr/local/gcc-2\.7\.2.*termios.h.* warning: .* redefined' >.tmp2 2>&1
#
/bin/rm -f .tmp
egrep -v '/usr/include/sys/ioctl.h.*this is the location' .tmp2 | \
egrep -v '^[ 	]*ranlib [- 	a-zA-Z0-9/_\.]*' | \
egrep -v '^[ 	]*true [- 	a-zA-Z0-9/_\.]*' | \
egrep -v '^Fortran linker needs|^Fortran externals ' | \
egrep -v '^About to|^Finished |^Generating message' |\
egrep -v '^[ 	]*mpir_[ig][a-z_]*:$' |\
egrep -v '^[a-z]*\.f:$' |\
egrep -v 'libsun\.a is not used for ' |\
egrep -v 'libpmpich\.a is not used for resolving' |\
egrep -v 'libmpich\.a is not used for resolving' > .tmp 2>&1
#
/bin/rm -f .tmp2
egrep -v '^[ 	]*done[ 	]*$' .tmp |\
egrep -v '^\+ /bin/rm|^[	 ]*/bin/rm |^[	 ]*for file|.*make clean\)' |\
egrep -v '^\+ cd |[ 	]*(gnu)?make[ ]*[a-zA-Z0-9\./]*[ 	]*$' |\
egrep -v 'WARNING 85: definition of' |\
egrep -v 'WARNING 134: weak definition of ' |\
egrep -v 'WARNING 84: .* is not used for resolving any symbol' |\
egrep -v '^Testing if |^Application can be linked|^Fortran application can' |\
egrep -v 'Attempting to|Adding a symbolic' |\
egrep -v '^[ 	]*\./makelinks|making adjlogs|not making server' > .tmp2 2>&1
#
/bin/rm -f .tmp
egrep -v 'cd lib;|cd alog;|cd usc' .tmp2 |\
egrep -v 'MPILIBNAME=.* p4inmpi' |\
egrep -v 'preparing mpif.h for ROMIO' |\
egrep -v '^[ 	]*-DMPIRUN_' |\
egrep -v '^[ 	]*if \[ "[01]" = "[01]" .* \] ; then' |\
egrep -v 'mpir_.*=== End of Compilation ? ===' |\
egrep -v 'Compilation successful' |\
egrep -v 'Target .* is up to date' |\
egrep -v 'for dir in contrib|contrib/test test|^[	 ]*if test -s' |\
egrep -v '^[ 	]*fi[ ]*;\\$' > .tmp 2>&1
#
/bin/rm -f .tmp2
egrep -v 'Configuration completed' .tmp |\
egrep -v '\(cd \./p4;|cd \.\./\.\./include' |\
egrep -v 'if test -n ".*" -a ".*" != "ignore"' |\
egrep -v '^[ 	]*-DMPICH_INFODLL|^[ 	]*msgqdllloc.c ; \\$' |\
egrep -v 'cd src/infoexport|^[ 	]*fi$|^[ 	]*else[ ]*\\$' |\
egrep -v 'mpe/profiling/nupshot has|use it on another|You must have java' |\
egrep -v 'cd contrib/test; make linktest|Application can use both' |\
egrep -v 'defining MPI_Offset|ROMIO home directory is|configuring for file' |\
egrep -v 'Compiling C\+\+|Include C\+\+ bindings' |\
egrep -v 'it should be defined in a file called ' |\
egrep -v 'When building multiple|include instead of bin/ and ' >.tmp2 2>&1
#
/bin/rm -f .tmp
egrep -v 'if test ".*" != "ignore"' .tmp2 |\
egrep -v 'export CC|util/makesharedlib -lib'  |\
egrep -v '^[ 	]*-kind=.* -local|^[ 	]*MAIN main:$|^pi3.f:$' |\
egrep -v 'updating cache ./config|config.h is unchanged' |\
egrep -v 'Testing that MPI applications' |\
egrep -v '^[ 	]*/bin/mv |may be used to remove the ' |\
egrep -v 'Created util/machines|You may want to edit|These hosts are used' |\
egrep -v '^[ 	]*if test "." = 1 ; then|Installing MPE'  |\
egrep -v '^[ 	]*if \[ ["!]|mpeconf.h is unchanged|^[ 	]*if \[ -n' |\
egrep -v '^[ 	]*mpif.h[ 	]*$' |\
egrep -v 'End of Compilation|Trying GETARG' |\
egrep -v '^[ 	]*(mp)?xlf(90)? ' |\
egrep -v '^[ 	]*[/a-zA-Z]*cc ' >.tmp 2>&1
#
/bin/rm -f .tmp2
egrep -v 'solaris/doctext|^[ ]*[a-zA-Z0-9/\._]*cc |^\*\* Testing|^\*\* C app' .tmp |\
egrep -v '^\*\* Fortran77 app|^[ 	]*touch ' |\
egrep -v '^\+ /bin/rm|^\+ rmdir|^\+ \[ -d' |\
egrep -v '^Trying to|^Checking |^updating cache|^Java is|^added manifest' |\
egrep -v '^[a-z]*conf.h is unchanged|^if test |^\+ make clean' |\
egrep -v '^[ 	]*make [a-zA-Z0-9/]|^[ 	]*cp |^[ 	]*cd ' |\
egrep -v '^[ 	]: [a-zA-Z0-9/]*\.a|^[ 	]*test -z' |\
egrep -v '^[ 	]*if \[ -x|^[ 	]*mv |^Linking:$' |\
egrep -v '^[a-z0-9A-Z_/]*mkdirs |^[a-z0-9A-Z_/]*CreateChoiceSrc |^Types are' |\
egrep -v '^[ 	]*mkdir |; make clean|^Installed ' |\
egrep -v '^adding: |^[ 	]*[-\.A-Za-z0-9/]*/javac |^Done configuring|^loading cache' |\
egrep -v '^[ 	]*[-\.A-Za-z0-9/]*/jar |^running /bin/sh|^configuring ' |\
cat    > .tmp2
rm -f .tmp

egrep -v '^.*CreateModuleSrc -exc|libm\.so is not used' .tmp2 |\
egrep -v '^.*CreateChoiceSrc -kind|^FORTRAN 77 Compiler.*Copyright' |\
egrep -v '^[/A-Za-z0-9]*\.f:[ 	]*$' |\
egrep -v '^[ 	]*mpi_constants\.o[ 	]*$' |\
egrep -v 'Nothing to be done for' > .tmp

rm -f .tmp2
mv .tmp .tmp2

if [ "$no_varwarn" = "yes" ] ; then
    sed -e '/^[	 ]*The variable .* is set but never used/,/^ *^/d' \
        -e '/^[	 ]*The variable .* was declared but never/,/^ *^/d' \
	.tmp2 > .tmp
else
    mv .tmp2 .tmp
fi
#
if [ "$no_tar" = "yes" ] ; then
    sed -e '/^\*# Buildtar/,/^Full distribution$/d' .tmp 
else
    cat .tmp
fi
#
# Try for ld messages
sed -n -e '/Unresolved:/,/^[ ]*$/p' $filename
/bin/rm -f .tmp .tmp2
