#! /usr/bin/env python

import os
import sys

# Add script directory to sys.path.
# This is complicated due to the fact that __file__ is not always defined.
#
# Note: This must be done before importing pyWhich.

def GetScriptFile():
    """Obtains the full path and file name of the Python script."""
    if (hasattr(GetScriptFile, "file")):
        return GetScriptFile.file
    ret = ""
    try:
        # The easy way. Just use __file__.
        # Unfortunately, __file__ is not available when cx_freeze is used or in IDLE.
        ret = os.path.realpath(__file__)
    except NameError:
        # The hard way.
        if (len(sys.argv) > 0 and len(sys.argv[0]) > 0 and os.path.isabs(sys.argv[0])):
            ret = os.path.realpath(sys.argv[0])
        else:
            ret = os.path.realpath(inspect.getfile(GetScriptFile))
            if (not os.path.exists(ret)):
                # If cx_freeze is used the value of the ret variable at this point is in
                # the following format: {PathToExeFile}\{NameOfPythonSourceFile}. This
                # makes it necessary to strip off the file name to get the correct path.
                ret = os.path.dirname(ret)
    GetScriptFile.file = ret
    return GetScriptFile.file

def GetScriptDirectory():
    """Obtains the path to the directory containing the script."""
    if (hasattr(GetScriptDirectory, "dir")):
        return GetScriptDirectory.dir
    module_path = GetScriptFile()
    GetScriptDirectory.dir = os.path.dirname(module_path)
    return GetScriptDirectory.dir

sys.path.insert(0, GetScriptDirectory())

import copyBuildResults

if (__name__ == "__main__"):
    copyBuildResults.CommandLineArgumentsParser().set_first_argument_index(1)
    sys.exit(copyBuildResults.main(None))
