cmake_minimum_required(VERSION 3.5.1)
project(PC2Car)

set(PC2Car_VERSION_MAJOR 0)
set(PC2Car_VERSION_MINOR 1)

set(CMAKE_CXX_STANDARD 14)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(
        "${PROJECT_SOURCE_DIR}/src/PC2CarLibConfig.h.in"
        "${PROJECT_BINARY_DIR}/PC2CarLibConfig.h")

# so we can import the config header file
include_directories("${PROJECT_BINARY_DIR}")

# compiler flags
set(CMAKE_CXX_FLAGS -pthread)

set(SOURCE_FILES
        include/PC2CarLib/CommandSender.h
        include/PC2CarLib/PC2Car.h
        include/PC2CarLib/CommandReceiver.h
        include/PC2CarLib/json.hpp
        src/CommandReceiver.cpp)

set(PUBLIC_HEADER_FILES
        include/PC2CarLib/CommandSender.h
        include/PC2CarLib/CommandReceiver.h
        include/PC2CarLib/json.hpp
        include/PC2CarLib/PC2Car.h
        ${CMAKE_CURRENT_BINARY_DIR}/PC2CarLibConfig.h)

foreach(HEADER ${PUBLIC_HEADER_FILES})
    set(PUBLIC_HEADER_FILES_COMBINED "${PUBLIC_HEADER_FILES_COMBINED}\\;${HEADER}")
endforeach()

add_library(PC2CarLib SHARED ${SOURCE_FILES})

# Specify public header files
set_target_properties(PC2CarLib PROPERTIES PUBLIC_HEADER ${PUBLIC_HEADER_FILES_COMBINED})

set(LOCAL_INSTALL_DIR ${CMAKE_CURRENT_LIST_DIR}/../../install)

set(CMAKE_PREFIX_PATH "${LOCAL_INSTALL_DIR}")

# NetworkingLib
find_package(NetworkingLib REQUIRED)
target_include_directories(PC2CarLib PUBLIC ${NetworkingLib_INCLUDE_DIRS})
target_link_libraries(PC2CarLib NetworkingLib)

find_package(Boost REQUIRED COMPONENTS system regex)
link_libraries(${Boost_LIBRARIES})

set(INSTALL_PACKAGE ON)
if(INSTALL_PACKAGE)
    #############################
    # Specify install directories
    #############################
    set(CMAKE_INSTALL_PREFIX "${LOCAL_INSTALL_DIR}")
    set(INSTALL_LIB_DIR lib/PC2CarLib)
    set(INSTALL_INCLUDE_DIR include/PC2CarLib)
    set(INSTALL_CMAKE_DIR lib/PC2CarLib/CMake)

    # Make relative paths absolute (needed later on)
    foreach(p LIB INCLUDE CMAKE)
        set(var INSTALL_${p}_DIR)
        if(NOT IS_ABSOLUTE "${${var}}")
            set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
        endif()
    endforeach()

    ####################################################
    # Create the *Config.cmake and *ConfigVersion files
    ####################################################
    # Get the relative path from the cmake install directory to the include install directory
    file(RELATIVE_PATH REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")

    # Configure *Config.cmake.in for the install tree
    # Put it inside the cmake-files directory (which is located inside the binary directory)
    set(CONF_INCLUDE_DIRS "\${PC2CARLIB_CMAKE_DIR}/${REL_INCLUDE_DIR}/..")
    configure_file(PC2CarLibConfig.cmake.in
            "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PC2CarLibConfig.cmake"
            @ONLY)

    # Configure *ConfigVersion.cmake.in for both build and install tree
    # Put it inside the binary directory
    configure_file(PC2CarLibConfigVersion.cmake.in
            "${PROJECT_BINARY_DIR}/PC2CarLibConfigVersion.cmake" @ONLY)

    ########################
    # Install PC2CarLib
    ########################
    # Install library and header files
    install(TARGETS PC2CarLib
            EXPORT PC2CarLibTargets
            LIBRARY DESTINATION "${INSTALL_LIB_DIR}"
            ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
            RUNTIME DESTINATION "${INSTALL_LIB_DIR}" # for windows
            PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}")

    # Install the PC2CarLibConfig.cmake and PC2CarLibConfigVersion.cmake
    install(FILES
            "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PC2CarLibConfig.cmake"
            "${PROJECT_BINARY_DIR}/PC2CarLibConfigVersion.cmake"
            DESTINATION "${INSTALL_CMAKE_DIR}")

    # Install the export set for use with the install-tree
    install(EXPORT PC2CarLibTargets
            DESTINATION "${INSTALL_CMAKE_DIR}")
endif()

#######
# Test
#######
set(TEST_SOURCE_FILES ${SOURCE_FILES} test/Main.cpp)
add_executable(PC2CarTest ${TEST_SOURCE_FILES})

# NetworkingLib
target_include_directories(PC2CarTest PUBLIC ${NetworkingLib_INCLUDE_DIRS})
target_link_libraries(PC2CarTest NetworkingLib)

# For debugging
target_compile_options(PC2CarTest PUBLIC -fopenmp -fPIC -O0 -g3 -ggdb)