## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-

import os

from waflib import Options


def options(opt):
    opt.add_option('--with-nsclick',
                   help=('Path to Click source or installation prefix for NS-3 Click Integration support'),
                   dest='with_nsclick', default=None)
    opt.add_option('--disable-nsclick',
                   help=('Disable NS-3 Click Integration support'),
                   dest='disable_nsclick', default=False, action="store_true")

def configure(conf):
    if Options.options.disable_nsclick:
        conf.report_optional_feature("nsclick", "NS-3 Click Integration", False,
                                     "disabled by user request")
        return
        
    if Options.options.with_nsclick:
        if os.path.isdir(Options.options.with_nsclick):
            conf.msg("Checking for libnsclick.so location", ("%s (given)" % Options.options.with_nsclick))
            conf.env['WITH_NSCLICK'] = os.path.abspath(Options.options.with_nsclick)
    else:
        # bake.py uses ../../build, while ns-3-dev uses ../click.
        lib_to_check = 'libnsclick.so'
        nsclick_bake_build_dir = os.path.join('..', '..', 'build')
        nsclick_bake_lib_dir = os.path.join(nsclick_bake_build_dir, 'lib')
        nsclick_dir = os.path.join('..','click')
        if os.path.exists(os.path.join(nsclick_bake_lib_dir, lib_to_check)):
            conf.msg("Checking for click location",("%s (guessed)" % nsclick_bake_build_dir))
            conf.env['WITH_NSCLICK'] = os.path.abspath(nsclick_bake_build_dir)
        elif os.path.isdir(nsclick_dir):
            conf.msg("Checking for click location", ("%s (guessed)" % nsclick_dir))
            conf.env['WITH_NSCLICK'] = os.path.abspath(nsclick_dir)
        del nsclick_bake_build_dir
        del nsclick_bake_lib_dir
        del nsclick_dir
    if not conf.env['WITH_NSCLICK']:
        conf.msg("Checking for click location", False)
        conf.report_optional_feature("nsclick", "NS-3 Click Integration", False,
                                     "nsclick not enabled (see option --with-nsclick)")

        # Add this module to the list of modules that won't be built
        # if they are enabled.
        conf.env['MODULES_NOT_BUILT'].append('click')

        return

    test_code = '''
#include<sys/types.h>
#include<sys/time.h>
#include<click/simclick.h>

#ifdef __cplusplus
extern "C" {
#endif

int simclick_sim_send(simclick_node_t *sim,int ifid,int type, const unsigned char* data,int len,simclick_simpacketinfo *pinfo)
{
  return 0;
}

int simclick_sim_command(simclick_node_t *sim, int cmd, ...)
{
  return 0;
}
#ifdef __cplusplus
}
#endif

int main()
{
  return 0;
}
'''
    conf.env['DL'] = conf.check(mandatory=True, lib='dl', define_name='DL', uselib_store='DL')

    for tmp in ['lib', 'ns']:
        libdir = os.path.abspath(os.path.join(conf.env['WITH_NSCLICK'],tmp))
        if os.path.isdir(libdir):
            conf.env.append_value('NS3_MODULE_PATH',libdir)
            conf.env['LIBPATH_NSCLICK'] = [libdir]

    conf.env['INCLUDES_NSCLICK'] = [os.path.abspath(os.path.join(conf.env['WITH_NSCLICK'],'include'))]
    conf.env['LIB_NSCLICK'] = ['nsclick']
    conf.env['DEFINES_NSCLICK'] = ['NS3_CLICK']
    conf.env['NSCLICK'] = conf.check_nonfatal(fragment=test_code, use='DL NSCLICK',
                                              msg="Checking for library nsclick")
    conf.report_optional_feature("nsclick", "NS-3 Click Integration",
                                  conf.env['NSCLICK'], "nsclick library not found")
    if not conf.env['NSCLICK']:
        # Add this module to the list of modules that won't be built
        # if they are enabled.
        conf.env['MODULES_NOT_BUILT'].append('click')

def build(bld):
    # Don't do anything for this module if click should not be built.
    if 'click' in bld.env['MODULES_NOT_BUILT']:
        return

    module = bld.create_ns3_module('click', ['core', 'network', 'internet'])
    module.source = [
        'model/ipv4-click-routing.cc',
        'model/ipv4-l3-click-protocol.cc',
        'helper/click-internet-stack-helper.cc',
        ]

    module_test = bld.create_ns3_module_test_library('click')
    module_test.source = [
        'test/ipv4-click-routing-test.cc',
        ]

    if bld.env['NSCLICK'] and bld.env['DL']:
        module.use.extend(['NSCLICK', 'DL'])
        module_test.use.extend(['NSCLICK', 'DL'])

    headers = bld(features='ns3header')
    headers.module = 'click'
    headers.source = [
        'model/ipv4-click-routing.h',
        'model/ipv4-l3-click-protocol.h',
        'helper/click-internet-stack-helper.h',
        ]

    if bld.env['ENABLE_EXAMPLES']:
        bld.recurse('examples')

    bld.ns3_python_bindings()
