IF(CMAKE_VERSION VERSION_LESS "3.10.0")
    IF(DEFINED BEYOND_PREPARE)
        RETURN()
    ENDIF(DEFINED BEYOND_PREPARE)
    SET(BEYOND_PREPARE TRUE)
ELSE(CMAKE_VERSION VERSION_LESS "3.10.0")
    INCLUDE_GUARD(GLOBAL)
ENDIF(CMAKE_VERSION VERSION_LESS "3.10.0")

IF(NOT CMAKE_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
    RETURN()
ENDIF(NOT CMAKE_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")

#####
# Prepare the default build settings
#
INCLUDE(FindPkgConfig)
INCLUDE(GNUInstallDirs)

IF(NOT DEFINED NAME)
    SET(NAME "beyond")
ENDIF(NOT DEFINED NAME)

IF(NOT DEFINED VERSION)
    SET(VERSION "0.0.1")
ENDIF(NOT DEFINED VERSION)

IF(NOT DEFINED PLATFORM_PACKAGE)
    SET(PLATFORM_PACKAGE true)
ENDIF(NOT DEFINED PLATFORM_PACKAGE)

IF(NOT DEFINED CMAKE_INSTALL_PCDIR)
    SET(CMAKE_INSTALL_PCDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
ENDIF(NOT DEFINED CMAKE_INSTALL_PCDIR)

IF(CMAKE_BUILD_TYPE)
    STRING(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
ENDIF(CMAKE_BUILD_TYPE)

IF(NOT PROJECT_NAME STREQUAL "beyond")
    IF(NOT DEFINED BEYOND_LIBRARIES)
        FIND_PACKAGE(libbeyond CONFIG)
        IF(libbeyond_FOUND)
            SET(BEYOND_LIBRARIES libbeyond::${NAME})
            SET(DEPENDS_ON_BEYOND libbeyond::${NAME})
        ELSE(libbeyond_FOUND)
            SET(BEYOND_LIBRARIES ${NAME})
            SET(DEPENDS_ON_BEYOND ${NAME})
        ENDIF(libbeyond_FOUND)
    ENDIF(NOT DEFINED BEYOND_LIBRARIES)
ENDIF(NOT PROJECT_NAME STREQUAL "beyond")

IF(CMAKE_BUILD_TYPE_LOWER MATCHES "debug")
    IF(COVERAGE)
        SET(COVERAGE_FLAGS "-fprofile-arcs -ftest-coverage")
    ENDIF(COVERAGE)
ENDIF(CMAKE_BUILD_TYPE_LOWER MATCHES "debug")

FIND_PROGRAM(CCACHE_FOUND ccache)
IF(CCACHE_FOUND)
	SET_PROPERTY(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)

	# Not recommended
	# SET_PROPERTY(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
ENDIF(CCACHE_FOUND)

IF(NOT PLATFORM)
    SET(PLATFORM "generic")
ENDIF(NOT PLATFORM)

IF(PLATFORM STREQUAL "android")
    ADD_DEFINITIONS(-DANDROID)
    FIND_LIBRARY(ALOG NAMES log)
    IF(ALOG STREQUAL "ALOG-NOTFOUND")
        MESSAGE(FATAL_ERROR "Uable to find the android log library")
    ENDIF(ALOG STREQUAL "ALOG-NOTFOUND")
    SET(LOG_LIBRARIES ${ALOG})
ELSEIF(PLATFORM STREQUAL "tizen")
    ADD_DEFINITIONS(-DTIZEN)
    IF(STDOUT_LOG)
        ADD_DEFINITIONS(-DDLOG_STDOUT)
    ENDIF(STDOUT_LOG)
    ENABLE_TESTING()
    PKG_CHECK_MODULES(PKGS REQUIRED
        dlog
    )
    SET(LOG_LIBRARIES ${PKGS_LDFLAGS})
    FOREACH(CFLAG ${PKGS_CFLAGS})
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CFLAG}")
    ENDFOREACH(CFLAG)
ELSEIF(PLATFORM STREQUAL "generic")
    # TODO
    # "x86_64-linux-gnu" string must be built by the current build system information
    LIST(APPEND CMAKE_PREFIX_PATH "/usr/lib/x86_64-linux-gnu")
    ADD_DEFINITIONS(-DGENERIC)
    ENABLE_TESTING()
ENDIF(PLATFORM STREQUAL "android")

ADD_DEFINITIONS(-DREVISION="${REVISION}" -DPLATFORM="${PLATFORM}" -DSYSCONFDIR="${CMAKE_INSTALL_SYSCONFDIR}")

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_FLAGS} -Wall -std=c++17 -fvisibility=hidden -Werror")
#SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-undefined,error")
