#!/usr/bin/env python3

import csv
import re
import socket


def parse_bash_vars(path):
    """
    Parses a bash script and returns a dictionary representing the
    variables declared in that script.

    Source: https://dev.to/htv2012/how-to-parse-bash-variables-b4f

    :param path: The path to the bash script
    :return: Variables as a dictionary
    """
    with open(path) as stream:
        contents = stream.read().strip()
    var_declarations = re.findall(
        r"^[a-zA-Z0-9_]+=.*$", contents, flags=re.MULTILINE
    )
    reader = csv.reader(var_declarations, delimiter="=")
    bash_vars = dict(reader)
    return bash_vars


try:
    os_info = parse_bash_vars("/etc/os-release")

except IOError:
    os_info = None

try:
    fqdn = socket.getfqdn()
    hostname = fqdn.split('.')[0]
except OSError:
    host_info = None
else:

    domainname = '.'.join([x for x in fqdn.split('.')[:1]])
    hostname = fqdn.split('.')[0]
    instance, location, environment, function, number = hostname.split('-')
    service = fqdn.split('.')[1]

    host_info = {
        "domainname": domainname,
        "environment": environment,
        "fqdn": fqdn,
        "function": function,
        "hostname": hostname,
        "instance": instance,
        "location": location,
        "number": number,
        "service": service,
    }


modulesfile: str = "/etc/puppet/cosmos-modules.conf"
modules: dict = {
    "concat": {
        "repo": "https://github.com/SUNET/puppetlabs-concat.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "stdlib": {
        "repo": "https://github.com/SUNET/puppetlabs-stdlib.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "cosmos": {
        "repo": "https://github.com/SUNET/puppet-cosmos.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "ufw": {
        "repo": "https://github.com/SUNET/puppet-module-ufw.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "apt": {
        "repo": "https://github.com/SUNET/puppetlabs-apt.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "vcsrepo": {
        "repo": "https://github.com/SUNET/puppetlabs-vcsrepo.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "hiera-gpg": {
        "repo": "https://github.com/SUNET/hiera-gpg.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "augeas": {
        "repo": "https://github.com/SUNET/puppet-augeas.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "bastion": {
        "repo": "https://github.com/SUNET/puppet-bastion.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "dhcp": {
        "repo": "https://github.com/SUNET/puppetlabs-dhcp.git",
        "upgrade": "yes",
        "tag": "sunet_dev-2*",
    },
    "apparmor": {
        "repo": "https://github.com/SUNET/puppet-apparmor.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "docker": {
        "repo": "https://github.com/SUNET/garethr-docker.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "network": {
        "repo": "https://github.com/SUNET/attachmentgenie-network.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
    "sunet": {
        "repo": "https://github.com/SUNET/puppet-sunet.git",
        "upgrade": "yes",
        "tag": "stable-2023v1-2*",
    },
    "nagioscfg": {
        "repo": "https://github.com/SUNET/puppet-nagioscfg.git",
        "upgrade": "yes",
        "tag": "sunet-2*",
    },
}

# When/if we want we can do stuff to modules here
if os_info:
    if os_info["VERSION_CODENAME"] == "bullseye":
        pass
if host_info:
    if host_info["environment"] == "test":
        modules["sunet"]["tag"] = "testing-2*"

with open(modulesfile, "w") as fh:
    for key in modules:
        fh.write(
            "{0:11} {1} {2} {3}\n".format(
                key,
                modules[key]["repo"],
                modules[key]["upgrade"],
                modules[key]["tag"],
            )
        )