#!/usr/bin/python3
#
# Copyright (C) 2025 Michael Chernigin <chernigin@altlinux.org>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

import os
import sys
from pathlib import Path
import argparse

import alterator_entry as ae

CATEGORIES_DIR = Path("/usr/share/alterator/components/categories")

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("category_name")
    args = parser.parse_args()

    if not os.path.isdir(CATEGORIES_DIR):
        sys.exit(f"Error: category dir '{CATEGORIES_DIR}' not found")

    for file in os.listdir(CATEGORIES_DIR):
        if file.endswith(".category"):
            file_path = CATEGORIES_DIR / file
            name = ae.get_field(file_path, "name")
            if name == args.category_name:
                print(file_path.read_text(), end='')


if __name__ == "__main__":
    main()
