#!/usr/bin/env python
# automatically generated by auxilium
import sys
from pathlib import Path


def main() -> int:
    company = "{{ aux.project.author }}"
    license = "{{ aux.project.license }}"
    marker = "Copyright"
    actice_years = "{{ aux.project.active_years }}"
    company_copyright = f"# {marker} (c) {actice_years} {company}; License: {license}"
    skip = [company_copyright, "automatically generated"]

    for file in sys.argv[1:]:
        path = Path(file)
        if path.suffix in [".py", ".sh"]:
            with path.open("r", encoding="utf8") as f:
                all_lines = f.readlines()
                first_line, second_line = (all_lines + ["", ""])[:2]
                if any(part in first_line for part in skip):
                    continue
                if any(part in second_line for part in skip):
                    continue

                msg = f"copyright-line not found in the first two lines of '{path.name}', fixed it!"
                print(msg)

                # make sure it is not somehow misspelled
                if marker in first_line and company in first_line:
                    all_lines[0] = company_copyright + "\n"
                elif marker in second_line and company in second_line:
                    all_lines[1] = company_copyright + "\n"
                else:
                    insert_loc = 0
                    if first_line.startswith("#!"):
                        insert_loc = 1

                    all_lines.insert(insert_loc, company_copyright + "\n")

            with path.open("w") as f:
                f.writelines(all_lines)

    return 0


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