#******************************************************************
#
# Copyright 2016 Samsung Electronics 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

######################################################################
# easy-setup Mediator CSDK build script
######################################################################

import os

Import('env')

env.AppendUnique(CPPDEFINES = ['EASY_SETUP_CA_INIT'])

if env.get('RELEASE'):
    env.AppendUnique(CCFLAGS = ['-Os'])
    env.AppendUnique(CPPDEFINES = ['NDEBUG'])
else:
    env.AppendUnique(CCFLAGS = ['-g'])

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

# Easysetup provides reference implemenation of Arduino Enrollee.
# Mediator applications developed on different platforms will be default defining the
# flag 'REMOTE_ARDUINO_ENROLEE'. If the target Enrollee is not Arduino, the below commented
# modifications must be enabled instead (and the existing define must be commented).
#
##### The following lines must be uncommented, if target Enrollee is not Arduino
target_enrollee = env.get('ES_TARGET_ENROLLEE')
if target_enrollee == 'arduino':
	print "REMOTE_ARDUINO_ENROLEE flag is defined."
	print "The Mediator application will be working only with Arduino Enrollee."
	env.AppendUnique(CPPDEFINES = ['REMOTE_ARDUINO_ENROLEE'])

lib_env = env.Clone()
easy_setup_env = env.Clone()

target_os = env.get('TARGET_OS')

if target_os in ['android']:
	# Add third party libraries
	SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')

######################################################################
# Build flags
######################################################################

easy_setup_env.AppendUnique(CPPPATH = ['inc',  'src','../../inc'])
easy_setup_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall'])
if target_os == 'linux':
	easy_setup_env.AppendUnique(LIBS = ['pthread', 'dl'])


######################################################################
# Linux Mediator
######################################################################
if target_os == 'linux':
	easy_setup_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
	easy_setup_env.AppendUnique(RPATH = [env.get('BUILD_DIR')])
	easy_setup_env.AppendUnique(CXXFLAGS = ['-pthread'])
	easy_setup_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'pthread', 'connectivity_abstraction'])

######################################################################
# Android Mediator
######################################################################
if target_os == 'android':
	easy_setup_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
	easy_setup_env.AppendUnique(RPATH = [env.get('BUILD_DIR')])
	easy_setup_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
	easy_setup_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'gnustl_shared'])
	if not env.get('RELEASE'):
		easy_setup_env.AppendUnique(LIBS = ['log'])


if target_os in ['android','linux']:
	easy_setup_env.PrependUnique(CPPPATH = [
		env.get('SRC_DIR') + '/resource/c_common/oic_malloc/include',
		env.get('SRC_DIR') + '/resource/csdk/connectivity/common/inc',
		env.get('SRC_DIR') + '/resource/csdk/connectivity/api',
	        env.get('SRC_DIR') + '/resource/csdk/stack/include',
		env.get('SRC_DIR') + '/resource/csdk/security/include',
		env.get('SRC_DIR') + '/extlibs/cjson',
		env.get('SRC_DIR') + '/service/easy-setup/inc',
		'inc'])

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

if target_os == 'android':
	es_m_csdk_static = easy_setup_env.StaticLibrary('libESMediatorCSDK',
					['src/provisioningapi.cpp',
					'src/wifiprovisioning.cpp',
                    'src/provisioning.cpp'])
	easy_setup_env.InstallTarget(es_m_csdk_static, 'libESMediatorCSDK')

if target_os == 'linux':
	es_m_csdk_shared = easy_setup_env.SharedLibrary('ESMediatorCSDK',
					['src/provisioningapi.cpp',
					'src/wifiprovisioning.cpp',
                    'src/provisioning.cpp'])
	easy_setup_env.InstallTarget(es_m_csdk_shared, 'libESMediatorCSDK')

	#Go to build sample apps
	SConscript('../../sampleapp/mediator/linux/csdk_sample/SConscript')

######################################################################
#Build UnitTestcases for Mediator[CSDK]
################################################ ######################
if target_os == 'linux':
    SConscript('unittests/SConscript')









