#!/usr/bin/env python

__author__ = 'thatcher'
import sys
from docker_container_runner import utils
from docker_container_runner.manager import Application, DockerDaemon, Hipache
import pkg_resources  # part of setuptools


# try:
#     settingsfile = sys.argv[3]
# except:


def initialize():
    """
    start it
    """

    directives = utils.read_appconfig(sys.argv[1])

    name, config = directives.items()[0]
    release_name = config['release_name']

    settingsfile = "settings.yml"
    settings = utils.read_settings(settingsfile)

    # Create the application
    application = Application(release_name, config, settings)

    # get (unitialized) containers
    containers = application.get_containers()

    for name, container in containers.items():
        status = container.status

    return application


def create():
    application = initialize()
    application.create_containers()


def start():
    application = initialize()
    application.create_containers()
    application.start_containers()
    application.get_status()

def stop():
    application = initialize()
    application.stop_containers()


# setup the application from the config file
# applications = create_applications()

def print_usage():
    print \
    """
    usage:

    dcr.py APP_CONFIGFILE [command]

    each command will be executed on each host

    options:

    version:        show current dcr version

    Managing the containers
    -----------------------

    status:         show current status of the containers
    create:         create container
    remove:         remove container
    start:          start container
    stop:           stop container
    pull:           pull the container from the registry, will login automatically
    login:          login to the registry (test)

    Hipache things
    --------------
    register:       register the container on all known hipache gateways
    unregister:     remove this specific container from all existing gateways (container must be running)
    unregister_all: remove all containers from this domain on all existing gateways
    switch:         remove existing gateways from back-end and register the new ones
    """

try:
    stream = open(sys.argv[1])
except:
    try:
        if sys.argv[1] == "version":
            print "dcr version: {}".format(pkg_resources.require("dcr")[0].version)
            sys.exit(0)
    except IndexError:
        print_usage()
        sys.exit(1)

directives = utils.read_appconfig(sys.argv[1])


try:
    cmd = sys.argv[2]
except:
    print_usage()
    sys.exit("no command given")

if cmd == "status":
    initialize()
elif cmd == "create":
    create()

elif cmd == "remove":
    application = initialize()
    application.remove_containers()

elif cmd == "start":
    start()
elif cmd == "stop":
    stop()

elif cmd == "pull":
    application = initialize()
    application.login_registry()
    application.pull_image()

elif cmd == "register":
    try:
        domain = sys.argv[3]
    except BaseException as ex:
        sys.exit("please provide a domain name to register")

    application = initialize()
    application.register(domain)

elif cmd == "unregister":
    try:
        domain = sys.argv[3]
    except BaseException as ex:
        sys.exit("please provide a domain name to register")

    application = initialize()
    application.unregister(domain)

elif cmd == "unregister_all":
    try:
        domain = sys.argv[3]
    except BaseException as ex:
        sys.exit("please provide a domain name to unregister everything from")

    application = initialize()
    application.unregister_all(domain)

elif cmd == "switch":
    try:
        domain = sys.argv[3]
    except BaseException as ex:
        sys.exit("please provide a domain name to register")

    application = initialize()
    application.switch_backends(domain)

elif cmd == "login":
    application = initialize()
    application.login_registry()


else:
    print_usage()
    sys.exit(1)
