# 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

# $XORP$

import os
Import('env')

subdirs = [
	#'config', # FIXME install
	'tests',
    ]

SConscript(dirs = subdirs, exports='env')

env = env.Clone()

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

env.PrependUnique(LIBPATH = [
    '$BUILDDIR/libxorp',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libproto',
    '$BUILDDIR/xrl/interfaces',
    '$BUILDDIR/xrl/targets',
    '$BUILDDIR/cli',
    '$BUILDDIR/cli/libtecla',
    '.',
    ])

### libxorp_rtrmgr

libxorp_rtrmgr_env = env.Clone()

# FIXME generate lex/yacc from source
# shorthand this plz
# see http://209.85.229.132/search?q=cache:3j0AsRORc6MJ:https://gforge.inria.fr/plugins/scmsvn/viewcvs.php/scons_util/trunk/src/platform.py%3Frev%3D18%26root%3Dopenalea%26view%3Dmarkup+scons+lexflags+append&cd=2&hl=en&ct=clnk&gl=uk&client=firefox-a
# ... needs toolchain check for flex 2.5.4 or poss more recent.
# known to work with freebsd base system flex.
#tplt_env = env.Clone()
#tplt_env.AppendUnique(LEXFLAGS='-Ptplt')
#tplt_env.CXXFile(source='template.ll', target='lex.tplt.cc')
#boot_env = env.Clone()
#boot_env.AppendUnique(LEXFLAGS='-Pboot')
#boot_env.CXXFile(source='boot.ll', target='lex.boot.cc')
#opcmd_env = env.Clone()
#opcmd_env.AppendUnique(LEXFLAGS='-Popcmd')
#opcmd_env.CXXFile(source='op_commands.ll', target='lex.opcmd.cc')

libxorp_rtrmgr_srcs = [
	'command_tree.cc',
	'conf_tree.cc',
	'conf_tree_node.cc',
	'config_operators.cc',
	'generic_module_manager.cc',
	'glob_win32.c',
	'lex.boot.cc',
	'lex.opcmd.cc',
	'lex.tplt.cc',
	'master_conf_tree.cc',
	'master_conf_tree_node.cc',
	'master_template_tree.cc',
	'master_template_tree_node.cc',
	'module_command.cc',
	'module_manager.cc',
	'op_commands.cc',
	'randomness.cc',
	'slave_conf_tree.cc',
	'slave_conf_tree_node.cc',
	'slave_module_manager.cc',
	'task.cc',
	'template_base_command.cc',
	'template_commands.cc',
	'template_tree.cc',
	'template_tree_node.cc',
	'unexpanded_program.cc',
	'unexpanded_xrl.cc',
	'userdb.cc',
	'xorp_client.cc',
	'y.boot_tab.cc',
	'y.opcmd_tab.cc',
	'y.tplt_tab.cc',
	]

# Runtime XRL syntax validation for developers.
if env['debug_xrldb']:
    libxorp_rtrmgr_srcs += [ 'xrldb.cc' ]
    libxorp_rtrmgr_env.AppendUnique(CPPDEFINES = [
        ( 'DEBUG_XRLDB', 1 ),
    ])

# Pushdown for static paths in util.cc.
xorp_paths = {
    "XORP_INSTALL_ROOT" : '\\"' + str(env['prefix']) + '\\"',
    "XORP_BUILD_ROOT"   : '\\"' + str(env['BUILDDIR']) + '\\"',
    "XORP_SRC_ROOT"     : '\\"' + str(env['xorp_sourcedir']) + '\\"',
}


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",
            ])

util_cc_env = env.Clone()
util_cc_env.AppendUnique(CPPDEFINES=xorp_paths.items())
obj_util = util_cc_env.StaticObject(source='util.cc')
libxorp_rtrmgr_srcs += [ obj_util ]

