#!/bin/csh -f
#-----------------------------------------------------------------------------
# file:         runTests
#
# description:
#       Shell script to run the Toolkit test drivers.
#
# notes:
#       1) Any existing driver output files (except the samples) will be 
#          deleted.
#        
# usage:
#       runTests [LANGUAGE] [GROUP] (ex. runTests c f90 test_TIME test_MET)
#
#       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 Toolkit compiled properly
#       using FORTRAN90.
#
# author:
#       Jason R. Watts / Hughes Applied Information Systems
#
# history:
#        3-Feb-1995 JRW Initial version
#       23-Feb-1995 JRW Added CUC, GCT and L0 tool groups
#       15-Mar-1995 JRW Removed AA from 'simple groups' and added to new
#                       'COMPLEX_GRPS' variable.  Added loop to go through
#                       the complex groups and run the appropriate script.
#                       Added check to be sure that the utilities 'orbsim'
#                       and 'L0sim' exist.  Put in line to create the 
#                       spacecraft ephemeris files.
#       23-Mar-1995 JRW Added code to analyze arguments being passed in and
#                       setup 'X_GRPS' variables accordingly.
#        3-Apr-1995 JRW Added code to 'grep' through log files and consolidate
#                       errors into one file (LOG_ERROR_FILE).
#       25-Apr-1995 JRW Changed the CUC tools to a COMPLEX group, since the
#                       PGS_PC_INFO_FILE variable needs to be adjusted.
#        9-May-1995 JRW Added code to search through difference files and 
#                       report which ones deviated from the expected output.
#       12-Jun-1995 JRW Changed the CUC tools back to a simple group since 
#                       they can just use the default PCF.
#       24-Apr-1996 JRW Changed processing of CBP, CSC and EPH tool groups.
#                       They moved from the simple tool group to the complex
#                       group because they can no longer use the default PCF.
#                       These groups now need a custom PCF because of the 
#                       new way logical id 10501 (and new 10502) are stored
#                       in the PCF.  Before (TK5), this entry was just the
#                       directory where all the *.eph files were stored, now
#                       it is for a specific file.  Therefore, the baseline
#                       PCF has many entries for logical id 10501 & 10502, 
#                       each using the version number to represent each of
#                       the different files.
#       26-Apr-1996 LVM Changed processing of TD tool group.  It moved from
#                       the simple tool group to the complex group because
#                       it can no longer use the default PCF. 
#       30-Apr-1996 JRW Changed MEM tool processing, now allows FORTRAN.
#       02-Apr-1997 DGH Added DEM (new tool) to the COMPLEX_BASELINE list 
#       12-Oct-2000 Abe Taaheri Modified for TOOLKIT_MTD
#----------------------------------------------------------------------------
 
# check for FORTRAN90

if ( `echo $F77 | grep f90 | 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 COMPLEX_BASELINE = ( test_MET test_TIME )


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

    set LANGS = ( c $FORTRAN_LANG )
    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 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 ( "$COMPLEX_GRPS" == "" ) then
        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 $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


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

# change to the 'Common' sub-directory

cd $PGSTK_TST_SRC/Common

# run the complex tool group drivers

foreach GROUP (${COMPLEX_GRPS})

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

end


echo ""; echo "Processing log files ..."; echo ""
 
set LOG_ERROR_FILE = $PGSTK_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'll invert the grep for these strings

set DRIVER_STRINGS = ( PGS_SMF_TestErrorLevel_Driver    \
                       PGS_SMF_TestWarningLevel_Driver  \
                       PGS_SMF_TestFatalLevel_Driver    \
                     )


# 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 = $PGSTK_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 $PGSTK_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 $PGSTK_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
