# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2019-2023 NV Access Limited
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.

import sys

Import("env", "outputDir", "sourceDir", "userDocsDir")

env = env.Clone()

devDocsOutputDir = outputDir.Dir("devDocs")

# Build the developer guide and move it to the output directory
mdFile = env.File("developerGuide.md")
# first substitute some variables such as NvDA version and URL into the markdown file
mdFileSub = env.Substfile(
	target=mdFile.abspath.replace(".md", ".md.sub"),
	source=mdFile,
	SUBST_DICT={
		"NVDA_VERSION": env["version"],
	},
)
htmlFile = env.Command(
	target=mdFile.abspath.replace(".md", ".html"),
	source=mdFileSub,
	action=[f'@"{sys.executable}" source/md2html.py -t developerGuide "$SOURCE" "$TARGET"'],
)
devGuide = env.Command(
	target=userDocsDir.File("developerGuide.html"), source=htmlFile, action=Move("$TARGET", "$SOURCE")
)
env.Alias("developerGuide", devGuide)
# Return the developer docs targets so they can be used as dependencies
Return(["devGuide"])

devDocs_nvdaHelper_temp = env.Doxygen(source="../../../nvdaHelper/doxyfile")
devDocs_nvdaHelper = env.Command(
	target=devDocsOutputDir.Dir("nvdaHelper"),
	source=devDocs_nvdaHelper_temp,
	action=Move("$TARGET", "$SOURCE"),
)
env.Alias("devDocs_nvdaHelper", devDocs_nvdaHelper)
env.Clean("devDocs_nvdaHelper", devDocs_nvdaHelper)

ignorePaths = [
	"_buildVersion.py",
	"comInterfaces",
	"images",
	"lib",
	"lib64",
	"libArm64",
	"locale",
	"louis",  # Not our project
	"typelibs",
	"waves",
	"mathType.py",  # Fails when not installed
	"oleTypes.py",  # Not our code
	"setup.py",  # Py2exe
]

sphinxAPIDocs = env.Command(
	"api",
	sourceDir,
	[
		[
			sys.executable,
			"-m",
			"sphinx.ext.apidoc",
			# "--force",  # overwrite existing files
			"-P",  # Include private modules
			"--module-first",  # put module documentation before submodule documentation
			"--output-dir",
			"$TARGET",
			"$SOURCE",  # Module sources
		]
		+ [f"{sourceDir}\\{f}" for f in ignorePaths]
	],
)
sphinxHtml = env.Command(
	"_build",
	sphinxAPIDocs,
	[
		[
			sys.executable,
			"-m",
			"sphinx.cmd.build",
			"-M",
			"html",
			"projectDocs/dev/developerGuide",  # Source directory
			"$TARGET",  # Build directory
		]
	],
)
devDocs_nvda = env.Command(devDocsOutputDir.Dir("NVDA"), sphinxHtml, Move("$TARGET", "$SOURCE"))
env.Alias("devDocs", [devGuide, devDocs_nvda])
env.Clean("devDocs", [devGuide, devDocs_nvda])
