#!/usr/bin/python3
#
# Copyright (C) 2024 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 argparse
import os
import sys
from pathlib import Path
from typing import Optional


def get_localized_file(base_path: Path) -> Optional[str]:
    locale = os.getenv("LC_ALL", "en_US").split(".")[0]
    variants = [
        f"description.{locale}.html",
        f"description.{locale.split('_')[0]}.html",
        f"description.html",
    ]

    for variant in variants:
        try:
            return (base_path / variant).read_text()
        except FileNotFoundError:
            continue


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

    content = get_localized_file(Path(args.description_dir))

    if content is None:
        sys.exit("Erorr: description not found")

    print(content, end="")


if __name__ == "__main__":
    main()
