#!/usr/bin/env python3

import sys
from re import match
from typing import List

import feedvalidator
from feedvalidator import compatibility
from feedvalidator.formatter.text_plain import Formatter


def main(argv):
    with open(argv[1], 'r', encoding='UTF-8') as fp:
        content = fp.read()

    result = feedvalidator.validateString(content)
    events = compatibility.AA(result['loggedEvents'])
    output = Formatter(events)

    # This always happens for non-URL feeds and can't be avoided.
    output = [
        i
        for i in output
        if not match(
            r'^line [0-9]+, column [0-9]+: Self reference doesn\'t match document location$',
            i,
        )
    ]

    if output:
        print('\n'.join(output))
        return 1

    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv))
