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

# Example for Debian/Ubuntu
# <<<lnx_packages:sep(124)>>>
# zlib1g:amd64|1:1.2.7.dfsg-13|amd64|deb|compression library - runtime|install ok installed
# zlib1g:i386|1:1.2.7.dfsg-13|i386|deb|compression library - runtime|install ok installed
# zlib1g-dev:amd64|1:1.2.7.dfsg-13|amd64|deb|compression library - development|install ok installed

# Example for Gentoo
# sys-libs/ncurses|6.0-r1|amd64|ebuild|Repository gentoo|installed
# sys-libs/pam|1.2.1|amd64|ebuild|Repository gentoo|installed
# sys-libs/readline|6.3_p8-r3|amd64|ebuild|Repository gentoo|installed
# sys-libs/slang|2.3.0|amd64|ebuild|Repository gentoo|installed
# sys-libs/timezone-data|2016h|amd64|ebuild|Repository gentoo|installed
# sys-libs/zlib|1.2.11|amd64|ebuild|Repository gentoo|installed

# Example for RPM
# gpg-pubkey|307e3d54|(none)|rpm|gpg(SuSE Package Signing Key <build@suse.de>)|
# gpg-pubkey|1d061a62|(none)|rpm|gpg(build@novell.com (Novell Linux Products) <support@novell.com>)|
# licenses|20070810|noarch|rpm|License collection as found in the packages of SuSE Linux|
# branding-SLES|11|noarch|rpm|SUSE Linux Enterprise Server Brand File|
# terminfo|5.6|i586|rpm|A terminal descriptions database|
# yast2-schema|2.17.4|noarch|rpm|YaST2 - AutoYaST Schema|
# glibc-i18ndata|2.11.1|i586|rpm|Database Sources for 'locale'|
# cpio-lang|2.9|i586|rpm|Languages for package cpio|
# zlib|1.2.3|i586|rpm|Data Compression Library|


def inv_lnx_packages(info):
    parsed_packages = []
    for line in info:
        if len(line) == 6:
            pacname, version, arch, pactype, summary, inststate = line
            release = None
        elif len(line) == 7:
            pacname, version, arch, pactype, release, summary, inststate = line
        else:
            continue

        if pactype == "deb":
            if "installed" not in inststate:
                continue
        if arch == "amd64":
            arch = "x86_64"
        entry = {
            "name": pacname,
            "version": version,
            "arch": arch,
            "package_type": pactype,
            "summary": summary,
        }
        # Split version into version of contained software and version of the
        # packages (RPM calls the later "release")
        parts = version.rsplit("-", 1)
        if len(parts) == 2:
            version, package_version = parts
            entry["version"] = version
            entry["package_version"] = package_version
        if release is not None:
            entry["package_version"] = release
        parsed_packages.append(entry)

    paclist = inv_tree_list("software.packages:")
    paclist.extend(sorted(parsed_packages, key=lambda r: r.get('name')))


inv_info['lnx_packages'] = {
    "inv_function": inv_lnx_packages,
}
