#!/bin/sh

dir=`dirname "$0"`

cd "${AUTOPKGTEST_TMP}"

cleanup() {
  ex=$?
  rm -rf lib1305build rsync.log build.log
  exit "${ex}"
}
trap "cleanup" EXIT TERM INT

# test if valgrind package exists
echo 'test if valgrind package exists ... START'
if which valgrind >/dev/null; then
  echo 'test if valgrind package exists ... OK'
else
  echo 'test if valgrind package exists ... FAILED, skipping tests'
  exit 77
fi

# copy source directory to lib1305build/
echo 'copying source directory to lib1305build/ ... START'
if rsync -a --exclude=debian "${dir}/../../" "lib1305build/" >rsync.log 2>&1; then
  echo 'copying source directory to lib1305build/ ... OK'
else
  cat rsync.log
  echo 'copying source directory to lib1305build/ ... FAILED'
  exit 1
fi

# build lib1305-fulltest
echo 'build lib1305-fulltest ... START'
(
  cd lib1305build || exit 1
  ./configure || exit 1
  make || exit 1
) > build.log 2>&1

ex=$?
if [ "${ex}" -eq 0 ]; then
  echo 'build lib1305-fulltest ... OK'
else
  cat build.log
  echo 'build lib1305-fulltest ... FAILED'
fi

# run lib1305-fulltest
echo 'run lib1305-fulltest ... START'
env PATH="${PATH}:./lib1305build/build/0/package/bin" lib1305-fulltest

ex=$?
if [ "${ex}" -eq 0 ]; then
  echo 'run lib1305-fulltest ... OK'
else
  echo 'run lib1305-fulltest ... FAILED'
fi
