# //******************************************************************
# //
# // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
# //
# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# //
# // Licensed under the Apache License, Version 2.0 (the "License");
# // you may not use this file except in compliance with the License.
# // You may obtain a copy of the License at
# //
# //      http://www.apache.org/licenses/LICENSE-2.0
# //
# // Unless required by applicable law or agreed to in writing, software
# // distributed under the License is distributed on an "AS IS" BASIS,
# // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# // See the License for the specific language governing permissions and
# // limitations under the License.
# //
# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#

Import('env')
import os
import os.path
srmtest_env = env.Clone()

src_dir = srmtest_env.get('SRC_DIR')

######################################################################
# Build flags
######################################################################
srmtest_env.PrependUnique(CPPPATH = [
        '../../ocmalloc/include',
		'../../connectivity/inc',
		'../../connectivity/api',
		'../../connectivity/external/inc',
		'../../connectivity/lib/libcoap-4.1.1',
		'../include',
		'../include/internal',
		'../../logger/include',
		'../../ocmalloc/include',
		'../../stack/include',
		'../../stack/include/internal',
		'../../../oc_logger/include',
		'../../../../extlibs/gtest/gtest-1.7.0/include',
		'../../../../extlibs/cjson/',
#		'../../../../extlibs/tinydtls/',
		'../include'
		])
srmtest_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-pthread'])
srmtest_env.AppendUnique(LIBS = ['-lpthread'])
srmtest_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
srmtest_env.AppendUnique(LIBPATH = [src_dir + '/extlibs/gtest/gtest-1.7.0/lib/.libs'])
srmtest_env.PrependUnique(LIBS = ['ocsrm',
                                    'octbstack',
                                    'oc_logger',
                                    'connectivity_abstraction',
                                    'coap',
                                    'gtest',
                                    'gtest_main'])

if env.get('SECURED') == '1':
    srmtest_env.AppendUnique(LIBS = ['tinydtls'])

if not env.get('RELEASE'):
	srmtest_env.AppendUnique(CPPDEFINES = ['TB_LOG'])

######################################################################
# Source files and Targets
######################################################################
unittest = srmtest_env.Program('unittest', ['aclresourcetest.cpp',
                                            'pstatresource.cpp',
                                            'doxmresource.cpp',
                                            'policyengine.cpp',
                                            'securityresourcemanager.cpp',
                                            'credentialresource.cpp',
                                            'srmutility.cpp',
                                            'iotvticalendartest.cpp',
                                            'base64tests.cpp',
                                            'svcresourcetest.cpp',
                                            'srmtestcommon.cpp'])

Alias("test", [unittest])

unittest_src_dir = src_dir + '/resource/csdk/security/unittest/'
unittest_build_dir = env.get('BUILD_DIR') +'/resource/csdk/security/unittest'

srmtest_env.Alias("install", srmtest_env.Install( unittest_build_dir,
    unittest_src_dir + 'oic_unittest.json'))
srmtest_env.Alias("install", srmtest_env.Install( unittest_build_dir,
    unittest_src_dir + 'oic_unittest_acl1.json'))
srmtest_env.Alias("install", srmtest_env.Install( unittest_build_dir,
    unittest_src_dir + 'oic_unittest_default_acl.json'))

env.AppendTarget('test')
if env.get('TEST') == '1':
	target_os = env.get('TARGET_OS')
	if target_os == 'linux':
		out_dir = env.get('BUILD_DIR')
		result_dir = env.get('BUILD_DIR') + '/test_out/'
		if not os.path.isdir(result_dir):
			os.makedirs(result_dir)
		srmtest_env.AppendENVPath('GTEST_OUTPUT', ['xml:'+ result_dir])
		srmtest_env.AppendENVPath('LD_LIBRARY_PATH', [out_dir])
		srmtest_env.AppendENVPath('LD_LIBRARY_PATH', ['./extlibs/gtest/gtest-1.7.0/lib/.libs'])
		ut = srmtest_env.Command ('ut', None, out_dir + '/resource/csdk/security/unittest/unittest')
		AlwaysBuild ('ut')

