#!/usr/bin/python3

import argparse
from pathlib import Path


directorys: list[str] = [
    "/etc/netplan/",
    "/etc/NetworkManager/system-connections/",
    "/var/lib/NetworkManager/user-connections/",
]


def search_string_in_directory(
    directories: list[str] = directorys,  # Use the predefined list as default
    search_string: str = "",  # Default is empty string
) -> bool:

    if len(search_string) == 0:
        return False

    result = False
    for directory in directories:
        in_paths = Path(directory)
        if not in_paths.exists() or not in_paths.is_dir():
            continue

        files = [file for file in in_paths.iterdir() if file.is_file()]
        if not files:
            continue

        # Search for the string in each file
        for file in files:
            try:
                with open(file, "r", errors="ignore") as f:
                    for line in f:
                        if search_string in line:
                            result = True  # String found
                            break
                    if result:
                        break  # No need to check further
            except Exception:
                continue  # Skip files that cause errors

    return result


def main() -> None:
    parser = argparse.ArgumentParser(
        description="Script only for use to compare the private key in the"
        "Network configurations to avoid errors with the network manager."
    )
    parser.add_argument("search_string", help="Search string")
    args = parser.parse_args()

    result = search_string_in_directory(search_string=args.search_string)
    print(result)


if __name__ == "__main__":
    main()