#! /usr/bin/env python
"""modsim_template report workflow

Requires the following ``SConscript(..., exports={})``

* ``env`` - The SCons construction environment with the following required keys

  * ``SPHINX_BUILD`` - String path for the Sphinx build executable
  * ``regression_alias`` - String for the alias collecting the regression workflow targets
  * ``documentation_abspath`` - String absolute path to the modsim HTML documentation source files

* ``project_variables`` - The project variables for use in Sphinx documentation parameters
"""
import pathlib

import waves


# Inherit the parent construction environment
Import(["env", "project_variables"])

# Set empty workflow list
report = []

# Perform variable substitution on Sphinx configuration file
report.extend(
    env.Substfile(
        "conf.py.in",
        SUBST_DICT=env.SubstitutionSyntax(project_variables),
    )
)

# Copy project root files required by the documentation
root_files = [
    ("README.txt", "#/README.rst"),
]
for target, source in root_files:
    report.extend(
        env.Command(
            target=[target],
            source=[source],
            action=Copy("$TARGET", "$SOURCE", False),
        )
    )

# Copy documentation source files re-used in the report
documentation_abspath = env["documentation_abspath"]
report_copy_list = [
    documentation_abspath / "nominal.rst",
    documentation_abspath / "project_brief.txt",
    documentation_abspath / "zreferences.rst",
    documentation_abspath / "references.bib",
    documentation_abspath / "targets.txt",
    documentation_abspath / "simulation_description.txt",
    documentation_abspath / "simulation_description_short.txt",
    documentation_abspath / "simulation_material.txt",
    documentation_abspath / "rectangle_schematic.png",
    documentation_abspath / "stress_strain_comparison.png",
    documentation_abspath / "theory.txt",
    documentation_abspath / "mesh_convergence.rst",
    documentation_abspath / "mesh_convergence_stress.png",
    documentation_abspath / "mesh_convergence_conclusions.txt",
    documentation_abspath / "nominal_conclusions.txt",
]
report_copy_list = [pathlib.Path(source_file) for source_file in report_copy_list]
# Call waves copy_substfile directly as a function instead of a pseudo-builder.  Required to help the Sphinx scanner and
# SCons find ``build/report/README.txt`` instead of looking for ``docs/README.txt``.
report.extend(
    waves.scons_extensions.copy_substfile(
        Environment(),
        report_copy_list,
        substitution_dictionary=env.SubstitutionSyntax(
            project_variables,
        ),
    )
)

# PDF Report build task
targets = [f"latex/{project_variables['report_pdf']}"]
sources = ["index.rst"] + [source_file.name.rstrip(".in") for source_file in report_copy_list]
report.extend(
    env.SphinxPDF(
        target=targets,
        source=sources,
    )
)
env.Clean(report, [Dir("latex")] + sources)
env.Alias("report", report)
env.Alias(env["regression_alias"], report)

# Collector alias to build the PDF report
parent_directory = Dir(".").srcnode().name
env.Alias(parent_directory, report)
