#!/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.

# .1.3.6.1.4.1.674.10892.5.4.300.50.1.8.1.1 2.5.2
# .1.3.6.1.4.1.674.10892.5.5.1.20.130.3.1.26.1 1.03
# .1.3.6.1.4.1.674.10892.5.1.3.2.0 JG2NS12
# .1.3.6.1.4.1.674.10892.5.1.3.3.0 42330791846


def _get_date_format(date):
    # Beware: Dell's actual definition of the format supposed
    # to be here is yyyymmddHHMMSS.uuuuuu+ooo. This has *never*
    # been observed in the wild. More accurate appears to be
    # mm/dd/yyyy or 0mm/dd/yyyy or mm/0dd/yyyy. The 0 represents a
    # random 0 thrown in for good measure :/
    try:
        if date[2] == "/" and date[5] == "/":  # mm/dd/yyyy
            return "%m/%d/%Y"
        elif date[3] == "/" and date[6] == "/":  # 0mm/dd/yyyy
            return "0%m/%d/%Y"
        elif date[2] == "/" and date[6] == "/":  # mm/0dd/yyyy
            return "%m/0%d/%Y"
        elif "/" not in date[:8]:  # In case of Dell devices following the MIB
            return "%Y%m%d"
        return
    except IndexError:
        return


def inv_dell_idrac_info(info):
    serial, expresscode, bios_date, bios_version, bios_vendor, raid_name, raid_version = info[0][0]

    node = inv_tree("hardware.system.")
    node["serial"] = serial
    node["expresscode"] = expresscode

    node = inv_tree("software.bios.")
    node["version"] = bios_version
    node["vendor"] = bios_vendor

    bios_date_fmt = _get_date_format(bios_date)
    if bios_date_fmt:
        node["date"] = time.mktime(time.strptime(bios_date, bios_date_fmt))

    node = inv_tree("hardware.storage.controller.")
    node["version"] = raid_version
    node["name"] = raid_name


inv_info["dell_hw_info"] = {
    "inv_function": inv_dell_idrac_info,
}
