#!/usr/bin/python
import sys
import os, os.path as osp

try:
    import narvalbot
except:
    from cubes.narval import narvalbot
    sys.modules['narvalbot'] = narvalbot

if narvalbot.MODE == 'dev':
    # we are running from sources, cubicweb *should* be available,
    # let's use it as starting point to look for cubes in which there
    # is a _narval directory
    from cubicweb import CW_SOFTWARE_ROOT
    cubesdir = osp.join(CW_SOFTWARE_ROOT, '..', 'cubes')
    pythonpath = []
    for cube in os.listdir(cubesdir):
        pluginsdir = osp.abspath(osp.join(cubesdir, cube, '_narval'))
        if osp.isdir(pluginsdir):
            pythonpath.append(pluginsdir)
else:
    pythonpath = [osp.join(narvalbot.INSTALL_PREFIX, 'share', 'narval')]

if 'PYTHONPATH' in os.environ:
    pythonpath +=[i for i in os.environ['PYTHONPATH'].split(':')]
os.environ['PYTHONPATH'] = ':'.join(pythonpath)
# now we're fine

from narvalbot import main

main.run(sys.argv[1:])
