#! /bin/bash

set -e

# I use a "tee" here so that errors qre written both immediately (to see progress)
# and on a file (for later report)

cppcheck --std=c++03 --std=posix -j 4 --platform=unix64 \
  --template='[{file}:{line}] ({severity}) :{id}: {message}' --enable=all --inline-suppr --force \
  */*.{h,cpp} 2> >(tee cppcheck.log >&2)

## this part could be useful to clean the code
# echo "+++++++ THIS IS A COMPLETE LIST OF ERRORS +++++++"
# cat cppcheck.log
# echo "+++++++++++++++++++++++++++++++++++++"
# echo "summary from cppcheck report:
# for t in error warning performance portability style
# do
#   echo "$t: $(cat cppcheck.log | grep "($t)" | wc -l)"
# done
# echo "+++++++++++++++++++++++++++++++++++++"


## I exclude src/molfile which is full of warnings
## I also exclude (style) messages
## perhaps we could keep some of them to make to code cleaner at some point
##
## (the "true" command is necessary so that the script does not fail if some string is not found)
grep -v "(style)" cppcheck.log | grep -v "\[molfile/" | grep -v "\[lapack/" | grep -v "\[blas/"> cppcheck.fatal || true
count=$(cat cppcheck.fatal | wc -l)
echo 
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
if ((count==0))
then
  echo "      cppcheck did not find any fatal error"
  echo "      (fatal here means: excluding style and excluding molfile/)"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  exit 0
fi

echo "      cppcheck reported the following fatal errors:"
echo "      (fatal here means: excluding style and excluding molfile/)"
echo
cat cppcheck.fatal
echo
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo

exit 1

