#!/usr/bin/python3

import argparse

if __name__ == "__main__":
    exceptions = ['\n\ndestination astra_json_dst {\n    file("/parsec/log/astra/events"\n         template("$(format-json --scope rfc5424 --exclude MESSAGE MSG.*)\\n")\n         group("astra-admin")\n         hook-commands(\n             setup("/usr/bin/astra-protect-event-log")\n         )\n         overwrite-if-older(2678400));\n};',
              '\n\ndestination astra_json_dst {\n    file("/parsec/log/astra/events"\n         template("$(format-json --scope rfc5424 --exclude MESSAGE MSG.*)\\n")\n         group("astra-admin")\n         hook-commands(\n            setup("/usr/bin/astra-protect-event-log")\n         )\n         overwrite-if-older(2678400));\n};',
              '\n\nlog {\n    source(astra_audit_src);\n    parser(astra_audit_parser);\n    if {\n        filter(astra_events_manipulation_filter);\n        destination(astra_events_log_control_dst);\n    } else {\n        destination(astra_json_dst);\n    };',
              '\n\nlog {\n    source(s_src);\n    filter(f_kern);\n    parser(astra_oom_parser);\n    destination(astra_json_dst);\n};',
              '\n\nsource astra_events_src {\n    file("/var/log/astra/events" flags(no-parse));\n};',
              '\n\nfilter astra_events_rotation_filter {\n    match("^/usr/sbin/logrotate$" value("MSG.astra-audit.exe"))\n};',
              '\n\nfilter astra_events_manipulation_filter {\n    (match("^events_log_removed$" value("MSG.astra-audit.message_id"))\n    or match("^events_log_renamed$" value("MSG.astra-audit.message_id")))\n    and not filter(astra_events_rotation_filter);\n};',
              '\n\ndestination astra_events_log_control_dst {\n    python(\n        class("syslog_ng_mod_astra.astra_syslog_ng_destination.AstraSyslogNgDestination")\n        options("destination_type", "log_control"\n                "log", "/var/log/astra/events"\n                "prefix", "astra-audit")\n        persist-name("events_log_control")\n    );\n};',
              '\n\ndestination astra_json_dst {\n    file("/var/log/astra/events"\n         template("$(format-json --scope rfc5424 --exclude MESSAGE MSG.*)\\n")\n         group("astra-admin")\n         overwrite-if-older(2678400));\n};',
              '\n\nlog {\n    source(astra_audit_src);\n    parser(astra_audit_parser);\n    channel { destination(astra_json_dst); };',
              '\n    channel { filter(astra_events_manipulation_filter); destination(astra_events_log_control_dst); };',
              '\n\ndestination astra_json_dst {\n    file("/var/log/astra/events"\n         template("$(format-json --scope rfc5424 --exclude MESSAGE MSG.*)\\n")\n         overwrite-if-older(2678400));\n};',
              '\n\nlog {\n    source(astra_audit_src);\n    parser(astra_audit_parser);\n    destination(astra_json_dst);\n};']

    parser = argparse.ArgumentParser(
                 prog = 'migrate-config',
                 description = 'Migrate syslog-ng-mod-astra configuration from ALSE 1.7.1 - 1.7.3 into 1.7.4 and later')
    parser.add_argument('-i', '--input', default="/etc/syslog-ng/conf.d/mod-astra.conf")
    parser.add_argument('-o', '--output', default="/etc/syslog-ng/conf.d/mod-astra.conf")
    args = parser.parse_args()

    default_config = "/usr/share/syslog-ng-mod-astra/mod-astra.conf"
    old_config = args.input
    result_config = args.output

    default_data = []
    old_data = []

    try:
        with open(result_config, "r") as file:
            content = file.read()
            if content.find("@include \"/usr/share/syslog-ng-mod-astra/mod-astra.conf\"") >= 0:
                exit(0)
    except:
        exit(0)

    try:
        with open(old_config, "r") as file:
            old_data = list(map(lambda x: x + "};", file.read().split("};")))
    except:
        exit(0)
    if not old_data:
        exit(0)

    try:
        with open(default_config, "r") as file:
            default_data = list(map(lambda x: x + "};", file.read().split("};")))
    except:
        exit(0)

    data = [ x for x in old_data if x not in default_data and x not in exceptions]
    try:
        with open(result_config, "w") as file:
            file.write("@include \"/usr/share/syslog-ng-mod-astra/mod-astra.conf\"\n")
            for line in data:
                file.write(line)
            file.write("\n")
    except:
        exit(0)
