#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.
"""mkcheck generates a check plugin template or empty man page and prints it to stdout."""

import sys
import argparse


def header():
    return r"""#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

"""


def mkcheck_m():
    return """title:
agents:
catalog:
license: GPL
distribution: check_mk
description:

item:

inventory:
"""


def get_check_info_str(name, parse, snmp, decorate):
    if parse:
        parse_function_str = """
def parse_{name}(info):
    parsed = {{}}
    return parsed

""".format(name=name)

        parse_str = """
    'parse_function': parse_{name},""".format(name=name)

        info_str = "parsed"

    else:
        parse_function_str = ""
        parse_str = ""
        info_str = "info"

    if snmp:
        snmp_str = """
    'snmp_info': ('', []),
    'snmp_scan_function': lambda oid: True,"""

    else:
        snmp_str = ""

    if decorate:
        decorator_str = "@get_parsed_item_data\n"
        decorator_info_str = "data"
    else:
        decorator_str = ""
        decorator_info_str = info_str

    return """{parse_function}
def inventory_{name}({info}):
    return []


{decorator}def check_{name}(item, params, {decorator_info}):
    return 3, 'not yet implemented'


check_info['{name}'] = {{{parse}
    'inventory_function': inventory_{name},
    'check_function': check_{name},
    'service_description': 'DESCR',{snmp}
}}
""".format(name=name,
           decorator=decorator_str,
           decorator_info=decorator_info_str,
           info=info_str,
           parse=parse_str,
           parse_function=parse_function_str,
           snmp=snmp_str)


def parse_arguments(argv):
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument("--snmp", action="store_true", help="Add SNMP scan and info")
    parser.add_argument("--parse", action="store_true", help="Add parse function")
    parser.add_argument("--decorate", action="store_true", help="Decorate check function")
    parser.add_argument("--man", action="store_true", help="Man page of check plugin")
    parser.add_argument("name", help="Check plugin name")
    return parser.parse_args(argv)


def main():
    argv = sys.argv[1:]
    args = parse_arguments(argv)

    if args.man:
        check_info_str = mkcheck_m()
    elif args.name:
        check_info_str = header() + get_check_info_str(
            args.name, parse=args.parse, snmp=args.snmp, decorate=args.decorate)
    else:
        sys.stdout.write('Please specify a check plugin name.\n')
        sys.exit()

    sys.stdout.write(check_info_str)


if __name__ == "__main__":
    main()
