#!/usr/bin/env python3

import sys
from pathlib import Path


REQUIRED_FIELDS = (
    "Submitted By",
    "Date",
    "Initial Package Version",
    "Upstream Status",
    "Origin",
    "Description",
)


def read_fields(path):
    fields = {}
    for line in path.read_text(errors="replace").splitlines():
        for field in REQUIRED_FIELDS:
            prefix = f"{field}:"
            if line.startswith(prefix):
                fields.setdefault(field, line[len(prefix) :].strip())
    return fields


def main(argv):
    if len(argv) != 2:
        print("Info: go-utils 0.5.0")
        print("ERROR: Patch File not specified.")
        print(f"Usage: {Path(argv[0]).name} file.patch")
        return 1

    patch_file = Path(argv[1])
    if not patch_file.is_file():
        print("ERROR: Patch File not found")
        return 1

    fields = read_fields(patch_file)
    errors = 0
    for field in REQUIRED_FIELDS:
        value = fields.get(field, "")
        if value:
            print(f"{field}: {value}")
        else:
            print(f"{field} missing!")
            errors = 1

    return errors


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