#!/usr/bin/python

# Copyright (C) 2009-2012:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    Gregory Starck, g.starck@gmail.com
#    Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken.  If not, see <http://www.gnu.org/licenses/>.


import optparse
import sys
import os
import re
import tempfile
import json
import shutil
import zipfile
import tarfile
import pycurl
import urllib
import ConfigParser
import imp
import shlex
import readline
import signal
from StringIO import StringIO

try:
    import shinken
    from shinkensolutions.localinstall import VERSION
except ImportError:
    # If importing shinken fails, try to load from current directory
    # or parent directory to support running without installation.
    # Submodules will then be loaded from there, too.
    import imp
    
    imp.load_module('shinken', *imp.find_module('shinken', [os.path.realpath("."), os.path.realpath(".."), os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "..")]))
    from shinkensolutions.localinstall import VERSION

print "This command is disabled in the Enterprise edition"
sys.exit(0)

from shinken.log import logger, cprint, nagFormatter
from shinken.objects.config import Config

logger.setLevel('WARNING')
logger.handlers[0].setFormatter(nagFormatter)


# Handle some signals
def sig_handler(signalnum, handle):
    """ Handle some signals """
    sys.exit(0)


signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)

CONFIG = {}


class Dummy():
    def __init__(self):
        pass
    
    
    def add(self, obj):
        pass


logger.load_obj(Dummy())

if os.name != 'nt':
    DEFAULT_CFG = os.path.expanduser('~/.shinken.ini')
else:
    DEFAULT_CFG = 'c:\\shinken\\etc\\shinken.ini'


# This will allow to add comments to the generated configuration file, once
# by section.
class ConfigParserWithComments(ConfigParser.RawConfigParser):
    def add_comment(self, section, comment):
        if not comment.startswith('#'):
            comment = '#' + comment
        self.set(section, ';%s' % (comment,), None)
    
    
    def write(self, fp):
        """Write an .ini-format representation of the configuration state."""
        if self._defaults:
            fp.write("[%s]\n" % ConfigParser.DEFAULTSECT)
            for (key, value) in self._defaults.items():
                self._write_item(fp, key, value)
            fp.write("\n")
        for section in self._sections:
            fp.write("[%s]\n" % section)
            for (key, value) in self._sections[section].items():
                self._write_item(fp, key, value)
            fp.write("\n")
    
    
    def _write_item(self, fp, key, value):
        if key.startswith(';') and value is None:
            fp.write("%s\n" % (key[1:],))
        else:
            fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))


