# Copyright AllSeen Alliance. All rights reserved.
#
#    Permission to use, copy, modify, and/or distribute this software for any
#    purpose with or without fee is hereby granted, provided that the above
#    copyright notice and this permission notice appear in all copies.
#
#    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 

from os.path import basename

Import('env')

router_env = env.Clone()

vars = Variables()
vars.Add(EnumVariable('POLICYDB', 'Enable policy support', 'off', allowed_values=('on', 'off')))
vars.Update(router_env)
Help(vars.GenerateHelpText(router_env))


if router_env['OS'] == "darwin":
    # Darwin has its own version of the daemon transport
	srcs = [ f for f in router_env.Glob('*.cc') + router_env.Glob('*.c') + [router_env['OS'] + '/DaemonTransport' + router_env['OS'].capitalize() + '.cc'] ]
else:
	srcs = [ f for f in router_env.Glob('*.cc') + router_env.Glob('*.c') + [router_env['OS_GROUP'] + '/DaemonTransport' + router_env['OS_GROUP'].capitalize() + '.cc']]

srcs += [router_env['OS_GROUP'] + '/Socket.cc']

if router_env['OS'] != "android":
    srcs += [router_env['OS_GROUP'] + '/PermissionMgr' + router_env['OS_GROUP'].capitalize() + '.cc']

router_env.Append(CPPPATH = [ router_env.Dir('..').srcnode(),
                              router_env.Dir('.').srcnode(),
                              router_env.Dir('$OS_GROUP').srcnode() ])

if router_env['POLICYDB'] == 'on':
    router_env.Append(CPPDEFINES = [ 'ENABLE_POLICYDB' ])
    router_env.Append(CPPPATH = [ router_env.Dir('policydb').srcnode() ])
    srcs += [ 'policydb/PolicyDB.cc' ]

router_objs = router_env.Object(srcs)

router_objs.extend(router_env.SConscript('ns/SConscript', exports = ['router_env']))

if router_env['OS'] == "android":
    router_objs.extend(router_env.SConscript('android/SConscript', exports = ['router_env']))

# Standalone daemon object file
srobj = router_env.SConscript('standalone/SConscript', exports = ['router_env'])

# Bundled daemon object file
brobj = router_env.SConscript('bundled/SConscript', exports = ['router_env'])

progs = router_env.SConscript('${OS_GROUP}/SConscript', exports=['router_env', 'router_objs', 'srobj'])

# Bundled daemon library
lib = router_env.StaticLibrary('ajrouter', brobj + router_objs);

# AllJoyn Daemon test programs
if router_env['OS'] == 'darwin':
    if router_env['CPU'] in ['arm', 'armv7', 'armv7s', 'arm64',]:
        print 'Not building unit tests for iOS...'    
        tests = []
    else:
        print 'Building unit tests for darwin...'
        tests = router_env.SConscript('test/SConscript', exports = ['router_env', 'router_objs', 'srobj'])
else:
    tests = router_env.SConscript('test/SConscript', exports = ['router_env', 'router_objs', 'srobj'])
    
# Return daemon and related tests
ret = progs + tests, lib, srobj, router_objs
Return('ret')
