#!/bin/sh -e

pkgname=unitizer
debname=r-cran-unitizer

filter() {
    grep -v -e '^R version' \
        -e '^Copyright (C)' \
        -e '^R : Copyright 20' \
        -e '^R Under development' \
        -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/${debname}-test.XXXXXX`
#    trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi
cd $AUTOPKGTEST_TMP
cp -a /usr/share/doc/$debname/tests $AUTOPKGTEST_TMP
gunzip -r *
cd tests

for htest in `ls *.R | sed 's/\.R$//'` ; do
   echo "Begin testing ${htest}.R"
   LC_ALL=C R --no-save < ${htest}.R 2>&1 | tee -a ${htest}.Rout
   # Relative path is different for both
   if [ "${htest}" = "t-nav" ]; then
	   sed -i 's/_helper\/unitizers\///g' ${htest}.Rout.save
   fi
   if [ -e ${htest}.Rout.save ]; then
	filter ${htest}.Rout.save > ${htest}.Rout.save_
	filter ${htest}.Rout > ${htest}.Rout_
	# Do not compare the base envs of these two files
	if [ "${htest}" = "t-repairenvs" ]; then
		sed -i '/environment/d' ${htest}.Rout_
		sed -i '/environment/d' ${htest}.Rout.save_
	fi
	diff -u --ignore-all-space ${htest}.Rout.save_ ${htest}.Rout_
   fi
   echo "PASS"
done
