#!/bin/sh -e

pkg=r-cran-markdown

# 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 '^Version 2.0' \
        -e '^Platform:' \
       $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
cp /usr/share/doc/r-cran-markdown/run-with-locales .
if [ "`find . -name "*.gz"`" != "" ] ; then gunzip *.gz ; fi

chmod +x run-with-locales

# weaker test for R files that have no saved results
for htest in smartypants.R empty.R ; do
   ./run-with-locales --generate en_US.utf8 -- env \
   LC_ALL=en_US.utf8 R --no-save < ${htest}
   rm ${htest} # remove here to skip the comparison with saved result
done
for htest in `ls *.R | sed 's/\.R$//'` ; do
   ./run-with-locales --generate en_US.utf8 -- env \
   LC_ALL=en_US.utf8 R --no-save < ${htest}.R | 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
