cmake_minimum_required(VERSION 3.5.1)
project(PlatoonProtocol)

set(PlatoonProtocol_VERSION_MAJOR 0)
set(PlatoonProtocol_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/PlatoonProtocolLibConfig.h.in"
        "${PROJECT_BINARY_DIR}/PlatoonProtocolLibConfig.h")

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

# compiler flags
set(CMAKE_CXX_FLAGS -pthread)

#######################################
# PlatoonPlatoonProtocol Library Target
#######################################
set(SOURCE_FILES
        include/PlatoonProtocolLib/Protocol.h
        include/PlatoonProtocolLib/VehicleEndpoint.h
        include/PlatoonProtocolLib/Vehicle.h
        include/PlatoonProtocolLib/FollowerVehicle.h
        include/PlatoonProtocolLib/LeaderVehicle.h
        include/PlatoonProtocolLib/Utils.h
        include/PlatoonProtocolLib/PlatoonService.h
        include/PlatoonProtocolLib/PlatoonMessage.h
        include/PlatoonProtocolLib/json.hpp
        include/PlatoonProtocolLib/Definitions.h
        src/LeaderVehicle.cpp
        src/FollowerVehicle.cpp
        src/Vehicle.cpp
        src/VehicleEndpoint.cpp
        src/Protocol.cpp)

add_library(PlatoonProtocolLib SHARED ${SOURCE_FILES})

target_link_libraries(PlatoonProtocolLib NetworkingLib)

set(PUBLIC_HEADER_FILES
        include/PlatoonProtocolLib/FollowerVehicle.h
        include/PlatoonProtocolLib/json.hpp
        include/PlatoonProtocolLib/LeaderVehicle.h
        include/PlatoonProtocolLib/PlatoonMessage.h
        include/PlatoonProtocolLib/PlatoonService.h
        include/PlatoonProtocolLib/Protocol.h
        include/PlatoonProtocolLib/Utils.h
        include/PlatoonProtocolLib/Vehicle.h
        include/PlatoonProtocolLib/VehicleEndpoint.h
        include/PlatoonProtocolLib/Definitions.h
        ${CMAKE_CURRENT_BINARY_DIR}/PlatoonProtocolLibConfig.h)

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

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

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

set(CMAKE_PREFIX_PATH "${LOCAL_INSTALL_DIR}")

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

set(INSTALL_PACKAGE ON)
if(${INSTALL_PACKAGE})
    #############################
    # Specify install directories
    #############################
    set(CMAKE_INSTALL_PREFIX "${LOCAL_INSTALL_DIR}")
    set(INSTALL_LIB_DIR lib/PlatoonProtocolLib)
    set(INSTALL_INCLUDE_DIR include/PlatoonProtocolLib)
    set(INSTALL_CMAKE_DIR lib/PlatoonProtocolLib/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
    ####################################################
    # Add all targets to the build-tree export set
    export(TARGETS PlatoonProtocolLib
            FILE "${PROJECT_BINARY_DIR}/PlatoonProtocolLibTargets.cmake")

    # Export the package for use from the build-tree
    # (this registers the build-tree with a global CMake-registry)
    export(PACKAGE PlatoonProtocolLib)

    # 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 build tree
    # Put it inside the binary directory
    set(CONF_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
    configure_file(PlatoonProtocolLibConfig.cmake.in
            "${PROJECT_BINARY_DIR}/PlatoonProtocolLibConfig.cmake"
            @ONLY)

    # 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 "\${PLATOON_PROTOCOL_LIB_CMAKE_DIR}/${REL_INCLUDE_DIR}/..")
    configure_file(PlatoonProtocolLibConfig.cmake.in
            "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PlatoonProtocolLibConfig.cmake"
            @ONLY)

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

    ########################
    # Install PlatoonProtocolLib
    ########################
    # Install library and header files
    install(TARGETS PlatoonProtocolLib
            EXPORT PlatoonProtocolLibTargets
            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 PlatoonProtocolLibConfig.cmake and PlatoonProtocolLibConfigVersion.cmake
    install(FILES
            "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PlatoonProtocolLibConfig.cmake"
            "${PROJECT_BINARY_DIR}/PlatoonProtocolLibConfigVersion.cmake"
            DESTINATION "${INSTALL_CMAKE_DIR}")

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

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

target_link_libraries(PlatoonProtocolTest NetworkingLib)
target_include_directories(PlatoonProtocolTest PUBLIC ${NetworkingLib_INCLUDE_DIRS})

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