#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
env = BuildEnv.Clone()
repo_top = env['api_repo_path']

import os

doc_sources_rules = []

sphinxapidocs = ['gen.rst', 'modules.rst']
if any(os.access(os.path.join(path, 'sphinx-apidoc'), os.X_OK)
        for path in os.environ["PATH"].split(os.pathsep)):
    sphinx_cmd = (
        'SPHINX_APIDOC_OPTIONS="members,show-inheritance" sphinx-apidoc -f -o '
        '%s %s %s' % (
            Dir('.').path,
            Dir('%s/api-lib/vnc_api/gen/' % env['TOP']).path,
            Dir('.').path,
        )
    )
    apidoc_rule = env.Command(sphinxapidocs, '', sphinx_cmd)
    env.Depends(apidoc_rule,
                File('%s/api-lib/vnc_api/gen/contrail_openapi.rst' % env['TOP']))

    userdoc_rule = env.Install(
        Dir('.'), Glob('#src/contrail-api-client/api-lib/doc/source/*'))
    schema_rule = env.Install(
        Dir('.'), Glob('#src/contrail-api-client/schema/*.xsd'))
    gendoc_rule = env.Install(
        Dir('./source'), Glob('%s/api-lib/vnc_api/gen/contrail_openapi.rst' % env['TOP']))
    openapi_rule = env.Install(
        Dir('./source'), Glob('%s/api-lib/vnc_api/gen/contrail_openapi.json' % env['TOP']))
    version_rule = env.Install(
        Dir('.'), Glob('#src/contrail-api-client/base/version.info'))

    html_gen_rule = env.Command('contrail-config', ['source/Makefile', 'source/conf.py'],
                                'make -C %s/source html' % Dir('.').path)
    env.Depends(
        html_gen_rule,
        [apidoc_rule, gendoc_rule, userdoc_rule, schema_rule, openapi_rule, version_rule],
    )

    doc_sources_rules.append(env.Install("../vnc_api", Dir('.')))
    env.Depends(doc_sources_rules, html_gen_rule)

env.Requires(doc_sources_rules, File('%s/api-lib/vnc_api/vnc_api.py' % env['TOP']))

if 'install' in BUILD_TARGETS:
    env.Alias('install',
              env.Install('%s/contrail-config' % env['INSTALL_DOC'],
                          doc_sources_rules))

Return('doc_sources_rules')
