#!/bin/sh -e

export LC_ALL=C.UTF-8

# R DESCRIPTION files use the debian control file format
# (see R package manual 1.1.1)
pkgname=$(grep-dctrl -s Package -n '' DESCRIPTION)
debname=$(grep-dctrl -s Source -n '' debian/control)

# Test 1: Try to load the R library
echo "BEGIN Test 1: load $pkgname into R"
R --no-save -e "library('$pkgname')"

###
echo "Other tests are currently unsupported!"
echo "They will be progressively added."
exit 0
###

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
  AUTOPKGTEST_TMP=`mktemp -d /tmp/${debname}-test.XXXXXX`
  trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi

cp -R * $AUTOPKGTEST_TMP
cd $AUTOPKGTEST_TMP

# Test 2: Test testthat workflow
if [ -f "tests/testthat.R" ]; then
  echo "BEGIN TEST testthat.R"
  cd tests
  R --no-save < testthat.R
  cd ..
fi


# TODO
# Test 3: Test RUnit workflow
# Test 4: Test non-standard workflow
# Test 4: Test Bioconductor package
# Test 5: Test to build vignettes
