from buildutils import *

Import('env', 'build', 'install')

localenv = env.Clone()

if localenv['doxygen_docs']:
    if localenv['graphvizdir']:
        localenv.Append(PATH=localenv['graphvizdir'])

    build(localenv.Command('#build/docs/doxygen/html/index.html',
                           'doxygen/Doxyfile', 'doxygen $SOURCE'))
    install(pjoin('$inst_docdir', 'doxygen/html'),
            mglob(localenv, '#/build/docs/doxygen/html', 'html', 'svg', 'css', 'png'))

if localenv['sphinx_docs']:
    localenv['SPHINXBUILD'] = Dir('#build/docs/sphinx')
    localenv['SPHINXSRC'] = Dir('sphinx')

    sphinxdocs = build(localenv.Command('${SPHINXBUILD}/html/index.html',
        'sphinx/conf.py',
        '${sphinx_cmd} -b html -d ${SPHINXBUILD}/doctrees ${SPHINXSRC} ${SPHINXBUILD}/html'))

    # Python examples: Create individual documentation pages with the source
    # for each example
    example_root = Dir('#interfaces/cython/cantera/examples').abspath
    for subdir in subdirs(example_root):
        for f in mglob(env, pjoin(example_root, subdir), 'py'):
            tmpenv = env.Clone()
            tmpenv['script_name'] = f.name
            tmpenv['script_path'] = '../../../../interfaces/cython/cantera/examples/%s/%s' % (subdir, f.name)
            b = tmpenv.SubstFile('#doc/sphinx/cython/examples/%s_%s.rst' % (subdir, f.name[:-3]),
                                 '#doc/sphinx/cython/examples/example-script.rst.in')
            build(b)
            localenv.Depends(sphinxdocs, b)

    # Matlab examples: create individual documentation pages with the source
    # for each example
    for f in mglob(env, '#samples/matlab', 'm'):
        tmpenv = env.Clone()
        tmpenv['script_name'] = f.name
        tmpenv['script_path'] = '../../../../samples/matlab/%s' % f.name
        b = tmpenv.SubstFile('#doc/sphinx/matlab/examples/%s.rst' % f.name[:-2],
                             '#doc/sphinx/matlab/examples/example-script.rst.in')
        build(b)
        localenv.Depends(sphinxdocs, b)

    localenv.AlwaysBuild(sphinxdocs)
