"""
Evennia MUD/MUX/MU* creation system

This is the main top-level API for Evennia. You can also explore the
evennia library by accessing evennia.<subpackage> directly.

For full functionality you need to explore this module via a django-
aware shell. Go to your game directory and use the command 'evennia.py shell'
to launch such a shell (using python or ipython depending on your install).

See www.evennia.com for full documentation.

"""

# Delayed loading of properties

# Typeclasses

DefaultPlayer = None
DefaultGuest = None
DefaultObject = None
DefaultCharacter = None
DefaultRoom = None
DefaultExit = None
DefaultChannel = None
DefaultScript = None

# Database models
ObjectDB = None
PlayerDB = None
ScriptDB = None
ChannelDB = None
Msg = None

# commands
Command = None
CmdSet = None
default_cmds = None
syscmdkeys = None

# search functions
search_object = None
search_script = None
search_player = None
search_channel = None
search_help = None
search_tag = None

# create functions
create_object = None
create_script = None
create_player = None
create_channel = None
create_message = None
create_help_entry = None

# utilities
settings = None
lockfuncs = None
oobhandler = None
logger = None
gametime = None
ansi = None
spawn = None
managers = None
contrib = None

# Handlers
SESSION_HANDLER = None
TICKER_HANDLER = None
OOB_HANDLER = None
CHANNEL_HANDLER = None


def _create_version():
    """
    Helper function for building the version string
    """
    import os
    from subprocess import check_output, CalledProcessError, STDOUT

    version = "Unknown"
    root = os.path.dirname(os.path.abspath(__file__))
    try:
        with open(os.path.join(root, "VERSION.txt"), 'r') as f:
            version = f.read().strip()
    except IOError as err:
        print err
    try:
        version = "%s (rev %s)" % (version, check_output("git rev-parse --short HEAD", shell=True, cwd=root, stderr=STDOUT).strip())
    except (IOError, CalledProcessError):
        pass
    return version

__version__ = _create_version()
del _create_version

def _init():
    """
    This function is called automatically by the launcher only after
    Evennia has fully initialized all its models. It sets up the API
    in a safe environment where all models are available already.
    """
    def imp(path, variable=True):
        "Helper function"
        mod, fromlist = path, "None"
        if variable:
            mod, fromlist = path.rsplit('.', 1)
        return __import__(mod, fromlist=[fromlist])

    global DefaultPlayer, DefaultObject, DefaultGuest, DefaultCharacter