#!/usr/bin/env python

import sys
import os
import glob
import pipes

# Set up PYTHONPATH to use the output of 'setup.py build', which will
# be in a subdirectory called 'lib.<something>' of the 'build'
# directory. The <something> will depend on the particular host
# architecture, so we use glob to find it, and hope to find only one
# of them.

builddir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "build")
libdirs = glob.glob(os.path.join(builddir, "lib.*"))
if len(libdirs) > 1:
    sys.stderr.write("Found multiple possible lib directories:" + "".join(
            ["\n  %s" % libdir for libdir in libdirs]) + "\n")
    sys.exit(1)
if len(libdirs) == 0:
    sys.stderr.write("Found no possible lib directory in %s\n" % builddir)
    sys.exit(1)
if "PYTHONPATH" in os.environ:
    os.environ["PYTHONPATH"] = libdirs[0] + ":" + os.environ["PYTHONPATH"]
else:
    os.environ["PYTHONPATH"] = libdirs[0]

if len(sys.argv) > 1:
    os.execvp("python", ["python"] + sys.argv[1:])
else:
    sys.stdout.write(
        pipes.quote("PYTHONPATH=" + os.environ["PYTHONPATH"]) +
        "; export PYTHONPATH\n")
