"""
Generates a file that loads shared library dependencies for the module

Arguments: pyproject.toml package.name /path/to/_init_name.py
"""

from __future__ import annotations

import inspect
import pathlib
import sys
import typing as T

from ..autowrap.buffer import RenderBuffer


def _write_libinit_py(
    init_py: pathlib.Path,
    modules: T.List[str],
) -> None:
    """
    :param init_py: the _init module for the library(ies) that is written out
    :param modules: the python modules to import
    """

    r = RenderBuffer()

    r.writeln("# This file is automatically generated, DO NOT EDIT")
    r.writeln("# fmt: off")
    r.writeln()

    for mod in modules:
        r.writeln(f"import {mod}")

    init_py.write_text(r.getvalue(), encoding="utf-8")


def main():
    try:
        _, libinit_py = sys.argv[:2]
        modules = sys.argv[2:]
    except ValueError:
        print(inspect.cleandoc(__doc__ or ""), file=sys.stderr)
        sys.exit(1)

    _write_libinit_py(
        pathlib.Path(libinit_py),
        modules,
    )


if __name__ == "__main__":
    main()
