#!/usr/bin/python
"""mkcheck generates a check plugin template or empty man page and prints it to stdout."""

import sys
import argparse
import time


def header():
    return r"""#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner {year}             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

""".format(year=time.gmtime(time.time())[0])


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()
