#!/usr/bin/python3 -sP
#
# Copyright (c) 2013-2015 Marin Atanasov Nikolov <dnaeon@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer
#    in this position and unchanged.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
vpoller-proxy is a ZeroMQ proxy/broker that load balances requests
between a pool of ZeroMQ workers.

"""

from __future__ import print_function

import os
import logging

from vpoller import __version__
from vpoller.client import VPollerClient
from vpoller.proxy import VPollerProxyManager
from docopt import docopt

def start(config):
    """
    Start the VPoller Proxy daemon

    Args:
        config  (string): Path to the vPoller configuration file

    """
    manager = VPollerProxyManager(config_file=config)
    manager.start()

def stop(endpoint):
    """
    Stops the VPoller Proxy

    Args:
        endpoint (string): The endpoint we send the shutdown message to

    """
    client = VPollerClient(endpoint=endpoint, timeout=1000, retries=3)
    result = client.run({'method': 'shutdown'})

    print(result.encode('utf-8'))

def status(endpoint):
    """
    Get status information from the VPoller Proxy

    Args:
        endpoint (string): The endpoint we send the status message to
    
    """
    client = VPollerClient(endpoint=endpoint, timeout=1000, retries=3)
    result = client.run({'method': 'status'})

    print(result.encode('utf-8'))
    
def main():

    usage="""
Usage: vpoller-proxy [-d] [-f <config>] start
       vpoller-proxy [-d] -e <endpoint> stop
       vpoller-proxy [-d] -e <endpoint> status
       vpoller-proxy --help
       vpoller-proxy --version

Arguments:
  start                                     Start the VPoller Proxy
  stop                                      Stop the VPoller Proxy
  status                                    Get status information

Options:
  -h, --help                                Display this usage info
  -v, --version                             Display version and exit
  -d, --debug                               Debug mode, be more verbose
  -f <config>, --file <config>              Specify config file to use
                                            [default: /etc/vpoller/vpoller.conf]
  -e <endpoint>, --endpoint <endpoint>      Specify the endpoint we connect to

"""

    args = docopt(usage, version=__version__)

    if not os.path.exists(args['--file']):
        raise SystemExit('Configuration file {} does not exist'.format(args['--file']))
    
    level = logging.DEBUG if args['--debug'] else logging.INFO
    logging.basicConfig(
        format='[%(asctime)s - %(levelname)s/%(processName)s] %(message)s',
        level=level
    )

    if args['start']:
        start(args['--file'])
    elif args['stop']:
        stop(args['--endpoint'])
    elif args["status"]:
        status(args['--endpoint'])

if __name__ == '__main__':
    main()
