# #-- pylib.test --#
# source the master var file when it's there
[ -f ../.tpkg.var.master ] && source ../.tpkg.var.master
# use .tpkg.var.test for in test variable passing
[ -f .tpkg.var.test ] && source .tpkg.var.test

PRE="../.."
if grep "define WITH_PYUNBOUND 1" $PRE/config.h; then
	echo "have pyunbound"
else
	echo "no pyunbound"
	exit 0
fi

if test "`uname 2>&1`" = "Darwin"; then
	echo export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:../../.libs"
	export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:../../.libs"
fi
#echo export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:../../.libs:."
#export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:../../.libs:."

if grep "PY_MAJOR_VERSION=3" $PRE/Makefile; then
	PYTHON="python3"; else PYTHON="python2"; fi
if test ! -x `which $PYTHON` 2>&1; then PYTHON="python"; fi

# do the test
echo "> pylib.lookup.py www.example.com."
$PYTHON pylib.lookup.py www.example.com. | tee outfile

echo "> cat logfiles"
cat fwd.log
echo "> check answer"
if grep "Resolve: \[.\?10.20.30.40.\?\]" outfile; then
	:
else
	echo "Not OK (resolve)"
	exit 1
fi
if grep "Async resolve: \[.\?10.20.30.40.\?\]" outfile; then
	:
else
	echo "Not OK (async resolve)"
	exit 1
fi
if grep "Ratelimit-fg-on: pass" outfile; then
	:
else
	echo "Not OK (ratelimit-fg-on)"
	exit 1
fi
if grep "Ratelimit-fg-off: \[.\?10.20.30.40.\?\]" outfile; then
	:
else
	echo "Not OK (ratelimit-fg-off)"
	exit 1
fi
if grep "Ratelimit-bg-on: pass" outfile; then
	:
else
	echo "Not OK (ratelimit-bg-on)"
	exit 1
fi
if grep "Ratelimit-bg-off: \[.\?10.20.30.40.\?\]" outfile; then
	:
else
	echo "Not OK (ratelimit-bg-off)"
	exit 1
fi

echo "OK"

exit 0
