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

Import('BuildEnv')

env = BuildEnv.Clone()
env.CppDisableExceptions()

env.Append(CPPPATH = env['TOP'])
env.Append(CPPPATH = [env['TOP'] + '/ifmap'])
env.Append(CPPPATH = [env['TOP'] + '/config-client-mgr'])
env.Append(CPPPATH = [env['TOP'] + '/base/sandesh'])
env.Append(CPPPATH = [env['TOP'] + '/database'])
env.Prepend(LIBS=['sandesh', 'http', 'http_parser', 'curl', 'io', 'base'])

except_env = BuildEnv.Clone()
except_env.Append(CPPPATH = [env['TOP'] + '/ifmap'])
except_env.Append(CPPPATH = [env['TOP'] + '/config-client-mgr'])
except_env.Append(CPPPATH = [env['TOP'] + '/base/sandesh'])
except_env.CppEnableExceptions()
except_env.Append(CPPPATH = env['TOP'])

SandeshGenFiles = env.SandeshGenCpp('ifmap_server_show.sandesh')
SandeshGenFiles += env.SandeshGenOnlyCpp('ifmap_server_show_internal.sandesh')
SandeshGenFiles += env.SandeshGenCpp('ifmap_log.sandesh')
SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)

sandesh_objs = []
for src in SandeshGenSrcs:
    objname = src.replace('.cpp', '.o')
    obj = except_env.Object(objname, src)
    sandesh_objs.append(obj)

ifmap_server = except_env.Object('ifmap_server.o', 'ifmap_server.cc')
ifmap_server_show = except_env.Object('ifmap_server_show.o', 'ifmap_server_show.cc')
ifmap_xmpp = except_env.Object('ifmap_xmpp.o', 'ifmap_xmpp.cc')
ifmap_xmpp_client_show = except_env.Object('ifmap_xmpp_client_show.o', 'ifmap_xmpp_client_show.cc')
AgentSandeshGenFiles  = env.SandeshGenCpp('ifmap_agent.sandesh')
AgentSandeshGenSrcs = env.ExtractCpp(AgentSandeshGenFiles)

libifmap_common = env.Library('ifmap_common',
                              ['ifmap_dependency_tracker.cc',
                               'ifmap_table.cc',
                               'ifmap_link.cc',
                               'ifmap_link_table.cc',
                               'ifmap_node.cc',
                               'ifmap_object.cc',
                               'ifmap_log.cc'] + sandesh_objs)

# control-node
libifmap = env.Library('ifmap_server',
                       ['ifmap_client.cc',
                        'ifmap_config_listener.cc',
                        'ifmap_encoder.cc',
                        'ifmap_exporter.cc',
                        'ifmap_factory.cc',
                        'ifmap_graph_walker.cc',
                        'ifmap_node_proxy.cc',
                        ifmap_server_show,
                        ifmap_server,
                        'ifmap_server_parser.cc',
                        'ifmap_server_table.cc',
                        'ifmap_update.cc',
                        'ifmap_update_queue.cc',
                        'ifmap_update_sender.cc',
                        'ifmap_util.cc',
                        'ifmap_uuid_mapper.cc',
                        ifmap_xmpp,
                        ifmap_xmpp_client_show,
                        ] + sandesh_objs)

# agent-module
libifmap_agent = env.Library('ifmap_agent',
                       [ 'ifmap_agent_parser.cc',
                        'ifmap_agent_table.cc',
                        'ifmap_agent_sandesh.cc',
                        ] + AgentSandeshGenSrcs)

test_suite = env.SConscript('client/SConscript', exports='BuildEnv',
                            duplicate = 0)

test_suite += env.SConscript('test/SConscript', exports='BuildEnv',
                             duplicate = 0)

def code_coverage(target, source, env):
    import shutil
    shutil.rmtree(target[0].path, ignore_errors = True)

    # lcov --base-directory $ROOT -- directory . --zerocounters -q
    import os
    os.system('lcov --base-directory . --directory ' + Dir('.').path +
              ' --zerocounters -q')
    # execute tests
    import subprocess
    ShEnv = {env['ENV_SHLIB_PATH']: 'build/lib'}

    for test in test_suite:
        cmd = test[0].path
        logfile = open(cmd + '.log', 'w')
        subprocess.call([cmd], stdout=logfile, env=ShEnv)

    # lcov --base-directory $ROOT -- directory . -c -o ifmap_test.info
    os.system('lcov --base-directory . --directory ' + Dir('.').path +
              ' -c -o ifmap_test.info')

    # genhtml -o ifmap/test_coverage ifmap_test.info
    os.system('genhtml -o ' + target[0].path +
              ' -t "test coverage" --num-spaces 4 ifmap_test.info')

if env['OPT'] == 'coverage':
    test_coverage = env.Command(Dir('test_coverage'), '', code_coverage)
    env.AlwaysBuild(test_coverage)
    env.Alias('src/ifmap:test_coverage', test_coverage)
