#******************************************************************
#
# Copyright 2014 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 os
import os.path

# SConscript file for Local PKI google tests
gtest_env = SConscript('#extlibs/gtest/SConscript')
unittests_env = gtest_env.Clone()
src_dir = unittests_env.get('SRC_DIR')
target_os = unittests_env.get('TARGET_OS')
with_mq = unittests_env.get('WITH_MQ')
rd_mode = unittests_env.get('RD_MODE')

######################################################################
# Build flags
######################################################################
unittests_env.PrependUnique(CPPPATH = [
		'../include',
		'../oc_logger/include',
		'../csdk/stack/include',
		'../csdk/security/include',
		'../csdk/stack/include/internal',
		'../csdk/connectivity/api',
		'../csdk/connectivity/external/inc',
		'../csdk/ocsocket/include',
		'../c_common/ocrandom/include',
		'../csdk/logger/include',
		'#extlibs/hippomocks-master/HippoMocks',
		'#extlibs/hippomocks-master/HippoMocksTest'
		])

unittests_env.AppendUnique(LIBPATH = [unittests_env.get('BUILD_DIR')])
unittests_env.PrependUnique(LIBS = [
		'oc',
		'octbstack',
		'oc_logger',
		'connectivity_abstraction',
		'coap'
		])

if unittests_env.get('SECURED') == '1':
	unittests_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509','mbedcrypto'])

if unittests_env.get('LOGGING'):
	unittests_env.AppendUnique(CPPDEFINES = ['TB_LOG'])

if unittests_env.get('WITH_CLOUD'):
	unittests_env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])

######################################################################
# Source files and Targets
######################################################################

unittests_src = [
		'ConstructResourceTest.cpp',
		'OCPlatformTest.cpp',
		'OCRepresentationTest.cpp',
		'OCRepresentationEncodingTest.cpp',
		'OCResourceTest.cpp',
		'OCExceptionTest.cpp',
		'OCResourceResponseTest.cpp',
		'OCHeaderOptionTest.cpp'
	]

if (('SUB' in with_mq) or ('PUB' in with_mq) or ('BROKER' in with_mq)):
	unittests_src = unittests_src + ['OCMQResourceTest.cpp']

if unittests_env.get('WITH_CLOUD'):
	unittests_src = unittests_src + ['OCAccountManagerTest.cpp']

unittests = unittests_env.Program('unittests', unittests_src)

Alias("unittests", [unittests])

unittests_env.AppendTarget('unittests')
if unittests_env.get('TEST') == '1':
	if target_os in ['linux']:
                from tools.scons.RunTest import *
                run_test(unittests_env,
                         'resource_unittests_unittests.memcheck',
                         'resource/unittests/unittests')

src_dir = unittests_env.get('SRC_DIR')
svr_db_src_dir = os.path.join(src_dir, 'resource/examples/')
svr_db_build_dir = os.path.join(unittests_env.get('BUILD_DIR'), 'resource', 'unittests') + os.sep
unittests_env.Alias("install",
                    unittests_env.Install(svr_db_build_dir,
                                          os.path.join(svr_db_src_dir,
                                                       'oic_svr_db_client.dat')))
