#!/usr/bin/env python
#
# This file is part of Fource
#
# Fource is free software: you can redistribute it and/or modify
# it under the terms of the The MIT License (MIT).
#
# Fource is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the MIT License (MIT)
# along with Fource.  If not, see <http://opensource.org/licenses/MIT>.
#
#### GLOBAL VARIABLES ##########################################################

__requires__ = ['fource']

import os
import sys
from optparse import OptionParser
# TODO
# import logging
# import logging.handlers

# Augment PYTHONPATH to find Python modules relative to this file path
# This is so that we can find the modules when running from a local checkout
# installed as editable with `pip install -e ...` or `python setup.py develop`
local_module_path = os.path.abspath(
    os.path.join(os.path.dirname(__file__), '..', 'lib')
)
sys.path.append(local_module_path)

from fource import controller
from fource import __version__, __author__


VERSION = "Version: %s, Author: %s" % (__version__, __author__)
DESCRIPTION = """
Fource is a fully automated status board application. It enables organizations
to create their own status board within minutes.

Features:
    * Easy configuration of service/API checks in non-programmatic way.
    * Automated interval based monitoring of every API defined.
    * Providing a detailed dashboard for operations.
    * Providing a customer friendly service status board.
    * Easy extension using modules.

Business Impact:
    * Helps in meeting SLA's.
    * Keeping customers updated on status of services.
    * Providing granular details to operations helping them to take informative decision.
"""
OPTIONS = {
    "c,config": "Path of config file",
}
USAGE = "%s [options]"  % os.path.basename(__file__)
DEFAULTS = {}

# # Logging in syslog (/var/log/syslog)
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
# handler = logging.handlers.SysLogHandler(address='/dev/log')
# logger.addHandler(handler)


#### FUNCTIONS AND CLASSES #####################################################
def parse_options():
    parser = OptionParser(description=DESCRIPTION, usage=USAGE, version=VERSION)
    for option, description in OPTIONS.items():
        option = option.strip()
        if ',' in option:
            shortopt, longopt = [k.strip() for k in option.split(',')]
            parser.add_option(
                '-%s' % shortopt,
                '--%s' % longopt,
                dest=longopt,
                help=description.strip(),
            )
        else:
            parser.add_option(
                '--%s' % option,
                dest=option,
                help=description.strip(),
            )
    option_args, _ = parser.parse_args()
    arguments = {}
    for option in OPTIONS.keys():
        option = option.strip()
        if ',' in option:
            _, option = [k.strip() for k in option.split(',')]
        arguments[option] = eval("option_args.%s" % option)
        if (not arguments[option]) and DEFAULTS.get(option):
            arguments[option] = DEFAULTS.get(option)
    return arguments


def run():
    arguments = parse_options()
    exit_status, message = controller.execute(arguments)
    if exit_status:
        print 'OK - %s' % message
        sys.exit(0)
    else:
        print 'CRITICAL - %s' % message
        sys.exit(2)
    # try:
    #     arguments = parse_options()
    #     controller.execute(arguments)
    # except Exception as e:
    #     print e.message
    #     sys.exit(1)


#### EXECUTE ###################################################################

if __name__ == '__main__':
    run()
