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

import os.path

def configure(conf):
    if conf.env['ENABLE_THREADING']:
        conf.env['ENABLE_TAP'] = conf.check_nonfatal(header_name='linux/if_tun.h',
                                            define_name='HAVE_IF_TUN_H')
        conf.report_optional_feature("TapBridge", "Tap Bridge",
                                     conf.env['ENABLE_TAP'],
                                     "<linux/if_tun.h> include not detected")
    else:
        conf.report_optional_feature("TapBridge", "Tap Bridge",
                                     False,
                                     "needs threading support which is not available")

    if conf.env['ENABLE_TAP']:
        blddir = os.path.abspath(os.path.join(conf.bldnode.abspath(), conf.variant))
        tapcreatordir = os.path.abspath(os.path.join(blddir, "src/tap-bridge"))
        conf.env.append_value('NS3_EXECUTABLE_PATH', tapcreatordir)
    else:
        # Add this module to the list of modules that won't be built
        # if they are enabled.
        conf.env['MODULES_NOT_BUILT'].append('tap-bridge')

def build(bld):
    # Don't do anything for this module if tap-bridge's not enabled.
    if not bld.env['ENABLE_TAP']:
        return

    module = bld.create_ns3_module('tap-bridge', ['network', 'internet'])
    module.source = [
        'model/tap-bridge.cc',
        'model/tap-encode-decode.cc',
        'helper/tap-bridge-helper.cc',
        ]
    headers = bld(features='ns3header')
    headers.module = 'tap-bridge'
    headers.source = [
        'model/tap-bridge.h',
        'helper/tap-bridge-helper.h',
        'doc/tap.h',
        ]

    if not bld.env['PLATFORM'].startswith('freebsd'):
        tap_creator = bld.create_suid_program('tap-creator')
        tap_creator.source = [
            'model/tap-creator.cc',
            'model/tap-encode-decode.cc',
            ]

    module.env.append_value("DEFINES", "TAP_CREATOR=\"%s\"" % (tap_creator.target,))

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

    bld.ns3_python_bindings()
