#!/usr/bin/python3
# coding=utf-8

from optparse import OptionParser
from optparse import OptionGroup
import json
import sys
import uvpconf


class MyParser(OptionParser):
        def format_epilog(self, formatter):
            return self.epilog

if __name__ == '__main__':
    examples = """

Examples:
    Offline set xVirt configurations from json file
        xVirtconf -f /xVirt/conf/json/file

    Online set xVirt configurations from json file
        xVirtconf -f /xVirt/conf/json/file -o

    Query global xVirt configurations
        xVirtconf

    Offline set xVirt configuration by specified key
        xVirtconf -m libvirt -k inject_nmi -v 0

    Online set xVirt configuration by specified key
        xVirtconf -m libvirt -k inject_nmi -v 0 -o

    Query xVirt configuration by specified key
        xVirtconf -m libvirt -k inject_nmi

"""
    parser = MyParser(usage="%prog [optinos]", epilog=examples)

    parser.add_option("-f", "--file", action="store", type='string',
                      dest="file", default=None,
                      help="specify json configuration file")

    parser.add_option("-m", "--module", action="store", type='string',
                      dest="module", default=None,
                      help="specify configuration module name")

    parser.add_option("-k", "--key", action="store", type='string',
                      dest="key", default=None,
                      help="specify configuration key name")

    parser.add_option("-v", "--value", action="store", type='string',
                      dest="value", default=None,
                      help="specify configuration value")

    parser.add_option("-o", "--online", action="store_true",
                      dest="online", default=False,
                      help="Online set configurations")

    (options, args) = parser.parse_args()

    ret = 0
    if options.module is None and options.key is None and options.value is None:
        if options.file is not None and options.online:
            ret = uvpconf.uvpSetGlobalOpts(options.file, "online")
        elif options.file is not None and not options.online:
            ret = uvpconf.uvpSetGlobalOpts(options.file, "offline")
        elif options.file is None and not options.online:
            cur = uvpconf.uvpGetGlobalOpts()
            print(json.dumps(cur, indent=3))
        else:
            parser.print_help(None)
            sys.exit(0)
    elif options.module is not None and options.key is not None and options.value is None:
        print(uvpconf.uvpGetGlobalParameters(options.module, options.key))
    elif options.module is not None and options.key is not None and options.value is not None:
        if options.file is None and options.online:
            ret = uvpconf.uvpSetGlobalParameters(options.module, options.key, options.value, 1)
        elif options.file is None and not options.online:
            ret = uvpconf.uvpSetGlobalParameters(options.module, options.key, options.value, 0)
        else:
            parser.print_help(None)
            sys.exit(0)
    else:
        parser.print_help(None)
        sys.exit(0)
    sys.exit(ret)
