#! /bin/bash

# (c) Siemens Schweiz AG, vertraulich
# $Id: checklatest.sh 323 2007-02-23 15:32:45Z chawama0 $
#
#        1          2         3         4         5         6         7         8
# 3456789012345676890123456789012345678901234567890123456789012345678901234567890

printUsage() {
    echo "Usage: $0 <program-to-check>"
    echo ""
    echo "This script checks a program using valgrind."
    return 0
}

if [ $# -ne 1 -o "$1" = "-h" -o "$1" = "--help" ] ; then
    printUsage
    exit 1
fi

if ! valgrind \
  --show-reachable=yes \
  --leak-check=full \
  --gen-suppressions=all \
  --log-file-exactly=valgrind.log \
  --suppressions=$(dirname $0)/suppressions.valgrind \
  $1; then
  echo "******** Valcheck: Testfall fehlgeschlagen! (normaler Fehler)"
  exit 1
fi

if ! ( ( grep 'ERROR SUMMARY: 0 errors from 0 contexts' valgrind.log \
         && \
         ( grep 'definitely lost: 0 bytes in 0 blocks' valgrind.log \
           && \
           grep 'possibly lost: 0 bytes in 0 blocks' valgrind.log \
           && \
           grep 'still reachable: 0 bytes in 0 blocks' valgrind.log \
           ||
           grep 'All heap blocks were freed -- no leaks are possible' \
                valgrind.log
         )
     ) 2>&1 > /dev/null ); then
  mv valgrind.log valgrind-$(basename $1).error
  echo "******** Valcheck: Speicherfehler! Siehe valgrind-$(basename $1).error"
  exit 1
fi

rm valgrind.log
