# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
import os

Import('BuildEnv')
env = BuildEnv.Clone()

cd_cmd = 'cd ' + Dir('.').path + ' && '

repo_top = env['api_repo_path']

autogen_infra_sources = [
                 repo_top + '/generateds/generateDS.py',
                 repo_top + '/generateds/TypeGenerator.py',
                 repo_top + '/generateds/ccmap.py',
                 repo_top + '/generateds/idl_parser.py',
                 repo_top + '/generateds/ifmap_global.py',
                 repo_top + '/generateds/ifmap_model.py',
                 repo_top + '/generateds/ifmap_frontend.py',
                ]

autogen_sources = [
                 repo_top + '/schema/all_cfg.xsd',
                 repo_top + '/schema/vnc_cfg.xsd',
                 repo_top + '/schema/bgp_schema.xsd',
                 repo_top + '/schema/ietf-l3vpn-schema.xsd',
                 repo_top + '/schema/smi-base.xsd',
                ]

autogen_script = File(repo_top + '/generateds/generateDS.py').path
autogen_cmd = '%s -f -o %s -g ifmap-frontend' % (autogen_script, Dir('vnc_api/gen/resource').path)

generated_files = [ 
                  'vnc_api/gen/__init__.py',
                  'vnc_api/gen/resource_xsd.py',
                  'vnc_api/gen/resource_common.py',
                  'vnc_api/gen/resource_client.py',
                  'vnc_api/gen/vnc_api_client_gen.py',
                  'vnc_api/gen/contrail_openapi.rst',
                  'vnc_api/gen/contrail_openapi.json',
                ]
env.Append(ENV = {'HEAT_BUILDTOP' : Dir(env['TOP']).abspath})

generated_rule = env.Command(generated_files,
                             repo_top + '/schema/all_cfg.xsd',
                             autogen_cmd + ' $SOURCE')

setup_sources = [
           'setup.py',
           '.testr.conf',
           '.coveragerc',
           'run_tests.sh',
           'tools',
           'tests',
           'requirements.txt',
           'test-requirements.txt',
          ]

setup_sources_rules = []
for file in setup_sources:
    setup_sources_rules.append(env.Install("", repo_top + "/api-lib/" + file))

remote_setup_sources = [
           repo_top+ '/base/version.info'
          ]

remote_setup_sources_rules = []
for file in remote_setup_sources:
    remote_setup_sources_rules.append(env.Install("vnc_api", file))

local_sources = [
           '__init__.py',
           'vnc_api.py',
           'vnc_api_lib.ini',
           'utils.py',
           'ssl_adapter.py',
           'exceptions.py',
          ]

local_sources_rules = []
for file in local_sources:
    local_sources_rules.append(env.Install("vnc_api", file))

generateds_rule = env.Install("vnc_api/gen", "generatedssuper.py")
cfixture_rule = env.Install("vnc_api/gen", "cfixture.py")

sdist_depends = [generated_rule, generateds_rule, cfixture_rule]
sdist_depends.extend(setup_sources_rules)
sdist_depends.extend(remote_setup_sources_rules)
sdist_depends.extend(local_sources_rules)
sdist_gen = env.Command('dist/vnc_api-{}.tar.gz'\
                        .format(open(File(repo_top + '/base/version.info').abspath, 'r')\
                        .read().strip('\n').strip('\t')),
                        'setup.py', cd_cmd + 'python setup.py sdist')

env.Default(sdist_gen)

# generated files depend on autogeneration infra parts
env.Depends(generated_rule, [autogen_infra_sources, autogen_sources, generateds_rule])
env.Depends(local_sources_rules, autogen_infra_sources)

# install everything before building distribution
env.Depends(sdist_gen, sdist_depends)

if 'install' in BUILD_TARGETS:
   install_cmd = env.Command(None, 'setup.py',
                             cd_cmd + 'python setup.py install %s' %
                             env['PYTHON_INSTALL_OPT'])
   env.Depends(install_cmd, sdist_depends)
   env.Alias('install', install_cmd)

env.Alias('install', env.Install(env['INSTALL_CONF'], 'vnc_api_lib.ini'))

env.SetupPyTestSuite(sdist_gen)

# Local Variables:
# mode: python
# End:
