#!/usr/bin/env python
import sys
import getopt
import os
import sys

def which(program):
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None


cur_version = sys.version_info
if cur_version[0] >= 3:
    interpreter = which('python2')
else:
    interpreter = None

if interpreter is None:
    interpreter = which('python')
if interpreter is None:
    interpreter = sys.executable

try:
    current_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
except:
    current_path = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv[0])))

ishellpath = os.path.join(current_path, 'ibidas_shell.py')

if not os.path.exists(ishellpath):
    ipath =  which('ibidas_shell.py')
    if not ipath is None:
        ishellpath = ipath
    

if hasattr(os, "execv"):
    os.execv(interpreter, [interpreter, ishellpath] + sys.argv[1:])
else:
    import subprocess
    sys.exit(subprocess.Popen([interpreter, ishellpath] + sys.argv[1:]).wait())
