# Copyright (C) 2022 Radiotherapy AI Pty Ltd

# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You may
# obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.

import os
import subprocess
import pathlib

AUTOGEN_MESSAGE = """# DO NOT EDIT THIS FILE!
# This file has been autogenerated by Radiotherapy AI's regulatory
# documentation manager

"""


HERE = pathlib.Path(__file__)
REPO_ROOT = HERE.parents[1]
DOCS_DIR = REPO_ROOT / "docs"
TABLE_OF_CONTENTS_PATH = DOCS_DIR / "_toc.yml"


def build():
    env = os.environ.copy()

    with open(TABLE_OF_CONTENTS_PATH, "r+") as file:
        file.truncate(0)

        subprocess.check_call(
            [
                "jupyter-book",
                "toc",
                "from-project",
                str(DOCS_DIR),
                "-f",
                "jb-book",
            ],
            cwd=REPO_ROOT,
            env=env,
            stdout=file,
        )

        file.seek(0)
        content = file.read()

        file.seek(0)
        file.write(AUTOGEN_MESSAGE + content)

    subprocess.check_call(
        ["jupyter-book", "build", str(DOCS_DIR)], cwd=REPO_ROOT, env=env
    )
