#!/usr/bin/python

import sys
if len(sys.argv) == 1:
    print "usage: lbin <command> [...]"
    print "This script recurses up the current path, looking for the Acro 'bin' directory."
    print "If found, this directory is prepended to the PATH environment path."
    sys.exit(1)

import os
import os.path
import subprocess

pexec = None
curr = os.path.abspath(os.getcwd())
dirs = []
while os.sep in curr:
    if os.path.exists(curr+os.sep+"python"):
        dirs.append( curr+os.sep+'python'+os.sep+"bin")
    if os.path.exists(curr+os.sep+"bin"):
        dirs.append( curr+os.sep+"bin")
    if os.path.basename(curr) == "":
        break
    curr = os.path.dirname(curr)

os.environ["PATH"] = os.pathsep.join(dirs) + os.pathsep + os.environ["PATH"]
try:
    subprocess.call(sys.argv[1:])
except OSError, err:
    print "ERROR executing command '%s': %s" % (' '.join(sys.argv[1:]), str(err))
