# Copyright (c) 2009-2011 XORP, Inc and Others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, Version 2, June
# 1991 as published by the Free Software Foundation. Redistribution
# and/or modification of this program under the terms of any other
# version of the GNU General Public License is not permitted.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
# see the GNU General Public License, Version 2, a copy of which can be
# found in the XORP LICENSE.gpl file.
#
# XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
# http://xorp.net


import os
Import('env')

env = env.Clone()

env.AppendUnique(CPPPATH = [
    '#',
    '$BUILDDIR',
     ])

env.AppendUnique(LIBPATH = [
    '$BUILDDIR/libxorp',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libfeaclient',
    '$BUILDDIR/xrl/interfaces',
    '$BUILDDIR/xrl/targets',
    '$BUILDDIR/policy/backend',
    '$BUILDDIR/policy/common',
    '$BUILDDIR/bgp',
    '$BUILDDIR/fea',
    '.',
    ])

env.AppendUnique(LIBS = [
    'xorp_bgp',
    'xif_bgp',
    'xorp_ipc',
    'xorp_core',
    'xorp_comm',
    'xst_test_peer',
    'xif_test_peer',
    'xst_coord',
    'xif_coord',
    'xif_rib',
    'xif_datain',
    'xorp_fea_client',
    'xorp_policy_backend',
    'xorp_policy_common',
    'xif_fea_ifmgr_mirror',
    'xif_fea_ifmgr_replicator',
    'xst_fea_ifmgr_mirror',
    'xst_bgp',
    'xif_finder_event_notifier',
    ])

if not (env.has_key('disable_profile') and env['disable_profile']):
    env.AppendUnique(LIBS = [
        'xif_profile_client',
        ])

is_shared = env.has_key('SHAREDLIBS')
if not is_shared:
    env.AppendUnique(LIBS = [
        "crypto",
        ])

    if not (env.has_key('mingw') and env['mingw']):
        env.AppendUnique(LIBS = [
            "rt",
            ])

if (env.has_key('mingw') and env['mingw']):
    env.AppendUnique(LIBS = [
        'ws2_32',
        'iphlpapi',
#        'mprapi',
        'regex',
        'winmm',
        ])

    # This is really needed, belive it or not.
    env.Append(LIBS = [
        'xorp_core',
        ]);


coord_srcs = [
    'bgppp.cc',
    'command.cc',
    'coord.cc',
    'peer.cc',
    'trie.cc',
    ]

test_peer_srcs = [
    'test_peer.cc',
    'bgppp.cc',
    ]

test_trie_srcs = [
    'test_peer.cc',
    'bgppp.cc',
    ]

script_srcs = [
    'args.sh',
    'test_path_attribute1.sh',
    'test_rib1.sh',
    'test_routing2.sh',
    'inject.sh',
    'test_path_attribute2.sh',
    'test_rib_fea1.sh',
    'test_terminate.sh',
    'notification_codes.sh',
    'test_path_attribute3.sh',
    'test_route_flap_damping1.sh',
    'xrl_shell_funcs.sh',
    'setup_paths.sh',
    'soak.sh',
    'test_busted',
    'test_peering1.sh',
    'test_route_reflection1.sh',
    'test1.sh',
    'test_peering2.sh',
    'test_route_reflection2.sh',
    'test2.sh',
    'test_peering3.sh',
    'test_routing1.sh',
    'harness.py',
    'lookup.py',
    'NOTES',
    'originate_routes.pl',
    ]


coord = env.Program(target = 'coord', source = coord_srcs)
test_peer = env.Program(target = 'test_peer', source = test_peer_srcs)
test_trie  = env.Program(target = 'test_trie', source = test_trie_srcs)

harnesspath = '$exec_prefix/bgp/harness'

if env['enable_tests']:
    env.Alias('install', env.InstallProgram(harnesspath, coord))
    env.Alias('install', env.InstallProgram(harnesspath, test_peer))
    env.Alias('install', env.InstallProgram(harnesspath, test_trie))

    for ss in script_srcs:
        env.Alias('install', env.InstallProgram(harnesspath, env.Entry('%s' % ss)))

if 'check' in COMMAND_LINE_TARGETS:
    from subprocess import call
    
    for ss in script_srcs:
        env.Alias('check', env.Execute(Copy('%s' % ss, '../../../../bgp/harness/%s' %ss)))

    # Tests to run automatically with 'scons test'
    # This 'Execute' recipe doesn't work right, because if a program fails,
    #  then scons will exit.  Use 'call' instead to force them to be run.
    #env.Alias('check', env.Execute(env.Action('./test_peering1.sh')))
    
    call("./test_busted")
    call("./test_peering1.sh")
    call("./test_peering2.sh")
    call('./test_peering3.sh')
    call('./test_routing1.sh')
    call('./test_routing2.sh')
    call('./test_rib1.sh')
    call('./test_rib_fea1.sh')    
    call('./test_path_attribute1.sh')
    call('./test_path_attribute2.sh')
    call('./test_path_attribute3.sh')
    call('./test_route_reflection1.sh')
    call('./test_route_reflection2.sh')
    call('./test_route_flap_damping1.sh')
    call('./test_terminate.sh')

#for t in tests:
#    test_targets.append(env.AutoTest(target = 'test_%s' % t,
#                                     source = 'test_%s.cc' % t))

Default(coord, test_peer, test_trie)
