#!/usr/bin/python

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

import sys
import optparse

from shinkensolutions import localinstall
from shinkensolutions.localinstall import (VERSION, check_root, do_import_data_history_from_file, do_import_data_history_from_version,
                                           do_import_data_history_no_version, add_update_version_to_context, merge_from_key, disable_addons, nagvis_update_uuid, __get_server_identity, get_shinken_current_version, add_patch_to_context, POSSIBLE_DATA)

if __name__ == '__main__':
    parser = optparse.OptionParser("%prog", version="%prog: " + VERSION, description='(INTERNAL TOOL, FOR SUPPORT USAGE ONLY) This tool is used to manage the context.json file')
    parser.add_option('--import-data-history-from-file', dest='import_data_history_from_file', help="Take the data_history-><data_type> from a backup context.json")
    parser.add_option('--import-data-history-from-version', dest='import_data_history_from_version', help="Only guess the data_history-><data_type> from a version number")
    parser.add_option('--import-data-history-no-version', dest='import_data_history_no_version', action='store_true', help="Cannot guess the version number, so do not create it")
    parser.add_option('--update-version', dest='update_version', action='store', help="Add information about update")
    parser.add_option('--install-patch', dest='install_patch', action='store_true', help="Add information about patch installation")
    parser.add_option('--revert-patch', dest='revert_patch', action='store_true', help="Add information about patch revert")
    parser.add_option('--install-addon', dest='install_addon', action='store_true', help="Add information about addon installation")
    parser.add_option('--revert-addon', dest='revert_addon', action='store_true', help="Add information about addon revert")
    parser.add_option('--merge-with-key', dest='merge_key', action='store', help="Merge the JSON file given as parameter into context.json, under the specified key")
    parser.add_option('--merge-json-file', dest='merge_file', action='store', help="Merge the JSON file given as parameter into context.json, under the specified key")
    parser.add_option('--backup-path', dest='backup_path', help='The path of the backup restored')
    parser.add_option('--data-type', dest='data_type', action='store', help='The type of data for which the operation is made')
    parser.add_option('--patch-name', dest='patch_name', action='store', help='The name of the patch')
    parser.add_option('--disable-addons', dest="disable_addons", action="store_true", help='Disable addons in context file')
    parser.add_option('--update-nagvis-uuid', dest="update_nagvis_uuid", action="store_true", help='Update nagvis maps UUID')
    parser.add_option('--old-nagvis-uuid', dest='old_uuid', action='store', help='Old UUID for nagvis map')
    parser.add_option('--new-nagvis-uuid', dest='new_uuid', action='store', help='New UUID for nagvis map')
    parser.add_option('--get-server-identity', dest='get_id', action='store_true', help='New UUID for nagvis map')
    parser.add_option('--get-shinken-current-version', dest='get_current_version', action='store_true', help='Returns Shinken current version')
    parser.add_option('--comment', dest='comment', default='', help='Add a comment on import or update')
    
    opts, args = parser.parse_args()
    
    # If not root, exit
    check_root()
    
    if opts.disable_addons:
        disable_addons()
        sys.exit(0)
    
    if opts.get_id:
        name, uuid = __get_server_identity()
        print uuid
        sys.exit(0)
    
    if opts.merge_key:
        merge_from_key(opts.merge_key, opts.merge_file)
        sys.exit(0)
    
    import_data_history_from_file = opts.import_data_history_from_file
    if import_data_history_from_file:
        do_import_data_history_from_file(opts.data_type, import_data_history_from_file, opts.backup_path, comment=opts.comment)
        sys.exit(0)
    
    import_data_history_from_version = opts.import_data_history_from_version
    if import_data_history_from_version:
        do_import_data_history_from_version(import_data_history_from_version, opts.backup_path)
        sys.exit(0)
    
    if opts.import_data_history_no_version:
        do_import_data_history_no_version(opts.backup_path)
        sys.exit(0)
    
    if opts.update_version:
        add_update_version_to_context(opts.update_version)
        sys.exit(0)
    
    if opts.install_patch or opts.revert_patch or opts.install_addon or opts.revert_addon:
        if not opts.data_type or not opts.patch_name:
            print "ERROR: You must provide the options --data-type and --patch-name"
            parser.print_usage()
            sys.exit(1)
        for patch_type in opts.data_type.split(','):
            if patch_type not in POSSIBLE_DATA and patch_type != 'installation':
                print "ERROR: [%s] is not part of the available data types : installation, %s" % (patch_type, ", ".join(POSSIBLE_DATA))
                parser.print_usage()
                sys.exit(1)
    
    if opts.install_patch:
        add_patch_to_context('PATCH', opts.data_type, opts.patch_name)
    if opts.revert_patch:
        add_patch_to_context('UNPATCH', opts.data_type, opts.patch_name)
    if opts.install_addon:
        add_patch_to_context('ADDON', opts.data_type, opts.patch_name)
    if opts.revert_addon:
        add_patch_to_context('REMOVE ADDON', opts.data_type, opts.patch_name)
    
    if opts.update_nagvis_uuid:
        if not opts.new_uuid:
            print "ERROR: You must provide the option --new-nagvis-uuid in relation with the --update-nagvis-uuid option"
            parser.print_usage()
            sys.exit(1)
        
        nagvis_update_uuid(opts.old_uuid, opts.new_uuid)
        sys.exit(0)
    
    if opts.get_current_version:
        shinken_version = get_shinken_current_version()
        print shinken_version
