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

"""
    File: pycollector
    Description: Collector wrapper.
"""

__software__ = "pycollector"
__desc__ = "Generic collector written in Python."
__version_info__ = ('0', '2', '6')
__version__ = '.'.join(__version_info__)

__author__ = "Ronald Kaiser"
__email__ = "ronald at intelie dot com dot br"


import sys
import logging
import optparse

try:
    import __meta__
    __meta__.load_paths()
except ImportError, e:
    print e
    sys.exit(-1)

import daemon_util
import ascii
import conf_reader
from rwtypes import rwtypes
from collector import Collector


DAEMON=True
SERVER=True
SERVER_PORT=8442


def disable_daemon(option, ospt, value, parser):
    global DAEMON
    DAEMON = False


def disable_server(option, opt, value, parser):
    global SERVER
    SERVER = False


def set_server_port(option, opt, value, parser, *args):
    global SERVER_PORT
    SERVER_PORT = int(parser.rargs[0])


def status(option, opt, value, parser):
    daemon_util.status()


def start(option, opt, value, parser):
    daemon_util.start(Collector, DAEMON, SERVER, SERVER_PORT)


def stop(option, opt, value, parser):
    daemon_util.stop()


def force_stop(option, opt, value, parser):
    daemon_util.force_stop()


def log_tail(option, opt, value, parser):
    daemon_util.log_tail()


def show_reader_types(option, opt, value, parser):
    keys = rwtypes.get_reader_keys()
    for key in keys:
        print "* %s" % key
    sys.exit(0)


def show_writer_types(option, opt, value, parser):
    keys = rwtypes.get_writer_keys()
    for key in keys:
        print "* %s" % key
    sys.exit(0)


if __name__ == '__main__':
    parser = optparse.OptionParser(prog=__software__, version=__version__)
    parser.set_description(__desc__)

    parser.add_option("--logtail",
                      action="callback",
                      callback=log_tail,
                      nargs=0,
                      help="tail -f in pycollector log")

    parser.add_option("--rtypes",
                      action="callback",
                      callback=show_reader_types,
                      nargs=0,
                      help="show reader types")

    parser.add_option("--wtypes",
                      action="callback",
                      callback=show_writer_types,
                      nargs=0,
                      help="show writer types")

    parser.add_option("--nodaemon",
                      action="callback",
                      callback=disable_daemon,
                      nargs=0,
                      help="enable collector to start without detaching the process")

    parser.add_option("--noserver",
                      action="callback",
                      callback=disable_server,
                      nargs=0,
                      help="disable default web server with realtime data")

    parser.add_option("--serverport",
                      action="callback",
                      callback=set_server_port,
                      nargs=1,
                      help="change web server port")

    parser.add_option("--start",
                      action="callback",
                      callback=start,
                      nargs=0,
                      help="start collector")

    parser.add_option("--stop",
                      action="callback",
                      callback=stop,
                      nargs=0,
                      help="stop collector")

    parser.add_option("--forcestop",
                      action="callback",
                      callback=force_stop,
                      nargs=0,
                      help="stop all running instances")

    parser.add_option("--status",
                      action="callback",
                      callback=status,
                      nargs=0,
                      help="check if collector is runnning.")

    if len(sys.argv) < 2:
        print ascii.ascii
        print __version__
        parser.print_help()
        sys.exit(0)
    elif '--help' in sys.argv:
        print ascii.ascii
        print __version__
    (options, args) = parser.parse_args()
