cmake_minimum_required(VERSION 2.6)

set(${PROJECT_NAME}_VERSION_MAJOR 1)
set(${PROJECT_NAME}_VERSION_MINOR 0)
set(${PROJECT_NAME}_VERSION_SUFFIX "~dev")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -pedantic -Wall -Wextra")

# Define a __FILENAME__ macro to get the filename of each file, instead of
# the full path as in __FILE__
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")

#
## Look for external libraries
#
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
include(FindEXPAT)
find_package(EXPAT REQUIRED)
find_package(ICONV REQUIRED)
find_package(LIBUUID REQUIRED)

if(WITH_LIBIDN)
  find_package(LIBIDN REQUIRED)
elseif(NOT WITHOUT_LIBIDN)
  find_package(LIBIDN)
endif()

if(WITH_SYSTEMD)
  find_package(SYSTEMD REQUIRED)
elseif(NOT WITHOUT_SYSTEMD)
  find_package(SYSTEMD)
endif()

if(WITH_BOTAN)
  find_package(BOTAN REQUIRED)
elseif(NOT WITHOUT_BOTAN)
  find_package(BOTAN)
endif()

if(WITH_CARES)
  find_package(CARES REQUIRED)
elseif(NOT WITHOUT_CARES)
  find_package(CARES)
endif()

# To be able to include the config.h file generated by cmake
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
include_directories(${EXPAT_INCLUDE_DIRS})
include_directories(${ICONV_INCLUDE_DIRS})
include_directories(${LIBUUID_INCLUDE_DIRS})

set(EXPAT_INCLUDE_DIRS ${EXPAT_INCLUDE_DIRS} PARENT_SCOPE)
set(ICONV_INCLUDE_DIRS ${ICONV_INCLUDE_DIRS} PARENT_SCOPE)
set(LIBUUID_INCLUDE_DIRS ${LIBUUID_INCLUDE_DIRS} PARENT_SCOPE)

if(LIBIDN_FOUND)
  include_directories(${LIBIDN_INCLUDE_DIRS})
  set(LIBDIN_FOUND ${LIBDIN_FOUND} PARENT_SCOPE)
  set(LIBDIN_INCLUDE_DIRS ${LIBDIN_INCLUDE_DIRS} PARENT_SCOPE)
endif()

if(SYSTEMD_FOUND)
  include_directories(${SYSTEMD_INCLUDE_DIRS})
  set(SYSTEMD_FOUND ${SYSTEMD_FOUND} PARENT_SCOPE)
  set(SYSTEMD_INCLUDE_DIRS ${SYSTEMD_INCLUDE_DIRS} PARENT_SCOPE)
endif()

if(BOTAN_FOUND)
  include_directories(SYSTEM ${BOTAN_INCLUDE_DIRS})
  set(BOTAN_FOUND ${BOTAN_FOUND} PARENT_SCOPE)
  set(BOTAN_INCLUDE_DIRS ${BOTAN_INCLUDE_DIRS} PARENT_SCOPE)
endif()

if(CARES_FOUND)
  include_directories(${CARES_INCLUDE_DIRS})
  set(CARES_FOUND ${CARES_FOUND} PARENT_SCOPE)
  set(CARES_INCLUDE_DIRS ${CARES_INCLUDE_DIRS} PARENT_SCOPE)
endif()

set(POLLER_DOCSTRING "Choose the poller between POLL and EPOLL (Linux-only)")
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
 set(POLLER "EPOLL" CACHE STRING ${POLLER_DOCSTRING})
else()
 set(POLLER "POLL" CACHE STRING ${POLLER_DOCSTRING})
endif()
if((NOT ${POLLER} MATCHES "POLL") AND
    (NOT ${POLLER} MATCHES "EPOLL"))
  message(FATAL_ERROR "POLLER must be either POLL or EPOLL")
endif()

#
## utils
#
file(GLOB source_utils
  utils/*.[hc]pp)
add_library(utils STATIC ${source_utils})
target_link_libraries(utils ${ICONV_LIBRARIES})

#
## config
#
file(GLOB source_config
  config/*.[hc]pp)
add_library(config STATIC ${source_config})
target_link_libraries(config utils)

#
## logger
#
file(GLOB source_logger
  logger/*.[hc]pp)
add_library(logger STATIC ${source_logger})
target_link_libraries(logger config)

#
## network
#
file(GLOB source_network
  network/*.[hc]pp)
add_library(network STATIC ${source_network})
target_link_libraries(network logger)
if(BOTAN_FOUND)
  target_link_libraries(network ${BOTAN_LIBRARIES})
endif()
if(CARES_FOUND)
  target_link_libraries(network ${CARES_LIBRARIES})
endif()

#
## xmpplib
#
file(GLOB source_xmpplib
  xmpp/*.[hc]pp)
add_library(xmpplib STATIC ${source_xmpplib})
target_link_libraries(xmpplib network utils logger
  ${EXPAT_LIBRARIES}
  ${LIBUUID_LIBRARIES})
if(LIBIDN_FOUND)
  target_link_libraries(xmpplib ${LIBIDN_LIBRARIES})
endif()
if(SYSTEMD_FOUND)
  target_link_libraries(xmpplib ${SYSTEMD_LIBRARIES})
endif()

#
## Check if we have std::get_time
#
include(CheckCXXSourceCompiles)

check_cxx_source_compiles("
  #include <iomanip>
  int main()
  { std::get_time(nullptr, \"\"); }"
  HAS_GET_TIME)

mark_as_advanced(HAS_GET_TIME)

check_cxx_source_compiles("
  #include <iomanip>
  int main()
  { std::put_time(nullptr, \"\"); }"
  HAS_PUT_TIME)

mark_as_advanced(HAS_PUT_TIME)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/louloulibs.h.cmake ${CMAKE_BINARY_DIR}/src/louloulibs.h)