#! /usr/bin/env python
"""Wrapper script for the bv_maker command-line tool."""

import os
import sys

# -----------------------------------------------------------------------------
# Make sure that sys.path is set correctly before importing our main module.
# -----------------------------------------------------------------------------
if os.path.exists(sys.argv[0]):
    this_script = sys.argv[0]
else:
    this_script = None
    for p in os.environ.get('PATH', '').split(os.pathsep) + [os.curdir]:
        s = os.path.join(p, sys.argv[0])
        if os.path.exists(s):
            this_script = s
            break
if this_script:
    this_script = os.path.abspath(this_script)
    python_modules = os.path.join(
        os.path.dirname(os.path.dirname(this_script)), 'python')
    if os.path.isdir(python_modules):
        sys.path.insert(0, python_modules)
        # set also the python path in the PYTHONPATH env variable
        # so that commands run as subprocesses will get the right path.
        # This happens in brainvisa-cmake from the cmake part.
        ppath = os.environ.get('PYTHONPATH')
        if ppath is None:
            ppath = python_modules
        else:
            ppath = os.pathsep.join([python_modules, ppath])
        os.environ['PYTHONPATH'] = ppath
# -----------------------------------------------------------------------------

from brainvisa_cmake.main import main
sys.exit(main())
