#******************************************************************
#
# Copyright 2017 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
##
# HUE Plugin build script
##

import os
import os.path

Import('env')

target_os = env.get('TARGET_OS')
src_dir = env.get('SRC_DIR')
bridging_path = os.path.join(src_dir, 'bridging')

hue_env = env.Clone()

print "Reading Hue Plugin script"

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

def maskFlags(flags):
    flags = [flags.replace('-Wl,--no-undefined', '' ) for flags in flags]
    return flags

hue_env.PrependUnique(CPPPATH = [ os.path.join(src_dir, 'resource', 'c_common', 'oic_malloc', 'include'),
                              os.path.join(src_dir, 'resource', 'c_common', 'oic_string', 'include'),
                              os.path.join(src_dir, 'resource', 'c_common'),
                              os.path.join(src_dir, 'resource', 'oc_logger', 'include'),
                              os.path.join(src_dir, 'resource', 'csdk', 'logger', 'include'),
                              os.path.join(src_dir, 'resource', 'csdk', 'include'),
                              os.path.join(src_dir, 'resource', 'csdk', 'stack', 'include'),
                              os.path.join(src_dir, 'resource', 'include'),
                              os.path.join(src_dir, 'extlibs', 'cjson'),
                              os.path.join(src_dir, 'extlibs', 'tinycbor', 'src'),
                              os.path.join(src_dir, 'extlibs', 'rapidjson', 'rapidjson', 'include', 'rapidjson')
                              ])
hue_env.AppendUnique(CPPPATH = [ os.path.join(bridging_path, 'include'),
                             os.path.join(bridging_path, 'plugins', 'hue_plugin', 'hue_objects')
                             ])

if target_os not in ['arduino', 'windows']:
    hue_env.AppendUnique(CPPDEFINES = ['WITH_POSIX'])

if target_os in ['darwin','ios']:
    hue_env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])

hue_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-Wextra', '-Werror'])
hue_env.AppendUnique(RPATH = [hue_env.get('BUILD_DIR')])
hue_env.AppendUnique(LIBPATH = [hue_env.get('BUILD_DIR')])

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

hue_env['LINKFLAGS'] = maskFlags(env['LINKFLAGS'])
hue_env.AppendUnique(LINKFLAGS = ['-Wl,--allow-shlib-undefined'])
hue_env.AppendUnique(LINKFLAGS = ['-Wl,--whole-archive', hue_env.get('BUILD_DIR') +'libmpmcommon.a','-Wl,-no-whole-archive'])

hue_env.AppendUnique(LIBS = ['m',
                             'cjson',
                             'octbstack',
                             'ocsrm',
                             'connectivity_abstraction',
                             'coap',
                             'curl' ])

#####################################################################
# Source files and Target(s)
######################################################################
hue_src = [
         os.path.join(bridging_path, 'plugins', 'hue_plugin', 'hue_resource.cpp'),
         os.path.join(bridging_path, 'plugins', 'hue_plugin', 'hue_auth_spec.cpp'),
         os.path.join(bridging_path, 'plugins', 'hue_plugin', 'hue_file.cpp'),
         os.path.join(bridging_path, 'plugins', 'hue_plugin', 'hue_objects', 'hue_bridge.cpp'),
         os.path.join(bridging_path, 'plugins', 'hue_plugin', 'hue_objects', 'hue_light.cpp'),
         ]

hue_env.AppendUnique(HUE_SRC = hue_src)
huelib = hue_env.SharedLibrary('hueplugin', hue_env.get('HUE_SRC'))
hue_env.InstallTarget(huelib, 'hueplugin')
hue_env.UserInstallTargetLib(huelib, 'hueplugin')


