#!/usr/bin/python

# Copyright (C) 2013:
#    Gabes Jean, j.gabes@shinken-solutions.com
#
# This file is part of Shinken Enterprise, all rights reserved.

import optparse

from shinkensolutions.localinstall import get_local_daemons, VERSION, WARNING_COLOR, ERROR_COLOR, OK_COLOR, get_local_instances_for_type, POSSIBLE_DAEMONS, get_instance_name, get_local_daemon_configuration_file_path

if __name__ == '__main__':
    parser = optparse.OptionParser("%prog ", version="%prog: " + VERSION, description='This tool is used to list all local daemons on this server')
    
    opts, args = parser.parse_args()
    
    daemons = get_local_daemons()
    
    for d in POSSIBLE_DAEMONS:
        local_instances = get_local_instances_for_type(d)

        if len(local_instances) == 0:  # no such daemons
            print ' %s:\033[%dm DISABLED\033[0m' % (d.ljust(20), WARNING_COLOR)
        else:  # some are available, default and some others
            local_instances.sort()
            star = '-- '
            for (instance_id, enabled) in local_instances:
                cfg_p = get_local_daemon_configuration_file_path(d, instance_id)
                # Some exception for display only
                if d == 'arbiter':
                    cfg_p = '/etc/shinken/shinken.cfg'
                if d == 'synchronizer':
                    cfg_p = '/etc/shinken/synchronizer.cfg'
                    
                display_name = '[id:%s] %s' % (instance_id, get_instance_name(d, instance_id))
                if enabled:
                    print '%s%s     - %s:\033[%dm ENABLED \033[0m  | configuration in: %s' % (star, d.ljust(12), display_name.ljust(30), OK_COLOR, cfg_p)
                else:
                    print '%s%s     - %s:\033[%dm DISABLED\033[0m  | configuration in: %s' % (star, d.ljust(12), display_name.ljust(30), WARNING_COLOR, cfg_p)
                star = '   '


