#!/bin/sh 
CLAMD_WRAPPER=${CLAMD_WRAPPER-}
killclamd() {
	test -f /tmp/clamd-test.pid || return
	pid=`cat /tmp/clamd-test.pid 2>/dev/null`
	if test "X$pid" = "X"; then
		# file can be removed between the 'test' and 'cat',
		# it happened a few times for me
		return
	fi
	kill -0 $pid 2>/dev/null || return
	kill $pid
	timeout=10
	(sleep $timeout && kill -0 $pid 2>/dev/null && kill -KILL $pid) &
	sleeperpid=$!
	kill -0 $pid 2>/dev/null && wait $pid
	kill $sleeperpid 2>/dev/null
	rm -f /tmp/clamd-test.pid
}
die() {
	killclamd
	rm -rf test-db test-clamd-viraction.conf test-clamd.log	test-clamd-heur-pred.conf clamd-test.socket
	exit $1
}
run_clamd_test() {
	conf_file=$1
	shift
	rm -f clamdscan.log clamdscan-multiscan.log
	../libtool --mode=execute $CLAMD_WRAPPER ../clamd/clamd -c $conf_file || { echo "Failed to start clamd!" >&2; die 1;}
	../clamdscan/clamdscan --version --config-file $conf_file 2>&1|grep "^ClamAV" >/dev/null || { echo "clamdscan can't get version of clamd!" >&2; die 2;}
	../clamdscan/clamdscan --quiet --config-file $conf_file $* --log=clamdscan.log
	../clamdscan/clamdscan --quiet --config-file $conf_file $* -m --log=clamdscan-multiscan.log
	if test -x /bin/nc; then
		echo RELOAD | nc -q 0 -n 127.0.0.1 3311
	fi
	if test $? = 2; then 
		echo "Failed to run clamdscan!" >&2;
		die 3;	
	fi
	killclamd
}

run_clamd_fdpass_test() {
	conf_file=$1
	shift
	rm -f clamdscan.log
	../libtool --mode=execute $CLAMD_WRAPPER ../clamd/clamd -c $conf_file || { echo "Failed to start clamd!" >&2; die 1;}
	../clamdscan/clamdscan --quiet --fdpass --config-file $conf_file - <$1 --log=clamdscan.log
	if test $? = 2; then
		echo "Failed to run clamdscan!" >&2;
		die 3;
	fi
	killclamd
}

mkdir -p test-db
cat <<EOF >test-db/test.hdb
aa15bcf478d165efd2065190eb473bcb:544:ClamAV-Test-File
EOF
cp $srcdir/input/daily.ftm test-db/
cp $srcdir/input/daily.pdb test-db/

# Test that all testfiles are detected
FILES=../test/clam*
run_clamd_test $srcdir/test-clamd.conf $FILES
NFILES=`ls -1 $FILES | wc -l`
NINFECTED=`grep "Infected files" clamdscan.log | cut -f2 -d:|sed -e 's/ //g'`
NINFECTED_MULTI=`grep "Infected files" clamdscan-multiscan.log | cut -f2 -d:|sed -e 's/ //g'`
if test "$NFILES" -ne "0$NINFECTED"; then
	echo "clamd did not detect all testfiles correctly!" >&2;
	grep OK clamdscan.log >&2;
	die 4;
fi
if test "$NFILES" -ne "0$NINFECTED_MULTI"; then
	echo "clamd did not detect all testfiles correctly in multiscan mode!" >&2;
	grep OK clamdscan-multiscan.log >&2;
	die 4;
fi

# Test VirusEvent feature
cat <$srcdir/test-clamd.conf >test-clamd-viraction.conf
echo "VirusEvent `pwd`/$srcdir/virusaction-test.sh `pwd` \"Virus found: %v\"" >>test-clamd-viraction.conf
rm -f test-clamd.log
run_clamd_test test-clamd-viraction.conf ../test/clam.exe
grep "Virus found: ClamAV-Test-File.UNOFFICIAL" test-clamd.log >/dev/null 2>/dev/null; 
if test ! $? ; then
	echo "Virusaction test failed!" 
	cat test-clamd.log
	die 5;
fi

# Test HeuristicScanPrecedence feature
cat <$srcdir/test-clamd.conf >test-clamd-heur-pred.conf
run_clamd_test test-clamd-heur-pred.conf clam-phish-exe
grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null;
if test ! $?; then
	echo "HeuristicScanPrecedence off test failed!" >&2;
	cat clamdscan.log;
	die 6;
fi
echo "HeuristicScanPrecedence yes" >>test-clamd-heur-pred.conf
run_clamd_test test-clamd-heur-pred.conf clam-phish-exe
grep "Phishing.Heuristics.Email.SpoofedDomain" clamdscan.log >/dev/null 2>/dev/null;
if test ! $?; then
	echo "HeuristicScanPrecedence on test failed!" >&2;
	cat clamdscan.log;
	die 6;
fi

if grep "^#define HAVE_FD_PASSING 1" ../clamav-config.h >/dev/null; then
	run_clamd_fdpass_test $srcdir/test-clamd.conf ../test/clam.exe
	grep "ClamAV-Test-File" clamdscan.log >/dev/null 2>/dev/null;
	if test ! $?; then
		echo "FDpassing test failed!" >&2;
		cat clamdscan.log;
		die 7;
	fi
else
	echo "No FD passing support, skipping test"
fi
die 0;