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

Import('BuildEnv')
import platform
import os

def MapBuildDir(list):
    return map(lambda x: '#/' + Dir('.').path + '/../' + x, list)

env = BuildEnv.Clone()

env.Prepend(CPPPATH = env['TOP'])
env.Append(CPPPATH = [env['TOP'] + '/base/sandesh'])
env.Append(CPPPATH = [env['TOP'] + '/config-client-mgr'])
env.Prepend(CPPPATH = env['TOP'] + '/dns')
env.Prepend(CPPPATH = env['TOP'] + '/dns/cmn')
env.Prepend(CPPPATH = env['TOP'] + '/ifmap')
env.Prepend(CPPPATH = env['TOP'] + '/schema')
env.Prepend(CPPPATH = env['TOP'] + '/sandesh/library')
env.Prepend(CPPPATH = env['TOP'] + '/bgp')

etcd_incl = os.environ.get('CONTRAIL_ETCD_INCL');

if etcd_incl:
    env.Prepend(LIBS = ['etcdql', 'protobuf', 'grpc++', 'grpc', 'z'])

env.Prepend(LIBS=['ifmap_vnc', 'ifmap_server', 'ifmap_common', 'net', 'config_client_mgr',
                  'ifmapio', 'xmpp', 'sandeshvns', 'peer_sandesh',
                  'curl', 'process_info',
                  'db', 'io', 'base', 'cassandra_cql', 'SimpleAmqpClient', 'rabbitmq',
                  'cassandra', 'gendb', 'xml', 'pugixml', 'xml2',
                  'cpuinfo', 'nodeinfo',
                  'boost_regex', 'boost_program_options','crypto', 'ssl', 'bgp_schema'])

env.Append(LIBS=['boost_chrono'])

env.Prepend(LIBS=['dns_cfg', 'cmn', 'mgr', 'agent_xmpp', 'bind_interface', 'dns_uve'])
env.Append(LIBS=['sandesh', 'http', 'http_parser', 'boost_filesystem'])

if platform.system() not in ['Darwin']:
    env.Append(LIBS=['rt'])

libs = MapBuildDir(['base', 'db', 'ifmap', 'io', 'net', 'xml', 'schema', 'xmpp', 'bgp',
                    'dns/bind', 'dns/cfg', 'dns/cmn', 'dns/mgr', 'dns/agent', 'dns/uve'])
env.Append(LIBPATH=libs)

if platform.system() not in ['Darwin']:
    env.Prepend(LINKFLAGS =
                     ['-Wl,--whole-archive',
                      '-lbase', '-lifmap_server', '-rdynamic',
                      '-lcpuinfo', '-lprocess_info', '-lnodeinfo',
                      '-Wl,--no-whole-archive'])

contrail_dns = env.Program(target = 'contrail-dns', source = ['main.cc'])
env.Alias('contrail-dns', contrail_dns)
env.Default(contrail_dns)

# Documentation
doc_files = []
doc_files += env['BASE_DOC_FILES']
doc_files += env['IO_DOC_FILES']
doc_files += env['DB_DOC_FILES']
doc_files += env['SANDESH_DOC_FILES']
doc_files += env['IFMAP_CLIENT_DOC_FILES']
doc_files += env['XMPP_DOC_FILES']
doc_files += env.SandeshGenDoc('uve/uve.sandesh')
doc_files += env.SandeshGenDoc('bind/bind.sandesh')

# Please update sandesh/common/vns.sandesh on process name change
env.Alias('install', env.Install(env['INSTALL_BIN'], contrail_dns))
env.Alias('install', env.Install(env['INSTALL_CONF'], 'contrail-dns.conf'))
env.Alias('install', env.Install(env['INSTALL_CONF'] +
    '/dns', 'applynamedconfig.py'))
env.Alias('install', env.Install(env['INSTALL_MESSAGE_DOC'] +
    '/contrail-dns/', doc_files))

DnsEnv = env.Clone()
DnsEnv.CppDisableExceptions()
Export('DnsEnv')

subdirs = [
           'agent',
           'bind',
           'cfg',
           'cmn',
           'mgr',
           'uve',
           'test',
           'scripts',
          ]

for dir in subdirs:
    env.SConscript(dir + '/SConscript', exports='DnsEnv', duplicate=0)
