#!/bin/sh
# This test uses the compiler with the standard Debian CFLAGS and LDFLAGS
# and fails if valgrind is not available.

set -e

if ! which valgrind >/dev/null; then
    echo 'no valgrind avalable' >&2
    exit 1
fi

dir=`dirname "$0"`

cd "${AUTOPKGTEST_TMP}"

cleanup() {
  ex=$?
  rm -f threaded
  exit "${ex}"
}
trap "cleanup" EXIT TERM INT

CC=cc
CFLAGS=`dpkg-buildflags --get CFLAGS`
CFLAGS="${CFLAGS} `dpkg-buildflags --get CPPFLAGS`"
CFLAGS="${CFLAGS} -Wall -O3"
LDFLAGS=`dpkg-buildflags --get LDFLAGS`
LDFLAGS="${LDFLAGS} -pthread"

${CC} ${CFLAGS} -o threaded "${dir}/threaded.c" ${LDFLAGS}

ret=0
for TOOL in memcheck drd helgrind; do
  echo "checking tool: ${TOOL}"
    if valgrind --tool="${TOOL}" ./threaded; then
      echo "----------- ${TOOL} ... OK -----------"
    else
      echo "----------- ${TOOL} ... FAILED -----------"
      ret=1
    fi
done

exit "${ret}"
