#!/bin/sh -e

pkg=r-cran-etm

# The saved result files do contain some differences in metadata and we also
# need to ignore version differences of R
filter() {
    grep -v -e '^R version' \
        -e '^Copyright (C)' \
        -e '^Platform: ' \
        -e '^ISBN 3' \
        -e '^Loading required package: lattice' \
        -e '^Loading required package: splines' \
        -e '^183  1\.0000 183\.0 [0-9.e-]* 1\.00000 1\.0000      1       1' \
        -e '^R Under development' \
       $1 | \
    sed -e '/^> *proc\.time()$/,$d' \
        -e '/^  Natural language support but running in an English locale/,+1d'
}

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
  AUTOPKGTEST_TMP=`mktemp -d /tmp/${pkg}-test.XXXXXX`
fi
cd $AUTOPKGTEST_TMP
cp /usr/share/doc/${pkg}/tests/* $AUTOPKGTEST_TMP
find . -name "*.gz" -exec gunzip \{\} \;
for htest in `ls *.R | sed 's/\.R$//'` ; do
   LC_ALL=C R --no-save < ${htest}.R 2>&1 | tee > ${htest}.Rout
   filter ${htest}.Rout.save > ${htest}.Rout.save_
   filter ${htest}.Rout > ${htest}.Rout_
   diff -u ${htest}.Rout.save_ ${htest}.Rout_
   if [ ! $? ] ; then
     echo "Test ${htest} failed"
     exit 1
   else
     echo "Test ${htest} passed"
   fi
done
rm -f $AUTOPKGTEST_TMP/*

exit 0