libxorp_rtrmgr = libxorp_rtrmgr_env.StaticLibrary(target = 'libxorp_rtrmgr',
					source = libxorp_rtrmgr_srcs)

#
# Common RPATH.
#
env.Replace(RPATH = [
    env.Literal(env['xorp_sbin_rpath'])
])

### rtrmgr

rtrmgr_env = env.Clone()

# The Router Manager's main() function is responsible for
# creating the XRLdb, if XRL syntax debugging is enabled.
if env['debug_xrldb']:
    rtrmgr_env.AppendUnique(CPPDEFINES = [
        ( 'DEBUG_XRLDB', 1 ),
    ])

rtrmgr_env.AppendUnique(LIBS = [
    'xorp_rtrmgr',
    'xst_rtrmgr',
    'xif_rtrmgr_client',
    'xif_finder_event_notifier',
    'xorp_finder',
    'xorp_ipc',
    'xorp_comm',
    'xorp_core',
    'crypto'  # Required on Fedora 13.
])

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

    rtrmgr_env.Append(LIBS = ['xorp_core', 'crypto'])


rtrmgr_srcs = [
	'main_rtrmgr.cc',
	'xrl_rtrmgr_interface.cc',
	]

rtrmgr = rtrmgr_env.Program(target = 'xorp_rtrmgr', source = rtrmgr_srcs)

env.Alias('install',
          env.InstallProgram(env['xorp_sbindir'], rtrmgr))

### xorpsh

if env['enable_xorpsh']:
    xorpsh_env = env.Clone()

    xorpsh_env.AppendUnique(LIBS = [
        'xorp_rtrmgr',
        'xorp_cli',
        'xst_cli',			# XXX Not picked up automagically?
        'xif_cli_processor',		# XXX ditto?
        'xorp_proto',
        'xorp_tecla',
        'xif_rtrmgr',
        'xst_xorpsh',
        'xif_finder_event_notifier',
        'xorp_ipc',
        'xorp_comm',
        'xorp_core'
        ])

    # Some sort of curses library is required.
    if xorpsh_env.has_key('has_libncurses') and xorpsh_env['has_libncurses']:
        xorpsh_env.AppendUnique(LIBS = [
            'ncurses'
            ])
    elif xorpsh_env.has_key('has_libpdcurses') and xorpsh_env['has_libpdcurses']:
        xorpsh_env.AppendUnique(LIBS = [
            'pdcurses'
            ])
    else:
        # Assume regular curses.
        xorpsh_env.AppendUnique(LIBS = [
            'curses'
            ])

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

        xorpsh_env.Append(LIBS = ['xorp_core', 'crypto'])

    xorpsh_srcs = [
	'xorpsh_main.cc',
	'xrl_xorpsh_interface.cc',
	'cli.cc',
	]

    xorpsh = xorpsh_env.Program(target = 'xorpsh', source = xorpsh_srcs)

    env.Alias('install',
              env.InstallProgram(env['xorp_sbindir'], xorpsh))

if not (env.has_key('disable_profile') and env['disable_profile']):
    ### profiler

    profiler_env = env.Clone()

    profiler_env.AppendUnique(LIBS = [
        'xif_profile',
        'xst_profiler',
        'xorp_finder',
        'xorp_ipc',
        'xorp_comm',
        'xorp_core'
        ])

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

        profiler_env.Append(LIBS = ['xorp_core', 'crypto'])
        
    profiler_srcs = [
	'profiler.cc',
	]

    profiler = profiler_env.Program(target = 'xorp_profiler',
                                    source = profiler_srcs)

    env.Alias('install',
              env.InstallProgram(env['xorp_sbindir'], profiler))

    if env['enable_xorpsh']:
        Default(rtrmgr, xorpsh, profiler)
    else:
        Default(rtrmgr, profiler)
else:
    if env['enable_xorpsh']:
        Default(rtrmgr, xorpsh)
    else:
        Default(rtrmgr)
    
