#!/bin/sh -e

pkg=r-cran-lsmeans

# 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-900051-07-0' \
        -e '^\[1\] "L*C' \
        -e '^During startup - Warning messages:' \
        -e '^R Under development' \
        -e '^[0-9]: Setting LC_' \
       $1 | \
    sed '/^> *proc\.time()$/,$d'
}

if [ "$ADTTMP" = "" ] ; then
  ADTTMP=`mktemp -d /tmp/${pkg}-test.XXXXXX`
  trap "rm -rf $ADTTMP" 0 INT QUIT ABRT PIPE TERM
fi
cd $ADTTMP
cp /usr/share/doc/${pkg}/tests/* $ADTTMP
find . -name "*.gz" -exec gunzip \{\} \;
find . -name "*.Rout.save" -exec dos2unix --quiet \{\} \; 
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
