#!/bin/bash

nerror=0
nok=0
notapp=0
html=
fail=

for arg
do
case $arg in
  (--html) html=1 ;;
  (--fail) fail=1 ;;
esac
done

echo
echo
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "+ Test session ended at $(date)"

dirs=$(
  for mod in `./scripts/find_modules.sh` ; do 
    for file in $mod/rt*/Makefile
    do
      echo ${file%Makefile}
    done
  done
  for file in basic/rt*/Makefile
  do
    echo ${file%Makefile}
  done
)


for dir in $dirs
do
  file=${dir}report.txt
  fullname="$file"
  if [ -n "$html" ] ; then
    fullname='<a href="'"$file"'">'"$file"'</a>'
  fi
  if grep -q NOT_APPLIABLE $file ; then
    ((notapp++))
    echo + test $dir NOT APPLIABLE
    echo + check file $fullname for more information
  elif grep -q FAILURE $file ; then
    ((nerror++))
    echo + ERROR in test $dir
    echo + check file $fullname for more information
  else
    ((nok++))
  fi
done

echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "+ Final report:"
echo "+ $((nok+nerror)) tests performed, $notapp tests not appliable"
echo "+ $nerror errors found"
if ((nerror==0)) ; then
echo "+ Well done!!"
else
echo "+ Find the bug!"
echo "+ To replace references, go to the test directory and"
echo "+ type 'make reset'"
if [ -n "$fail" ] ; then
  exit 1
fi
fi
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
echo

