#******************************************************************
#
# 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 C++[Rich] SDK build script
######################################################################
import os.path
Import('env')

easysetup_path = os.curdir

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'])

# Add third party libraries
lib_env = env.Clone()
SConscript('#service/third_party_libs.scons', 'lib_env')

easy_setup_env = lib_env.Clone()
target_os = env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
easy_setup_env.AppendUnique(CPPPATH = ['inc', 'src', '../../inc'])

if target_os not in ['windows']:
    easy_setup_env.AppendUnique(CXXFLAGS = ['-Wall', '-std=c++0x'])

if target_os in ['linux']:
   easy_setup_env.AppendUnique(LIBS = ['pthread', 'dl', 'coap'])

if target_os in ['android']:
	easy_setup_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
	if env.get('SECURED') == '1':
		easy_setup_env.AppendUnique(LIBPATH = [env.get('SRC_DIR')+'/android/android_api/base/libs/armeabi'])
	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 env.get('SECURED') == '1':
		easy_setup_env.AppendUnique(LIBS = ['ocpmapi','ocprovision'])
	if not env.get('RELEASE'):
		easy_setup_env.AppendUnique(LIBS = ['log'])

if target_os in ['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'])
	if env.get('SECURED') == '1':
		easy_setup_env.AppendUnique(LIBS = ['ocpmapi', 'ocprovision'])

if target_os in ['android','linux', 'ios']:
	easy_setup_env.PrependUnique(CPPPATH = [
		env.get('SRC_DIR') + '/resource/c_common/oic_malloc/include',
		env.get('SRC_DIR') + '/resource/include',
		env.get('SRC_DIR') + '/resource/csdk/logger/include',
		env.get('SRC_DIR') + '/resource/csdk/stack/include',
		env.get('SRC_DIR') + '/resource/csdk/logger/include',
		env.get('SRC_DIR') + '/resource/csdk/security/include',
		env.get('SRC_DIR') + '/extlibs/cjson',
		env.get('SRC_DIR') + '/service/easy-setup/inc'])
	if target_os not in ['linux', 'tizen']:
		easy_setup_env.PrependUnique(CPPPATH = [env.get('SRC_DIR') + '/extlibs/sqlite3'])
	if env.get('SECURED') == '1':
		with_upstream_libcoap = easy_setup_env.get('WITH_UPSTREAM_LIBCOAP')
		if with_upstream_libcoap == '1':
			# For bring up purposes only, we manually copy the forked version to where the unforked version is downloaded.
			easy_setup_env.AppendUnique(CPPPATH = ['#extlibs/libcoap/libcoap/include'])
		else:
			# For bring up purposes only, the forked version will live here.
			easy_setup_env.AppendUnique(CPPPATH = [env.get('SRC_DIR') + '/resource/csdk/connectivity/lib/libcoap-4.1.1/include'])
		easy_setup_env.AppendUnique(CPPPATH = [
			env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include',
			env.get('SRC_DIR') + '/resource/csdk/security/include/internal',
			env.get('SRC_DIR') + '/resource/csdk/connectivity/api/',
			env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include/internal',
			env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include/cloud',
			env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include/oxm'])
		if env.get('WITH_TCP') == True:
			easy_setup_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509', 'mbedcrypto'])

if not env.get('RELEASE') and target_os not in ['ios']:
    easy_setup_env.PrependUnique(LIBS = ['gcov'])
    easy_setup_env.AppendUnique(CCFLAGS = ['--coverage'])

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

print"easysetup_path %s" % easysetup_path

es_src = None

if target_os in ['android','linux','ios']:
	if env.get('SECURED') == '1':
		env.AppendUnique(es_src = [os.path.join(easysetup_path, 'src/EnrolleeSecurity.cpp')])

	es_common_src =	['src/RemoteEnrollee.cpp',
		'src/EnrolleeResource.cpp',
		'src/CloudResource.cpp',
		'src/EasySetup.cpp',
		'src/ESException.cpp']

	env.AppendUnique(es_src = es_common_src)

	es_sdk_static = easy_setup_env.StaticLibrary('ESMediatorRich', env.get('es_src'))
	easy_setup_env.InstallTarget(es_sdk_static, 'libESMediatorRich')
	if target_os not in ['ios']:
		es_sdk_shared = easy_setup_env.SharedLibrary('ESMediatorRich', env.get('es_src'))
		easy_setup_env.InstallTarget(es_sdk_shared, 'libESMediatorRich')
		easy_setup_env.AppendUnique(LINKFLAGS = ['-Wl,-soname,libESMediatorRich.so'])

print "Files path is %s" % env.get('es_src')

######################################################################
# Generate RichSDK Mediator AAR
################################################ ######################
if target_os in ['android']:
	SConscript('android/SConscript')

######################################################################
# Build RichSDK Mediator Sample App
################################################ ######################
if target_os in ['linux']:
	SConscript('../../sampleapp/mediator/linux/richsdk_sample/SConscript')

if target_os in ['android']:
	SConscript('../../sampleapp/mediator/android/SConscript')


######################################################################
#Build UnitTestcases for Mediator[RichSDK]
################################################ ######################
if env.get('SECURED') == '0':
   if target_os == 'linux':
       SConscript('unittests/SConscript')