# Commander is the main class for managing the CLI session and behavior
class CLICommander(object):
    ini_defaults = {'paths': {
        'comment': ''' # Set the paths according to your setup. Defaults follow
# the Linux Standard Base''',
        'values' : [('etc', '/etc/shinken'), ('lib', '/var/lib/shinken'),
                    ('share', '%(lib)s/share'), ('cli', '%(lib)s/cli'),
                    ('packs', '%(etc)s/packs'), ('modules', '%(lib)s/modules'),
                    ('doc', '%(lib)s/doc'), ('inventory', '%(lib)s/inventory'),
                    ('libexec', '%(lib)s/libexec'),
                    ]}
        ,
        'shinken.io'       : {
            'comment': '''# Options for connection to the shinken.io website.
# proxy: curl style, should look as http://user:password@server:3128
# api_key: useful for publishing packages or earn XP after each install. Create an account at http://shinken.io and go to http://shinken.io/~
''',
            'values' : [('proxy', ''), ('api_key', '')]}
    }
    
    
    def __init__(self, config, cfg, opts):
        self.keywords = {}
        self.cfg = cfg
        self.config = config
        self.init_done = False
        
        # We will now try to load the keywords from the modules
        self.load_cli_mods(opts)
        
        self.completion_matches = []
    
    
    def load_cli_mods(self, opts):
        # Main list of keywords for the first parameter
        self.keywords = {}
        if not 'paths' in self.config or not 'cli' in self.config.get('paths', []):
            # We are dign the init, so bail out
            if opts.do_init:
                return
            logger.error('Cannot load cli commands, missing paths or cli entry in the config')
            return
        
        self.init_done = True
        
        cli_mods_dir = os.path.abspath(self.config['paths']['cli'])
        logger.debug("WILL LOAD THE CLI DIR %s" % cli_mods_dir)
        cli_mods_dirs = [os.path.join(cli_mods_dir, d) for d in os.listdir(cli_mods_dir) if os.path.isdir(os.path.join(cli_mods_dir, d))]
        
        new_config_param = False
        for d in cli_mods_dirs:
            f = os.path.join(d, 'cli.py')
            if os.path.exists(f):
                dname = os.path.split(d)[1]
                # Let's load it, but first att it to sys.path
                sys.path.append(d)
                # Load this PATH/cli.py file
                m = imp.load_source(dname, f)
                # Link the CONFIG objet into it
                m.CONFIG = self.config
                exports = getattr(m, 'exports', {})
                for (f, v) in exports.iteritems():
                    m_keywords = v.get('keywords', [])
                    for k in m_keywords:
                        e = {'f'          : f,
                             'args'       : v.get('args', []),
                             'description': v.get('description', ''),
                             'came_from'  : dname}
                        # Finally save it
                        self.keywords[k] = e
                
                # And now the configuration part
                mconfig = getattr(m, 'configurations', {})
                for (p, e) in mconfig.iteritems():
                    # print p, e
                    # print p in self.config
                    if p not in self.config:
                        self.ini_defaults[p] = e
                        new_config_param = True
                        continue
                    # Maybe it's just in the self.ini_defaults if so it's not a new
                    # config
                    if p not in self.ini_defaults:
                        self.ini_defaults[p] = e
                    # print "CONFIG P",p, self.config[p]
                    cvalues = self.config[p]
                    ini_defaults_values = self.ini_defaults[p]['values']
                    mvalues = e.get('values', [])
                    for (prop, v) in mvalues:
                        if prop not in cvalues:
                            ini_defaults_values.append((prop, v))
                            new_config_param = True
        
        # update our ini with the new defaults
        if new_config_param:
            logger.info('New module parameter detected, updating the ini file')
            write_ini_file(self.cfg, self)
        
        logger.debug('We load the keywords %s' % self.keywords)
    
    
    def loop(self):
        if not self.init_done:
            logger.error('CLI loading not done: missing configuration data. Please run --init')
            return
        
        readline.parse_and_bind('tab: complete')
        # Use the CLI as completer
        readline.set_completer(self.complete)
        
        # EMACS rules :) / VI sucks
        readline.parse_and_bind('set editing-mode emacs')
        # Try to read and save the history when exiting
        histfile = os.path.join(os.path.expanduser("~"), ".shinken.history")
        try:
            readline.read_history_file(histfile)
        except IOError:
            pass
        import atexit
        atexit.register(readline.write_history_file, histfile)
        
        while True:
            try:
                line = raw_input('> ')
            except EOFError:
                print ''
                break
            line = line.strip()
            if line in ['quit', 'bye', 'sayonara']:
                break
            
            if not line:
                continue
            if line.startswith('!'):
                self.execute_shell(line[1:])
                continue
            # More cleassic command
            args = shlex.split(line.encode('utf8', 'ignore'))
            logger.debug("WANT TO CALL WITH ARGS %s" % args)
            self.one_loop(args)
    
    
    def execute_shell(self, line):
        output = os.popen(line).read()
        print output
    
    
    # Execute a function based on the command line
    def one_loop(self, command_args):
        if not self.init_done:
            logger.error('CLI loading not done: missing configuration data. Please run --init')
            return
        
        logger.debug("ARGS: %s" % command_args)
        keyword = command_args.pop(0)
        mod = self.keywords.get(keyword, None)
        if mod is None:
            logger.error("UNKNOWN command %s" % keyword)
            return
        
        # Now prepare a new parser, for the command call this time
        command_parser = optparse.OptionParser(
            '',
            version="%prog " + VERSION)
        command_parser.prog = keyword
        
        f_args = []
        for a in mod.get('args', []):
            n = a.get('name', None)
            if n is None:
                continue
            default = a.get('default', Dummy)
            description = a.get('description', '')
            _type = a.get('type', 'standard')
            if n.startswith('-'):
                # Get a clean version of the parameter, without - or --
                dest = n[1:]
                if dest.startswith('-'):
                    dest = dest[1:]
                # And if the parameter is like download-only, map it to
                # download_only
                dest = dest.replace('-', '_')
                if _type == 'bool':
                    command_parser.add_option(n, action='store_true', dest=dest, help=(description))
                else:
                    command_parser.add_option(n, dest=dest, help=(description))
        
        cmd_opts, cmd_args = command_parser.parse_args(command_args)
        f = mod.get('f', None)
        logger.debug("CALLING" + str(f) + "WITH" + str(cmd_args) + "and" + str(cmd_opts) + str(type(cmd_opts)) + str(dir(cmd_opts)))
        f(*cmd_args, **cmd_opts.__dict__)
    
    
    # Complete is a bit strange in readline. It will call it as it do not answser None, by increasing the
    # state int for each call. So don't loop forever!
    def complete(self, text, state):
        # print "STATE?", text, state
        
        # New completion call
        if state == 0:
            self.completion_matches = []
        
        # print "TRY TO COMPLETE", text
        text = text.strip()
        
        args = shlex.split(text.encode('utf8', 'ignore'))
        # print "ARGS", args
        if len(args) == 0:
            args = ['']
        keyword = args[0]
        # Trying to expand the command name
        if len(args) == 1 and state == 0:
            for k in self.keywords:
                if k.startswith(text):
                    self.completion_matches.append(k)
        
        response = None
        try:
            response = self.completion_matches[state]
        except IndexError:
            response = None
        
        # print "CALL", text, "state", state, response
        return response


