SET(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")

MESSAGE(STATUS "Configuring ${PROJECT_NAME} ${VERSION}...")

# TODO: move a lot of stuff to src directory

# directories
SET(src_base "${CMAKE_CURRENT_SOURCE_DIR}/src/")

# system abilities
INCLUDE(CheckIncludeFiles)
CHECK_INCLUDE_FILES(sys/mman.h HAVE_SYS_MMAN)

IF(WANT_MLOCK AND HAVE_SYS_MMAN)
    SET(USE_MLOCK ON)
ELSE()
    SET(USE_MLOCK OFF)
ENDIF()

try_compile(HAVE_STD_THREAD ${CMAKE_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/tests/std-thread.cpp")
IF(HAVE_STD_THREAD)
    SET(CAN_TEST ON)
ELSE()
    SET(CAN_TEST OFF)
ENDIF()

# warn flags
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-Wextra COMPILER_SUPPORTS_WEXTRA)
CHECK_CXX_COMPILER_FLAG(-Weverything COMPILER_SUPPORTS_WEVERYTHING)
CHECK_CXX_COMPILER_FLAG(-Wno-padded COMPILER_SUPPORTS_NOPADDED)
CHECK_CXX_COMPILER_FLAG(-Wno-c++98-compat COMPILER_SUPPORTS_NO98COMPAT)
CHECK_CXX_COMPILER_FLAG(-Werror COMPILER_SUPPORTS_WERROR)
if(COMPILER_SUPPORTS_WEXTRA)
    set(WARN_FLAGS "${WARN_FLAGS} -Wextra")
endif()
if(COMPILER_SUPPORTS_WEVERYTHING)
    if(COMPILER_SUPPORTS_NOPADDED AND COMPILER_SUPPORTS_NO98COMPAT)
        set(WARN_FLAGS "${WARN_FLAGS} -Weverything -Wno-padded -Wno-c++98-compat")
    endif()
endif()
if(COMPILER_SUPPORTS_WERROR)
    set(WARN_FLAGS "${WARN_FLAGS} -Werror")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall ${WARN_FLAGS}")

set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

# install paths
get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
if("${LIB64}" STREQUAL "TRUE")
    set(LIBSUFFIX 64)
else()
    set(LIBSUFFIX "")
endif()

set(INSTALL_LIB_DIR "lib${LIBSUFFIX}" CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR "include" CACHE PATH "Installation directory for headers")
mark_as_advanced(
	INSTALL_LIB_DIR
	INSTALL_INC_DIR
	)

if(CAN_TEST)
    enable_testing()
endif()

# subdirectories
add_subdirectory(src)
add_subdirectory(doc)
add_subdirectory(include)

SET(DESCRIPTION "A small lock-free, multi-reader ringbuffer")

# CPack
if(RINGBUFFER_DO_CPACK)
SET(MY_NAME_CAP ${PROJECT_NAME})
#SET(MY_NAME_LOW ${PROJECT_NAME}) # (TODO: tolower)
STRING(TOLOWER ${MY_NAME_CAP} MY_NAME_LOW)
#SET(MY_PACKAGE_FILE_NAME "${MY_NAME_LOW}-${VERSION}-${OS}-${MY_HOST_STR}")
SET(MY_PACKAGE_FILE_NAME "${MY_NAME_LOW}-${VERSION}")
SET(MY_SRC_PACKAGE_FILE_NAME "${MY_NAME_LOW}-${VERSION}")

SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${MY_NAME_CAP} - ${DESCRIPTION}")
SET(CPACK_PACKAGE_FILE_NAME ${MY_PACKAGE_FILE_NAME})
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "${MY_SRC_PACKAGE_FILE_NAME}")
SET(CPACK_PACKAGE_VENDOR "${MY_NAME_CAP} developers")
SET(CPACK_PACKAGE_VERSION ${VERSION})
SET(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE.txt")
SET(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")
SET(CPACK_SOURCE_GENERATOR "TBZ2")
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${MY_SRC_PACKAGE_FILE_NAME}")
INCLUDE(CPack)
endif()

# summary
# TODO: make this colored like in zyn
function(print_summary_base)
	MESSAGE("")
        MESSAGE("SUMMARY for ${PROJECT_NAME} ${VERSION}:")
	MESSAGE(" * Compiler used: ${CMAKE_CXX_COMPILER}")
	if(CMAKE_BUILD_TYPE STREQUAL "Debug")
		set(MSG_BUILD_TYPE_FLAG ${CMAKE_CXX_FLAGS_DEBUG})
	else()
		set(MSG_BUILD_TYPE_FLAG ${CMAKE_CXX_FLAGS_RELEASE})
	endif()
	MESSAGE(" * Build Type: ${CMAKE_BUILD_TYPE} (${MSG_BUILD_TYPE_FLAG})")
        MESSAGE(" * mlock (realtime requirement): ${USE_MLOCK}")
	MESSAGE(" * can build tests: ${CAN_TEST}")
        MESSAGE(" * Building Doc: No - Type make ringbuffer-doc if you want")
	MESSAGE(" * Executing Tests: No - Type make test if you want")
        MESSAGE("")
endfunction()
