"""We need to run Sphinx inside MantidPlot to document the internal
   module. This script calls the sphinx entry point with the necessary
   arguments
"""
import os
import sys

def main():
    # Update path to find mantid
    package_dir = "@CMAKE_LIBRARY_OUTPUT_DIRECTORY@"
    runtime_config = os.environ.get("RUNTIME_CONFIG", "") # for visual studio and xcode
    if runtime_config != "":
        package_dir = os.path.join(package_dir, runtime_config)
    sys.path.insert(0, package_dir)

    # Update environment with screenshots path
    screenshots_dir = "@SCREENSHOTS_DIR@"
    if screenshots_dir != "":
        os.environ["SCREENSHOTS_DIR"] = screenshots_dir

    builder = "@BUILDER@"
    src_dir = "@SPHINX_SRC_DIR@"
    sphinx_build_dir = "@SPHINX_BUILD_DIR@"
    output_dir = os.path.join(sphinx_build_dir, builder)
    doctree_dir = os.path.join(sphinx_build_dir, "doctrees", builder)

    # See if we have been told to only process a particular file
    src_file = os.environ.get("SPHINX_SRC_FILE", None)
    if src_file:
        # Use a dedicated doctree directory for the given file. This is so that
        # tests can be run in parallel by ctest without stepping on
        # each others' toes.
        doctree_dir = os.path.join(doctree_dir, os.path.split(src_file)[1])

    import sphinx
    argv = [sys.executable, "-b", builder, "-d", doctree_dir, src_dir, output_dir]
    if src_file is not None:
        argv.append(src_file)

    # run
    sys.exit(sphinx.main(argv))

##################################################################################

if __name__ == "__main__":
    main()