#!/usr/bin/python3

import argparse
from pathlib import Path

if __name__ == '__main__':
    exceptions = [
        '\n\n'
        'destination 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\n'
        'destination 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\n'
        'log {\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\n'
        'log {\n'
        '    source(s_src);\n'
        '    filter(f_kern);\n'
        '    parser(astra_oom_parser);\n'
        '    destination(astra_json_dst);\n'
        '};',

        '\n\n'
        'source astra_events_src {\n'
        '    file("/var/log/astra/events" flags(no-parse));\n'
        '};',

        '\n\n'
        'filter astra_events_rotation_filter {\n'
        '    match("^/usr/sbin/logrotate$" value("MSG.astra-audit.exe"))\n'
        '};',

        '\n\n'
        'filter 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\n'
        'destination 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\n'
        'destination 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\n'
        'log {\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\n'
        'destination 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\n'
        'log {\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_path = Path('/usr/share/syslog-ng-mod-astra/mod-astra.conf')
    old_config = Path(args.input)
    result_config = Path(args.output)

    default_data = []
    old_data = []

    try:
        if f'@include "{default_config_path}"' in result_config.read_text():
            exit(0)
    except:
        exit(0)

    try:
        old_data = list(map(lambda x: x + '};', old_config.read_text().split('};')))
    except:
        exit(0)

    if not old_data:
        exit(0)

    try:
        default_data = list(map(lambda x: x + '};', default_config_path.read_text().split('};')))
    except:
        exit(0)

    data = [x for x in old_data if x not in default_data and x not in exceptions]
    try:
        with result_config.open('w') as file:
            file.write(f'@include "{default_config_path}"\n')
            for line in data:
                file.write(line)
            file.write('\n')
    except:
        exit(0)
