#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             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.

# Example outputs from various systems:
# <<<lnx_distro:sep(124)>>>
# /etc/debian_version|wheezy/sid

# <<<lnx_distro:sep(124)>>>
# /etc/lsb-release|DISTRIB_ID=Ubuntu|DISTRIB_RELEASE=12.10|DISTRIB_CODENAME=quantal|DISTRIB_DESCRIPTION="Ubuntu 12.10"

# <<<lnx_distro:sep(124)>>>
# /etc/redhat-release|Red Hat Enterprise Linux Server release 6.5 (Santiago)

# <<<lnx_distro:sep(124)>>>
# /etc/redhat-release|Oracle VM server release x.x.x

# <<<lnx_distro:sep(124):persist(1399310551)>>>
# /etc/SuSE-release|SUSE Linux Enterprise Server 11 (x86_64)|VERSION = 11|PATCHLEVEL = 2

# <<<lnx_distro:sep(124):persist(1490598851)>>>
# /etc/oracle-release|Oracle LinuxServer release 7.1


def inv_lnx_distro(info):
    parsed = {}
    for line in info:
        parsed.setdefault(line[0], line[1:])

    node = inv_tree("software.os.")
    node["type"] = "Linux"
    for file_name, handler in [
        ("/usr/share/cma/version", inv_lnx_parse_cma),
        ("/etc/gentoo-release", inv_lnx_parse_gentoo),
        ("/etc/SuSE-release", inv_lnx_parse_suse),
        ("/etc/oracle-release", inv_lnx_parse_oracle_vm_server),
        ("/etc/redhat-release", inv_lnx_parse_redhat),
        ("/etc/lsb-release", inv_lnx_parse_lsb),
        ("/etc/debian_version", inv_lnx_parse_debian),
    ]:
        if file_name in parsed:
            handler(node, parsed[file_name])
            break


def inv_lnx_parse_suse(node, line):
    version = line[1].split()[-1]
    if len(line) >= 3:
        patchlevel = line[2].split()[-1]
    else:
        patchlevel = "0"

    node["vendor"] = "SuSE"
    node["version"] = "%s.%s" % (version, patchlevel)
    node["name"] = "%s.%s" % (line[0].split('(')[0].strip(), patchlevel)

    if node["version"] == "11.2":
        node["code_name"] = "Emerald"
    elif node["version"] == "11.3":
        node["code_name"] = "Teal"
    elif node["version"] == "11.4":
        node["code_name"] = "Celadon"
    elif node["version"] == "12.1":
        node["code_name"] = "Asparagus"
    elif node["version"] == "12.2":
        node["code_name"] = "Mantis"
    elif node["version"] == "12.3":
        node["code_name"] = "Darthmouth"
    elif node["version"] == "13.1":
        node["code_name"] = "Bottle"


def inv_lnx_parse_redhat(node, line):
    entry = line[0]
    if entry.startswith("Oracle"):
        inv_lnx_parse_oracle_vm_server(node, line)
    else:
        parts = entry.split("(")
        left = parts[0].strip()
        node["code_name"] = parts[1].rstrip(")")
        name, _release, version = left.rsplit(None, 2)
        if name.startswith("Red Hat"):
            node["vendor"] = "Red Hat"
        node["version"] = version
        node["name"] = left


def inv_lnx_parse_oracle_vm_server(node, line):
    parts = line[0].split(" ")
    node["vendor"] = parts.pop(0)
    node["version"] = parts.pop(-1)
    node["name"] = " ".join(parts[:-1])


def inv_lnx_parse_lsb(node, line):
    for entry in line:
        varname, value = entry.split("=", 1)
        value = value.strip("'").strip('"')
        if varname == "DISTRIB_ID":
            node["vendor"] = value
        elif varname == "DISTRIB_RELEASE":
            node["version"] = value
        elif varname == "DISTRIB_CODENAME":
            node["code_name"] = value.title()
        elif varname == "DISTRIB_DESCRIPTION":
            node["name"] = value


# Do not overwrite Ubuntu information
def inv_lnx_parse_debian(node, line):
    entry = line[0]
    node["name"] = "Debian " + entry
    node["vendor"] = "Debian"
    node["version"] = entry
    if entry.startswith("2.0."):
        node["code_name"] = "Hamm"
    elif entry.startswith("2.1."):
        node["code_name"] = "Slink"
    elif entry.startswith("2.2."):
        node["code_name"] = "Potato"
    elif entry.startswith("3.0."):
        node["code_name"] = "Woody"
    elif entry.startswith("3.1."):
        node["code_name"] = "Sarge"
    elif entry.startswith("4."):
        node["code_name"] = "Etch"
    elif entry.startswith("5."):
        node["code_name"] = "Lenny"
    elif entry.startswith("6."):
        node["code_name"] = "Squeeze"
    elif entry.startswith("7."):
        node["code_name"] = "Wheezy"
    elif entry.startswith("8."):
        node["code_name"] = "Jessie"


def inv_lnx_parse_cma(node, line):
    node["name"] = "Check_MK Appliance " + line[1]
    node["vendor"] = "Mathias Kettner GmbH"
    node["version"] = line[1]
    if "code_name" in node:
        del node["code_name"]


def inv_lnx_parse_gentoo(node, line):
    entry = line[0]
    node["name"] = entry
    node["vendor"] = "Gentoo"
    parts = entry.split(" ")
    node["version"] = parts.pop(-1)
    if "code_name" in node:
        del node["code_name"]


inv_info['lnx_distro'] = {
   "inv_function": inv_lnx_distro,
}
