#!/bin/sh -e

pkg=r-cran-maxlik

# 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:' \
        -e '^ISBN 3-900051-07-0' \
        -e '^\[1\] "L*C' \
        -e '^During startup - Warning messages:' \
        -e '^[0-9]: Setting LC_' \
        -e "'help.start()' for an* HTML browser interface to help." \
       $1 | \
    sed -e '/^> *proc\.time()$/,$d' \
        -e 's#"Component 3:#"Component \\"gradient\\":#' \
        -e 's#"Component 4:#"Component \\"hessian\\":#' \
        -e 's#"Component 8:#"Component \\"fixed\\":#'
}

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 | grep -v tinytest.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 --ignore-all-space ${htest}.Rout.save_ ${htest}.Rout_
   if [ ! $? ] ; then
     echo "Test ${htest} failed"
     exit 1
   else
     echo "Test ${htest} passed"
   fi
done

LC_ALL=C R --no-save < tinytest.R

rm -f $AUTOPKGTEST_TMP/*

exit 0