def write_ini_file(cfg, CLI):
    new_cfg = ConfigParserWithComments()
    modify = False
    if not cfg:
        cfg = ConfigParser.ConfigParser()
    
    # Import data from the loaded configuration
    for section in cfg.sections():
        if not new_cfg.has_section(section):
            new_cfg.add_section(section)
        for (k, v) in cfg.items(section):
            new_cfg.set(section, k, v)
    
    for (s, d) in CLI.ini_defaults.iteritems():
        comment = d.get('comment', '')
        values = d.get('values', [])
        if not cfg.has_section(s) and not new_cfg.has_section(s):
            print "Creating ini section", s
            new_cfg.add_section(s)
            modify = True
        if comment:
            new_cfg.add_comment(s, comment)
        for (k, v) in values:
            if not cfg.has_option(s, k):
                new_cfg.set(s, k, v)
                modify = True
                # print new_cfg.items(s)
    if modify:
        print "Saving the new configuration file", opts.iniconfig
        with open(opts.iniconfig, 'wb') as configfile:
            new_cfg.write(configfile)


if __name__ == '__main__':
    parser = optparse.OptionParser(
        '',
        version="%prog " + VERSION,
        add_help_option=False)
    parser.add_option('--proxy', dest="proxy",
                      help="""Proxy URI. Like http://user:password@proxy-server:3128""")
    parser.add_option('-A', '--api-key',
                      dest="api_key", help=("Your API key for uploading the package to the Shinken.io website. If you don't have one, please go to your account page"))
    parser.add_option('-l', '--list', action='store_true',
                      dest="do_list", help=("List available commands"))
    parser.add_option('--init', action='store_true',
                      dest="do_init", help=("Initialize/refill your shinken.ini file (default to %s)" % DEFAULT_CFG))
    parser.add_option('-D', action='store_true',
                      dest="do_debug", help=("Enable the debug mode"))
    parser.add_option('-c', '--config', dest="iniconfig", default=DEFAULT_CFG,
                      help=("Path to your shinken.ini file. Default: %s" % DEFAULT_CFG))
    parser.add_option('-v', action='store_true',
                      dest="do_verbose", help=("Be more verbose"))
    parser.add_option('-h', '--help', action='store_true',
                      dest="do_help", help=("Print help"))
    
    # First parsing, for purely internal parameters, but disable
    # errors, because we only want to see the -D -v things
    old_error = parser.error
    parser.error = lambda x: 1
    opts, args = parser.parse_args()
    # reenable the errors for later use
    parser.error = old_error
    
    do_help = opts.do_help
    if do_help and len(args) == 0:
        parser.print_help()
        sys.exit(0)
    
    if opts.do_verbose:
        logger.setLevel('INFO')
    
    if opts.do_debug:
        logger.setLevel('DEBUG')
    
    cfg = None
    if not os.path.exists(opts.iniconfig):
        logger.debug('Missing configuration file!')
    else:
        cfg = ConfigParser.ConfigParser()
        cfg.read(opts.iniconfig)
        for section in cfg.sections():
            if not section in CONFIG:
                CONFIG[section] = {}
            for (key, value) in cfg.items(section):
                CONFIG[section][key] = value
    
    CLI = CLICommander(CONFIG, cfg, opts)
    
    
    # We should look on the sys.argv if we find a valid keywords to
    # call in one loop or not.
    def hack_sys_argv():
        command_values = []
        internal_values = []
        # print "RARGS", parser.rargs
        founded = False
        for arg in sys.argv:
            if arg in CLI.keywords:
                founded = True
            # Did we found it?
            if founded:
                command_values.append(arg)
            else:  # ok still not, it's for the shinekn command so
                internal_values.append(arg)
        
        sys.argv = internal_values
        return command_values
    
    
    # We will remove specific commands from the sys.argv list and keep
    # them for parsing them after
    command_args = hack_sys_argv()
    
    # Global command parsing, with the error enabled this time
    opts, args = parser.parse_args()
    
    if opts.do_help:
        if len(command_args) == 0:
            logger.error("Cannot find any help for you")
            sys.exit(1)
        a = command_args.pop(0)
        if a not in CLI.keywords:
            logger.error("Cannot find any help for %s" % a)
            sys.exit(1)
        cprint('%s' % a, 'green')
        for arg in CLI.keywords[a]['args']:
            n = arg.get('name', '')
            desc = arg.get('description', '')
            cprint('\t%s' % n, 'green', end='')
            cprint(': %s' % desc)
        
        sys.exit(0)
    
    # If the user explicitely set the proxy, take it!
    if opts.proxy:
        CONFIG['shinken.io']['proxy'] = opts.proxy
    
    # Maybe he/she just want to list our commands?
    if opts.do_list:
        if not CLI.init_done:
            sys.exit(0)
        print "Available commands:"
        all_from = {}
        for (k, m) in CLI.keywords.iteritems():
            came_from = m['came_from']
            if came_from not in all_from:
                all_from[came_from] = [(k, m)]
            else:
                all_from[came_from].append((k, m))
        for (mod_name, d) in all_from.iteritems():
            print '%s:' % mod_name
            for (k, m) in d:
                cprint('\t%s ' % k, 'green', end='')
                cprint(': %s' % m['description'])
        sys.exit(0)
    
    if opts.do_init:
        write_ini_file(cfg, CLI)
        '''
        new_cfg = ConfigParserWithComments()
        modify = False
        if not cfg:
            cfg = ConfigParser.ConfigParser()

        # Import data from the loaded configuration
        for section in cfg.sections():
            if not new_cfg.has_section(section):
                new_cfg.add_section(section)
            for (k, v) in cfg.items(section):
                new_cfg.set(section, k, v)

        for (s, d) in CLI.ini_defaults.iteritems():
            comment = d.get('comment', '')
            values = d.get('values', [])
            if not cfg.has_section(s) and not new_cfg.has_section(s):
                print "Creating ini section", s
                new_cfg.add_section(s)
                modify = True
            if comment:
                new_cfg.add_comment(s, comment)
            for (k, v) in values:
                if not cfg.has_option(s, k):
                    new_cfg.set(s, k, v)
                    modify = True
                #print new_cfg.items(s)
        if modify:
            print "Saving the new configuration file", opts.iniconfig
            with open(opts.iniconfig, 'wb') as configfile:
                new_cfg.write(configfile)
        '''
        sys.exit(0)
    
    # if just call shinken, we must open a prompt, but will be for another version
    if len(command_args) == 0:
        CLI.loop()
        sys.exit(0)
    
    # If it's just a one call shot, do it!
    CLI.one_loop(command_args)
