#!/bin/sh
# file: libcheck		G. Moody	8 September 2001
#                               Last revised:  12 September 2001
#
# This script checks the functionality of the WFDB library by comparing the
# outputs of 'lcheck' with expected outputs.  See 'lcheck.c' for details of
# the tests performed, or examine the output of 'lcheck -v'.  Note that
# 'lcheck' must be run within this directory to ensure that its local input
# files (in ../data) are available.
#
# Suggestions for additional checks are welcome;  please send them to the
# author (george@mit.edu).

if [ \! -s ../data/100s.hea ]
then
  echo "Run this program from the 'checkpkg' directory of the WFDB sources."
  echo "The 'data' directory, including record 100s, must also be present."
  exit
fi

test -s Makefile || ( cd ..; ./configure )
test -s lcheck || make lcheck
./lcheck -v >lcheck.log
cp ../data/100s.atr expected/100s.chk
cp ../data/100s.dat expected/100z.dat
CF="lcheck.log 100s.chk 100z.dat 100z.hea"
if grep -q "WFDB supports NETFILES" lcheck.log
then
  CF="$CF udb/100s.chk udb/100z.dat udb/100z.hea"
  cp ../data/100s.atr expected/udb/100s.chk
  cp ../data/100s.dat expected/udb/100z.dat
  cp expected/lcheck.log-NETFILES expected/lcheck.log
else
  cp expected/lcheck.log-no-NETFILES expected/lcheck.log
fi

for i in $CF
do
    case $i in
      *.hea) diff -q $i expected/$i &&
	       ( echo " Files $i and expected/$i match: test succeeded"
		 rm -f $i ) ;;
      *) diff -q $i expected/$i &&
	       ( echo " Files $i and expected/$i match: test succeeded"
		 rm -f $i expected/$i ) ;;
    esac
done

# rmdir will fail if 'udb' is not empty.  This is a *feature*, not a bug --
# don't fix it!
test -d udb && rmdir udb
