#!/usr/bin/python
# -*- coding: utf-8 -*-


"""
Launcher.
"""


import sys

from ergo import init, show_error
from ergo.core import Instance as ErgoInstance, Error as ErgoError


def main(argv = []):
    """
    Launcher.
    """
    
    # Initialize
    try:
        # After this globals available: config, log
        if not init(argv):
            return 0
    except ErgoError, error:
        return show_error(error.message)
    
    # Start instances
    instances = []
    
    for account in config["ao"]["accounts"]:
        ergo = ErgoInstance(
            username  = account["username"],
            password  = account["password"],
            host      = account["dimension"]["host"],
            port      = account["dimension"]["port"],
            dimension = account["dimension"]["name"],
            character = account["character"],
        )
        
        ergo.start()
        
        instances.append(ergo)
    
    for ergo in instances:
        ergo.join()
    
    return 0


if __name__ == "__main__":
    status = main(sys.argv[1:])
    sys.exit(status)
