#!/bin/sh -e

pkg=r-cran-fields

# 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 '^R : Copyright 20' \
        -e '^R Under development (unstable)' \
        -e '^Version 2.0' \
        -e '^Platform:' \
        -e '^Spam version .* is loaded.' \
        -e '^Loading required package:' \
        $1 | \
    sed -e '/^> *proc\.time()$/,$d'
}

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

exit 0
