#!/bin/csh -f
#-----------------------------------------------------------------------------
# file:         runTests
#
# description:
#       Shell script to run HDFEOS5 test drivers.
#
# notes:
#       1) Any existing driver output files (except the samples) will be 
#          deleted.
#        
# usage:
#       runTests [LANGUAGE] [GROUP] (ex. runTests c f90 point grid)
#
#       If no parameters are passed, the default is to run every driver in
#       both 'C' and 'FORTRAN'.  The script will look at the 'F77' environment
#       variable to see whether to run FORTRAN77 or FORTRAN90.  If FORTRAN90
#       is found, it is up to the user to have the HDFEOS compiled properly
#       using FORTRAN90.
#
# author:
#       Abe Taaheri
#
# history:
#       07-Jul-2014 AT  Used TOOLKIT testdrivers originally written by
#                       Jason R. Watts / Hughes Applied Information Systems
#                       to create this and other scripts that this script runs
#----------------------------------------------------------------------------
 
# check for FORTRAN90

if ( `echo $F77 | grep f90 | wc -l` > 0 ) then
    set FORTRAN_LANG = ( f90 )
    set DIFF_LANG = ( f90 )
else if ( `echo $F77 | grep gfortran | wc -l` > 0 ) then
    set FORTRAN_LANG = ( f90 )
    set DIFF_LANG = ( f90 )
else if ( `echo $F77 | grep g77 | wc -l` > 0 ) then
    set FORTRAN_LANG = ( f)
    set DIFF_LANG = ( f77 )
else if ( `echo $F77 | grep g95 | wc -l` > 0 ) then
    set FORTRAN_LANG = ( f90 )
    set DIFF_LANG = ( f90 )
else
    set FORTRAN_LANG = ( f )
    set DIFF_LANG = ( f77 )
endif
 

# initialize variables

set LANGS = ( )
set SIMPLE_GRPS = ( )
set COMPLEX_GRPS = ( )


# initialize baselines

set SIMPLE_BASELINE = ( grid swath point za )
set COMPLEX_BASELINE = ( )


# check the number of arguments coming in
    
