"""Dynamically write project files.

This script is used to create the following files 
    * pyproject.toml
    * README.rst
    * sphinx/index.rst (which is used to generate README.html)
    * sphinx/reverse_sphinx_path.py
    * .readthedocs.yaml
dynamically to avoid having to hard-code global constants.
"""
from turboctl import global_constants


# This note is added to the beginning of each created file.
note = """{comment_symbol} Note: This file was automatically generated by make_files.py.
{comment_symbol} To make changes to this file, edit that instead of this.
"""


# pyproject.toml
# Version can be made dynamic so we don't need to set it here;
# author and URL can't.
pyproject = note.format(comment_symbol='#') + f"""
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "turboctl"
description = "TurboCtl is a RS-232 controller for a Leybold TURBOVAC i or iX turbovacuum pump."
keywords = ["RS-232", "Turbovac"]
readme = "README.rst"
authors = [
    {{name = "{global_constants.AUTHOR}"}},
]
requires-python = ">=3.10"
dependencies = ["pyserial", "tabulate"]
license = {{text = "GPL-3.0-or-later"}}
dynamic = ["version"]

[project.optional-dependencies]
urwid = ["urwid"]
test = ["hypothesis"]

[project.urls]
homepage = "{global_constants.GITHUB_URL}"

[project.scripts]
turboctl = "turboctl.__main__:main"

[tool.setuptools.packages.find]

[tool.setuptools.dynamic]
version = {{attr = "turboctl.VERSION"}}
"""


# This body is used for both README.rst and sphinx/index.rst; the only
# difference between those is the documentation section. 
readme_body = note.format(comment_symbol='..') + f"""
TurboCtl
========

TurboCtl is a RS-232 controller for a Leybold TURBOVAC i or iX turbovacuum
pump. It was written in Python 3 and is intended for Linux operating systems.

Documentation
-------------

{{documentation}}

Author
------

TurboCtl was written by {global_constants.AUTHOR}.
The author can be contacted at {global_constants.EMAIL}
(with the "ä" replaced by an "a").

Copyright © {global_constants.COPYRIGHT}.

GitHub
------

The GitHub home page of the TurboCtl project can be found
`here <{global_constants.GITHUB_URL}>`__.

Version
-------

This is version {global_constants.VERSION} of TurboCtl.

License
-------

TurboCtl 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, see https://www.gnu.org/licenses/.
"""
# Note the double underscores at the ends of links.
# With a regular single underscore the duplicate link names won't be accepted
# by PyPI.


# The documentation section for README.rst.
# This version displays correctly in GitHub and PyPI, which don't support
# Sphinx.
readme_raw_rst_docs = (f'You can view the TurboCtl documentation '
                       f'`here <{global_constants.DOCS_URL}>`__.')


# The documentation section for sphinx/index.rst, which includes a
# Sphinx-generated table of contents.
readme_sphinx_docs = """.. toctree::

   installation.rst
   usage.rst
   modules.rst
   errata.rst
   changelog.rst
"""


# sphinx/reverse_sphinx_path.py
# This file makes it possible for sphinx/conf.py to import turboctl without
# having to hard-code the path.
reverse_sphinx_path = note.format(comment_symbol='#') + f"""
REVERSE_SPHINX_PATH = {repr(global_constants.REVERSE_SPHINX_PATH)}"""


# .readthedocs.yaml
readthedocs = note.format(comment_symbol='#') + f"""
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

version: 2

build:
    os: ubuntu-22.04
    tools:
        python: "3.12"

sphinx:
    configuration: {global_constants.SPHINX_PATH}/conf.py

python:
    install:
        - requirements: {global_constants.SPHINX_PATH}/requirements.txt
"""


def main():
    """Write the files."""

    names_and_contents = {
        'pyproject.toml': pyproject,

        'README.rst': readme_body.format(documentation=readme_raw_rst_docs),

        global_constants.SPHINX_PATH + '/index.rst':
            readme_body.format(documentation=readme_sphinx_docs),

        global_constants.SPHINX_PATH + '/reverse_sphinx_path.py':
            reverse_sphinx_path,

        '.readthedocs.yaml': readthedocs}

    for name, contents in names_and_contents.items():
        with open(name, 'w') as file:
            file.write(contents)


if __name__ == '__main__':
    main()
