#! /bin/sh
# $Id: //depot/offshore/Riflex/riflex_3.6/test/run_all#7 $
#
# This is a script for running the RIFELX tests below
#
#    man_stamod/detail_maint_test_stamod.run
#    man_dynmod/detail_maint_test_dynmod.run
#    fremod/run_all
#    mpf_and_stress/run_all
#    slowdrift/run_all
#
# Calls: run_tests: on the directory ../tools or in PATH
#
#   Org. version : Elizabeth Passano 00-08-17
#   Last modified: 
#   2000-08-30 / EP  - Do not rely on $PATH including 
#                      curret directoty
#   2008-03-11 / EP  - Uncomment fremod tests
#   2008-03-13 / EP  - Add vivana tests and check for error
#   2008-11-11 / EP  - Add aliases for release and debug versions 
#   2009-05-05 / EP  - Add comparison to old results
#   2009-05-05 / EP  - Add path for run_tests and improved error handling
#   2009-05-06 / EP  - Add missing quote (syntax error) and add *** to
#                      final summery
#   2009-05-20 / EP  - Use run_tests in path if not found in tools
#   2011-09-01 / EP  - Add -c option
#

if [ $# -lt 1 ]
then
   echo "Usage:  `basename $0` runscript"
   echo "        `basename $0` R[elease] run with ../Release/bin/vriflex"
   echo "        `basename $0` D[ebug]   run with   ../Debug/bin/vriflex"
   echo "                  -nr to skip running test"
   echo "                  -nc to skip result comparison"
   echo "                  -c  <cmpdir> to compare with cmpdir"
   exit 1
fi


# Init
istat=0
istat_err=0
istat_diff=0

lisfile=`basename $0`.lis
rm -f $lisfile
touch $lisfile


# Decode options
lrun=1
lcmp=1
cmpdir=""

while [ -n "`echo $1 | grep -e '^-' -e ' -' `" ]
do

   if   [ "$1" = "-nc" ]
   then
      lcmp=0
      shift 1
   
   elif [ "$1" = "-nr" ]
   then
      lrun=0
      shift 1
   
   elif   [ "$1" = "-c" ]
   then
      lcmp=1
      cmpdir="$2"
      shift 2
   
   elif [ -n "`echo $1 | grep -e '^-' -e ' -' `" ]
   then
      echo "Illegal option $1 found!" >&2
      exit 2

   fi
done


# Check options
if   [ $lrun -eq 1 -a $lcmp -eq 1 ]
then
   text="Run and compare"
elif [ $lrun -eq 1 ]
then
   text="Run"
elif [ $lcmp -eq 1 ]
then
   text="Compare"
else
   text="List"
fi


# Check specified riflex script
riflex=$1
if   [ -n "`echo 'Release' | grep "^$riflex"`" ] 
then
   dir=`pwd`
   riflex="`dirname $dir`/Release/bin/vriflex"
elif [ -n "`echo 'Debug' | grep "^$riflex"`" ] 
then
   dir=`pwd`
   riflex="`dirname $dir`/Debug/bin/vriflex"
fi

if   [ ! -f $riflex -o ! -x $riflex ]
then
   echo "Script $riflex not found"
   exit 2
fi


# Get bindir and find script run_tests
if [ $lcmp -eq 1 ]
then
   bindir="`dirname $riflex`"
   if [ ! -d "$bindir" ]
   then
      echo "bindir $bindir not found!"
      exit 2
   else
      echo "bindir is $bindir"
   fi

   if [ -x ../tools/run_tests ]
   then
      runtest="../../tools/run_tests"
   else
      run_tests >/dev/null 2>&1
      if [ $? -eq 1 ]
      then
         runtest="run_tests"
      else
         echo    "The script run_tests is not found"
         echo    "in directory ../tools or in PATH"
         exit 2
      fi
   fi
fi


echo "Starting tests using script $riflex to run riflex"
echo


# Run all test sets
for dir in man_stamod man_dynmod fremod mpf_and_stress slowdrift fishfarm vivana
do
   echo "*** $dir ***"
   cd $dir

   if [ $lrun -eq 1 ]
   then
      if   [ $dir = "man_stamod" ]
      then
         runfile="./detail_maint_test_stamod.run"
         outfile="./detail_maint_test_stamod.out"
      elif [ $dir = "man_dynmod" ]
      then
         runfile="./detail_maint_test_dynmod.run"
         outfile="./detail_maint_test_dynmod.out"
      else
         runfile="./run_all"
         outfile="./run_all.out"
      fi

      $runfile $riflex

      if [ $? -ne 0 ]
      then
         istat_err=1
         echo "*** $dir FAILED ***"
         echo

      else
   	 output="`grep -i error $outfile | \
                  grep -v 'No error messages found on file'`"
   	 if [ -n "$output" ]
   	 then
   	    istat_err=1
   	    echo "*** $dir FAILED ***"
   	    echo "Errors found:"
   	    echo "$output"
            echo
   	 fi
      fi

   fi

   grep -i error *res | grep -v -e 'Error limit' -e 'EIGENVALUES'


   if [ $lcmp -eq 1 ]
   then

      if [ -z "$cmpdir" ]
      then
         cmpflags=""
      else
         cmpflags="-c $cmpdir/$dir"
      fi

      $runtest -nr $cmpflags $bindir

      if [ $? -ne 0 ]
      then
         istat_diff=1
      fi

      if [ -s run_tests.lis ]
      then
         sed -e s%^%${dir}/% run_tests.lis | tr -d '\r' >>../$lisfile
      fi

      echo
      echo
   fi

   cd ..
done


echo 
echo "$text of tests using script $riflex completed"


# Update status
echo

if   [ $istat_err -ne 0 ]
then
    echo "*** Run errors occurred ***"
    istat=9

elif [ $istat_diff -ne 0 -a ! -s $lisfile ]
then
    echo "*** Comparison errors occurred ***"
    istat=3

elif [ $istat_diff -ne 0 ]
then
   echo "*** `cat $lisfile | wc -l` result files differ ***"
   cat $lisfile
   istat=3
else
   echo "*** All tests OK ***"
fi


# Exit
exit $istat