if ($#argv < 1) then

    set LANGS = ( c $FORTRAN_LANG )
    set SIMPLE_GRPS = ($SIMPLE_BASELINE)
    set COMPLEX_GRPS = ($COMPLEX_BASELINE)
else
    # setup the languages to be used 
    
    if ( `echo $argv | grep "c" | wc -l` == "1" ) then
        set LANGS = ( $LANGS " c " )
    endif
    
    if ( `echo $argv | egrep '(f|f77|f90)' | wc -l` == "1" ) then
        set LANGS = ( $LANGS $FORTRAN_LANG )
    endif
    
    if ( "$LANGS" == "" ) then
        set LANGS = ( c $FORTRAN_LANG )
    endif
    

    # setup simple groups according to input from command line ($argv)
    
    foreach GROUP ( ${SIMPLE_BASELINE} )
        if ( `echo $argv | grep $GROUP | wc -l` == "1" ) then
            set SIMPLE_GRPS = ( $SIMPLE_GRPS $GROUP )
        endif
    end
    
    
    # setup complex groups according to input from command line ($argv)
    
    foreach GROUP ( ${COMPLEX_BASELINE} )
        if ( `echo $argv | grep $GROUP | wc -l` == "1" ) then
            set COMPLEX_GRPS = ( $COMPLEX_GRPS $GROUP )
        endif
    end

    if ( "$SIMPLE_GRPS" == "" && "$COMPLEX_GRPS" == "" ) then
        set SIMPLE_GRPS = ($SIMPLE_BASELINE)
        set COMPLEX_GRPS = ($COMPLEX_BASELINE)
    endif
endif

echo ""; echo "The following languages will be used : ${LANGS}" 
echo ""; echo "The following tool groups will be processed :"
echo ""; echo $SIMPLE_GRPS $COMPLEX_GRPS |          \
              awk '{for(i=1;i<=NF;i++) print $i;}' | sort | \
              awk '{printf("%s ",$1);}' ; echo ""
echo ""; echo "If this isn't what you want, hit CTRL-C now."
sleep 5


# cleanup the previous test files (this only needs to be done
# for the SIMPLE groups, complex groups remove the files within
# their scripts)

if ( "$SIMPLE_GRPS" != "" ) then
    echo ""; echo "Cleaning up any previous test output files ..."
    $HDFEOS5_TST_SRC/Common/cleanup.csh $SIMPLE_GRPS >&! /dev/null
endif

echo ""; echo ""; echo ""; echo "Start running test drivers ..."; echo ""

# run the simple tool group drivers

foreach GROUP (${SIMPLE_GRPS})

    if (-d $HDFEOS5_TST_SRC/$GROUP) then

        # change to the group sub-directory

        cd $HDFEOS5_TST_SRC/$GROUP

        # run the drivers in that directory

        echo "";echo "Processing $GROUP drivers ..."
        ${HDFEOS5_TST_SRC}/Common/rundrivers.csh $LANGS >&! ${HDFEOS5_TST_SRC}/Common/${GROUP}.log
        echo "";echo "Results are in ${HDFEOS5_TST_SRC}/Common/${GROUP}.log ...";echo ""

    else

        echo "";echo "Unable to find directory for $GROUP ...";echo ""

    endif

end


# change to the 'Common' sub-directory

cd $HDFEOS5_TST_SRC/Common

# run the complex tool group drivers ( At this time we have no complex group!)

foreach GROUP (${COMPLEX_GRPS})

    echo ""; echo "Processing the ${GROUP} drivers ..."
    $HDFEOS5_TST_SRC/Common/run${GROUP}.csh $LANGS $ATTEPH_MODE >&! $HDFEOS5_TST_SRC/Common/${GROUP}.log
    echo ""; echo "Results are in ${HDFEOS5_TST_SRC}/Common/${GROUP}.log ...";echo ""

end


echo ""; echo "Processing log files ..."; echo ""
 
set LOG_ERROR_FILE = $HDFEOS5_TST_SRC/Common/log_file_errors
 
\rm -f $LOG_ERROR_FILE

# setup the strings we need to look for

set ERROR_STRINGS = ( error      \
                      warning    \
                      undefined  \
                      fatal      \
                      failed     \
                      core       \
                      exit       \
                    )


# these drivers 'get in the way' because the 'error' strings we are looking for
# are included in these drivers, therefore we will invert the grep for these strings

set DRIVER_STRINGS = ( \
                     )

# setup the grep strings 

set GREP_STR = `echo $ERROR_STRINGS | awk '{for(i=1;i<NF;i++) printf("%s|",$i); printf("%s",$i);}' `

set INV_GREP_STR = `echo $DRIVER_STRINGS | awk '{for(i=1;i<NF;i++) printf("%s|",$i); printf("%s",$i);}' `

# grep all the log files

# First setup a dummy log file so that there is 
# always more than one file (this is to make sure
# that the filename is output from the grep command).

echo "" >! $$.log

egrep -i -n '('$GREP_STR')' *.log | egrep -v '('$INV_GREP_STR')' >&! ${LOG_ERROR_FILE}.$$

\rm -f $$.log

# if we have something in the log file then format it with 'awk'

if ( ! -z ${LOG_ERROR_FILE}.$$ ) then
    awk -F: 'BEGIN {printf("  LOGFILE    LINE#     MESSAGE\n\n")}   \
                   {printf("%9s    %4s      ",$1,$2);               \
                    for(i=3;i<NF;i++) printf("%s:",$i);             \
                    printf("%s\n",$i);       }' ${LOG_ERROR_FILE}.$$ >&! ${LOG_ERROR_FILE}
    \rm -f ${LOG_ERROR_FILE}.$$
    echo "Problems were found in some of the log files, check the"
    echo "file:  $LOG_ERROR_FILE for the errors."; echo ""
else
    mv ${LOG_ERROR_FILE}.$$ ${LOG_ERROR_FILE}
    echo "No problems found with the log files ..."; echo ""
endif

 
echo "Processing difference files ..."; echo ""
 
set DIFF_ERROR_FILE = $HDFEOS5_TST_SRC/Common/diff_file_errors
 
\rm -f $DIFF_ERROR_FILE
 
# search for 'c' difference files that have
# a number of lines different than whats expected

foreach diff_file ( `find $HDFEOS5_TST_SRC -name "*_c.diff" -type f -print` )
    if ( `grep "Machine Name" $diff_file | wc -l` == 2 ) then
        @ EXP_LINES = 16
    else
        @ EXP_LINES = 12
    endif
 
    if ( `wc -l $diff_file | awk '{print $1}'` != $EXP_LINES ) then
        echo $diff_file >>! ${DIFF_ERROR_FILE}.$$
    endif
end
 
# search for FORTRAN difference files of non-zero length

foreach diff_file ( `find $HDFEOS5_TST_SRC -name "*_${DIFF_LANG}.diff" -type f -print` )
    if ( ! -z $diff_file ) then
        echo $diff_file >>! ${DIFF_ERROR_FILE}.$$
    endif
end
 

if ( -e ${DIFF_ERROR_FILE}.$$ ) then
    awk 'BEGIN {printf("#\!/bin/csh -f\n")} \
               {printf("more %s\n",$0);}'    ${DIFF_ERROR_FILE}.$$ | sort >&! $DIFF_ERROR_FILE
    echo "Problems were found in some of the difference files, check the"
    echo "file:  $DIFF_ERROR_FILE for the filenames."; echo ""
    \rm -f ${DIFF_ERROR_FILE}.$$
    chmod 755 $DIFF_ERROR_FILE
else
    echo "No problems found with the difference files ..."; echo ""
endif
