#!/bin/sh

dirname=`dirname $0`

if [ `echo $dirname | grep "^/" -c` -eq 1 ]; then
	echo $path
else
	cd `pwd`/$dirname/.. > /dev/null
	path=`pwd`
	cd - > /dev/null
fi

export PYTHONPATH=$path/lib:/usr/share/exabgp/lib/2.0.7
export ETC=$path/etc/exabgp

if [ "$INTERPRETER" != "" ]
then
	INTERPRETER=`which $INTERPRETER`
fi

PYPY=`which pypy`
PYTHON27=`which python2.7`
PYTHON26=`which python2.6`
PYTHON25=`which python2.5`
PYTHON24=`which python2.4`
PYTHON2=`which python2`
PYTHON=`which python`

if [ -f "$PYPY" ]
then
	INTERPRETER=$PYPY
elif [ -f "$PYTHON27" ]
then
	INTERPRETER=$PYTHON27
elif [ -f "$PYTHON26" ]
then
	INTERPRETER=$PYTHON26
elif [ -f "$PYTHON25" ]
then
	INTERPRETER=$PYTHON25
elif [ -f "$PYTHON24" ]
then
	INTERPRETER=$PYTHON24
elif [ -f "$PYTHON2" ]
then
	INTERPRETER=$PYTHON2
elif [ -f "$PYTHON" ]
then
	INTERPRETER=$PYTHON
else
	INTERPRETER=python
fi

APPLICATIONS=`$INTERPRETER -c "import sys,os; print ' '.join(os.path.join(_,'exabgp','application.py') for _ in sys.path if os.path.isfile('/'.join((_,'exabgp/application.py'))))"`
APPLICATION=`echo $APPLICATIONS | awk '{ print $1; }'`

exec $INTERPRETER -m exabgp.debug $APPLICATION $*
