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

import os
import Options

def set_options(opt):
    opt.add_option('--with-openflow',
		   help=('Path to OFSID source for NS-3 OpenFlow Integration support'),
		   dest='with_openflow', default=None) 

def configure(conf):
    if Options.options.with_openflow:
	if os.path.isdir(Options.options.with_openflow):
	    conf.check_message("libopenflow.a location", '', True, ("%s (given)" % Options.options.with_openflow))
	    conf.env['WITH_OPENFLOW'] = os.path.abspath(Options.options.with_openflow)
	else:
	    openflow_dir = os.path.join('..','openflow')
	    if os.path.isdir(openflow_dir):
		conf.check_message("openflow location", '', True, ("%s (guessed)" % openflow_dir))
		conf.env['WITH_OPENFLOW'] = os.path.abspath(openflow_dir)
	    del openflow_dir
    if not conf.env['WITH_OPENFLOW']:
	conf.check_message("openflow location", '', False)
	conf.report_optional_feature("openflow", "NS-3 OpenFlow Integration", False,
				     "OpenFlow not enabled (see option --with-openflow)")
	return 

    test_code = '''
#include "openflow.h"
#include "nicira-ext.h"
#include "ericsson-ext.h"

extern "C"
{
#define private _private
#define delete _delete
#define list List

#include "csum.h"
#include "poll-loop.h"
#include "rconn.h"
#include "stp.h"
#include "vconn.h"
#include "xtoxll.h"

#include "chain.h"
#include "table.h"
#include "datapath.h" // The functions below are defined in datapath.c
uint32_t save_buffer (ofpbuf *);
ofpbuf * retrieve_buffer (uint32_t id);
void discard_buffer (uint32_t id);
#include "dp_act.h" // The functions below are defined in dp_act.c
void set_vlan_vid (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
void set_vlan_pcp (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
void strip_vlan (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
void set_dl_addr (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
void set_nw_addr (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
void set_tp_port (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
void set_mpls_label (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
void set_mpls_exp (ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah);
#include "pt_act.h" // The function below is defined in pt_act.c
void update_checksums (ofpbuf *buffer, const sw_flow_key *key, uint32_t old_word, uint32_t new_word);

#undef list
#undef private
#undef delete
}

int main()
{
  return 0;
}
'''

    conf.env['DL'] = conf.check(mandatory=True, lib='dl', define_name='DL', uselib='DL')
    conf.env['XML2'] = conf.check(mandatory=True, lib='xml2', define_name='XML2', uselib='XML2')
    
    conf.env.append_value('NS3_MODULE_PATH',os.path.abspath(os.path.join(conf.env['WITH_OPENFLOW'],'build','default')))
    
    conf.env['CPPPATH_OPENFLOW'] = [
				    os.path.abspath(os.path.join(conf.env['WITH_OPENFLOW'],'include')),
				    os.path.abspath(os.path.join(conf.env['WITH_OPENFLOW'],'include','openflow')),
				    os.path.abspath(os.path.join(conf.env['WITH_OPENFLOW'],'lib')),
				    os.path.abspath(os.path.join(conf.env['WITH_OPENFLOW'],'switch'))
				   ]
    conf.env['LIBPATH_OPENFLOW'] = [os.path.abspath(os.path.join(conf.env['WITH_OPENFLOW'],'build','default'))]
    
    conf.env['OPENFLOW'] = conf.check(fragment=test_code, lib='openflow', uselib='OPENFLOW DL XML2')
    conf.report_optional_feature("openflow", "NS-3 OpenFlow Integration",
    conf.env['OPENFLOW'], "openflow library not found")
    if conf.env['OPENFLOW']:
	conf.env.append_value('CXXDEFINES', 'NS3_OPENFLOW')
	conf.env.append_value('CPPPATH', conf.env['CPPPATH_OPENFLOW']) 

def build(bld):
    # Build the Switch module
    obj = bld.create_ns3_module('switch', ['node'])
    obj.source = [
	'openflow-interface.cc',
        'switch-net-device.cc',
	'switch-helper.cc',
	'switch-test-suite.cc',
        ]

    if bld.env['OPENFLOW'] and bld.env['DL'] and bld.env['XML2']:
	obj.uselib = 'OPENFLOW DL XML2' 

    headers = bld.new_task_gen('ns3header')
    headers.module = 'switch'
    headers.source = [
	'openflow-interface.h',
        'switch-net-device.h',
	'switch-helper.h',
        ]