#!/usr/bin/python3

import os.path
import re
import sys
import yaml

PKGNAME = os.environ['SNAP_NAME']
_CONFIG_DEFAULTS = os.path.join(os.environ["SNAP_APP_DATA_PATH"], "etc/default/ufw")
_CONFIG_UFW = os.path.join(os.environ["SNAP_APP_DATA_PATH"], "etc/ufw/ufw.conf")


def get_config():
    config = {"config": {PKGNAME: {}}}

    if not os.path.exists(_CONFIG_DEFAULTS):
        return config

    settable = ["IPV6", "MANAGE_BUILTINS"]
    with open(_CONFIG_DEFAULTS) as fp:
        for line in fp.readlines():
            for s in settable:
                if line.startswith('%s=' % s):
                    key, value = line.split('=', 1)
                    config["config"][PKGNAME][key.lower()] = value.strip().strip('"')

#     if not os.path.exists(_CONFIG_UFW):
#         return config
# 
#     settable = ["ENABLED", "LOGLEVEL"]
#     with open(_CONFIG_UFW) as fp:
#         for line in fp.readlines():
#             for s in settable:
#                 if line.startswith('%s=' % s):
#                     key, value = line.split('=', 1)
#                     config["config"][PKGNAME][key.lower()] = value.strip().strip('"')

    # print(config)
    return config


def set_config(config=None):
    if config is None:
        return

    with open(_CONFIG_DEFAULTS) as fp:
        lines = fp.readlines()
    fp.close()

    settable = []
    for k in config["config"][PKGNAME].keys():
        settable.append(k.upper())

    with open(_CONFIG_DEFAULTS, "w") as fp:
        for line in lines:
            key = None
            found = False
            for key in settable:
                if line.startswith('%s=' % key):
                    value = config["config"][PKGNAME][key.lower()]
                    if value != "yes" and value != "no":
                        print("ERROR: '%s' should be 'yes' or 'no'. Skipping.")
                        continue
                    found = True
                    fp.write("%s=\"%s\"\n" % (key, value))
            if not found:
                fp.write(line)

#     with open(_CONFIG_UFW) as fp:
#         lines = fp.readlines()
#     fp.close()
# 
#     valid_loglevels = ['off', 'on', 'low', 'medium', 'high']
# 
#     settable = []
#     for k in config["config"][PKGNAME].keys():
#         settable.append(k.upper())
# 
#     with open(_CONFIG_UFW, "w") as fp:
#         for line in lines:
#             key = None
#             found = False
#             for key in settable:
#                 if line.startswith('%s=' % key):
#                     value = config["config"][PKGNAME][key.lower()]
#                     if key != "LOGLEVEL" and value != "yes" and value != "no":
#                         print("ERROR: '%s' should be 'yes' or 'no'. Skipping." % key.lower())
#                         continue
#                     if key == "LOGLEVEL" and value not in valid_loglevels:
#                         print("ERROR: '%s' should be one of: %s. Skipping." % (key.lower(), ", ".join(valid_loglevels)))
#                         continue
#                     found = True
#                     fp.write("%s=\"%s\"\n" % (key, value))
#             if not found:
#                 fp.write(line)

if __name__ == "__main__":
    config_yaml = yaml.load(sys.stdin)
    if config_yaml is not None:
        # print("DEBUG: reading stdin")
        set_config(config_yaml)
    yaml.dump(get_config(), stream=sys.stdout, default_flow_style=False)
