#!/bin/sh

set +e

dirname=`dirname $0`
retval=0

case $dirname in
	/*)
		cd $dirname/../.. > /dev/null
		path=`pwd`
		cd - > /dev/null
	;;
	*)
		cd `pwd`/$dirname/../.. > /dev/null
		path=`pwd`
		cd - > /dev/null
	;;
esac

export PYTHONPATH=$path/lib

cd $path/etc/exabgp
names=`ls *.conf`
cd -

echo $names

export exabgp_tcp_bind=''
export exabgp_debug_selfcheck=true

for conf in $names
do
	printf "%-50s " $conf
	result=`$path/sbin/exabgp $path/etc/exabgp/$conf 2>&1`
	retcode=$?
	problem=`echo $result | grep 'Problem with the configuration file' || true`

	if [ $retcode -eq 0 ] && [ "$problem" = "" ]
	then
		printf "ok\n"
	else
		printf "failed\n"
		printf "\n"
		printf "env exabgp.debug.configuration=true exabgp.tcp.bind='' exabgp.debug.selfcheck=true $path/sbin/exabgp -d -p $path/etc/exabgp/$conf 2>&1"
		printf "\n\n"
		printf "$result"
		printf "\n\n"
		retval=1
	fi
done

exit $retval
