cmake_minimum_required(VERSION 3.12)

set(THIS_PROJECT "MyGst")
project(${THIS_PROJECT})

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Release>:Release>")

MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(THIS_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

message("THIS_ROOT = ${THIS_ROOT}")

##################### PkgConfig BEGIN
set(MY_GST_PATH $ENV{MY_GST_PATH})
if (DEFINED MY_GST_PATH) 
    message("Env variable MY_GST_PATH=[${MY_GST_PATH}]")
else()
    message(FATAL_ERROR "Env variable MY_GST_PATH is NOT DEFINED. Falling back to defualt value")
    # set(MY_GST_PATH /home/admin1/gst/bin)
    # message("Now the value of of MY_NDI_SDK_PATH=[${MY_GST_PATH}]")
endif()
set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH ON)
# set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${MY_GST_PATH}/lib/pkgconfig")
find_package(PkgConfig REQUIRED)
if ( NOT (PKGCONFIG_FOUND))
    message(FATAL_ERROR "Please Install PkgConfig: CMake will Exit")
endif()
##################### PkgConfig END
##################### OpenCV BEGIN
set(OpenCV_DIR C:/OpenCV3/build)
find_package(OpenCV 3.4.16 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
##################### OpenCV END
##################### OSC BEGIN
# TODO: Build oscpack normally in the project's directory
set(OSC_DIR C:/Users/ivan/Source/Repos/oscpack_1_1_0)
set(OSC_LIB_DIR ${OSC_DIR}/build/Release)

include_directories(
${OSC_DIR}
${OSC_LIB_DIR})

IF(WIN32)
 set(IpSystemTypePath ${OSC_DIR}/ip/win32)
 set(LIBS ${LIBS} Ws2_32 winmm)
ELSE(WIN32)
 set(IpSystemTypePath ${OSC_DIR}/ip/posix)
ENDIF(WIN32)

ADD_LIBRARY(oscpack 
${OSC_DIR}/ip/IpEndpointName.h
${OSC_DIR}/ip/IpEndpointName.cpp
${OSC_DIR}/ip/NetworkingUtils.h

${IpSystemTypePath}/NetworkingUtils.cpp

${OSC_DIR}/ip/UdpSocket.h
${IpSystemTypePath}/UdpSocket.cpp

${OSC_DIR}/ip/PacketListener.h
${OSC_DIR}/ip/TimerListener.h

${OSC_DIR}/osc/OscTypes.h
${OSC_DIR}/osc/OscTypes.cpp 
${OSC_DIR}/osc/OscHostEndianness.h
${OSC_DIR}/osc/OscException.h
${OSC_DIR}/osc/OscPacketListener.h
${OSC_DIR}/osc/MessageMappingOscPacketListener.h
${OSC_DIR}/osc/OscReceivedElements.h
${OSC_DIR}/osc/OscReceivedElements.cpp
${OSC_DIR}/osc/OscPrintReceivedElements.h
${OSC_DIR}/osc/OscPrintReceivedElements.cpp
${OSC_DIR}/osc/OscOutboundPacketStream.h
${OSC_DIR}/osc/OscOutboundPacketStream.cpp
)

target_link_libraries(oscpack
${OSC_LIB_DIR}/oscpack.lib)

##################### OSC END
##################### ORB_SLAM3 BEGIN
set(ORB_SLAM3_DIR C:/Users/ivan/Source/Repos/ORB_SLAM3)
set(Boost_INCLUDE_DIR C:/Users/ivan/Source/Repos/ORB-SLAM3forWindows/Thirdparty/boost_1_77_0)
set(Pangolin_DIR ${ORB_SLAM3_DIR}/Thirdparty/Pangolin/build)
set(Eigen3_DIR C:/Users/ivan/Source/Repos/ORB-SLAM3forWindows/Thirdparty/eigen/build)

find_package(Eigen3 3.1.0 REQUIRED)
find_package(Pangolin REQUIRED)
find_package(Boost)
find_package(realsense2)

include_directories(
${ORB_SLAM3_DIR}
${ORB_SLAM3_DIR}/include
${ORB_SLAM3_DIR}/include/CameraModels
${ORB_SLAM3_DIR}/Thirdparty/g2o
${EIGEN3_INCLUDE_DIR}
${Pangolin_INCLUDE_DIRS}
${Boost_INCLUDE_DIR}
${Boost_INCLUDE_DIR}/stage/lib
)


##################### ORB_SLAM3 END
##################### GStreamer BEGIN
# gstreamer-1.0
pkg_check_modules(GST REQUIRED gstreamer-1.0>=1.18.5)
include_directories("${GST_INCLUDE_DIRS}")
link_directories(BEFORE ${GST_LIBDIR})
# gstreamer-app-1.0
pkg_check_modules(GST_APP REQUIRED gstreamer-app-1.0>=1.18.5)
include_directories("${GST_APP_INCLUDE_DIRS}")
link_directories(BEFORE ${GST_APP_LIBDIR})
# gstreamer-audio-1.0
pkg_check_modules(GST_AUDIO REQUIRED gstreamer-audio-1.0>=1.18.5)
include_directories("${GST_AUDIO_INCLUDE_DIRS}")
link_directories(BEFORE ${GST_AUDIO_LIBDIR})
# gstreamer-video-1.0
pkg_check_modules(GST_VIDEO REQUIRED gstreamer-video-1.0>=1.18.5)
include_directories("${GST_VIDEO_INCLUDE_DIRS}")
link_directories(BEFORE ${GST_VIDEO_LIBDIR})
##################### GStreamer END
# dump everything
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
        message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
##################### NDI Begin
set(MY_NDI_SDK_PATH $ENV{NDI_SDK_DIR})
if (DEFINED MY_NDI_SDK_PATH)
    message("Env variable MY_NDI_SDK_PATH=[${MY_NDI_SDK_PATH}]")
else()
    message("Env variable MY_NDI_SDK_PATH is NOT DEFINED. Falling back to defualt value")
    IF (WIN32)
        set(MY_NDI_SDK_PATH "C:\\Program Files\\NDI\\NDI 5 SDK")
    ELSE()
        set(MY_NDI_SDK_PATH "/home/admin1/gst/from_source/NDI_SDK_for_Linux")
    ENDIF()
    message("Now the value of MY_NDI_SDK_PATH=[${MY_NDI_SDK_PATH}]")
endif()
set(MY_NDI_INCLUDE "${MY_NDI_SDK_PATH}/include")
include_directories(${MY_NDI_INCLUDE})
IF (WIN32)
    add_library(MY_NDI_LIBS SHARED IMPORTED)
    set_target_properties(MY_NDI_LIBS PROPERTIES
        IMPORTED_LOCATION "${MY_NDI_SDK_PATH}/Bin/x64/Processing.NDI.Lib.x64.dll"
        IMPORTED_IMPLIB "${MY_NDI_SDK_PATH}/Lib/x64/Processing.NDI.Lib.x64.lib"
    )
ELSE()
    add_library(MY_NDI_LIBS SHARED IMPORTED)
    set_property(TARGET MY_NDI_LIBS
        PROPERTY IMPORTED_LOCATION "${MY_NDI_SDK_PATH}/lib/x86_64-linux-gnu/libndi.so"
    )
ENDIF()

##################### NDI End
##################### Debug Begin
#get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
#foreach(dir ${dirs})
#    message(STATUS "This directory is in INCLUDE_DIRECTORIES variable='${dir}'")
#endforeach()
##################### Debug End

add_executable(gst_ndi
    gst_ndi.h gst_ndi.cpp
)
target_link_libraries(gst_ndi ${GST_LIBRARIES})

add_executable(gst_get_ndi
	 gst_get_ndi.cpp
)
target_link_libraries(gst_get_ndi
         ${GST_LIBRARIES} ${GST_APP_LIBRARIES} ${GST_AUDIO_LIBRARIES}
         ${OpenCV_LIBRARIES}
         ${GST_VIDEO_LIBRARIES}
         oscpack
         ${LIBS}
         ${ORB_SLAM3_DIR}/build

        #${OpenCV_LIBS}
        ${EIGEN3_LIBS}
        ${Pangolin_LIBRARIES}
        ${Boost_LIBS}
        ${ORB_SLAM3_DIR}/Thirdparty/DBoW2/lib/Release/DBoW2.lib
        ${ORB_SLAM3_DIR}/Thirdparty/g2o/build/Release/g2o.lib
        ${ORB_SLAM3_DIR}/build/Release/ORB_SLAM3.lib
        -lboost_serialization
        -lcrypto
)

#add_executable(try_1
# try_1.cpp
#)
#target_link_libraries(try_1
#	 ${GST_LIBRARIES} ${OpenCV_LIBRARIES} ${GST_APP_LIBRARIES}
#)

add_executable(my_ndi
    my_ndi.cpp
)
target_include_directories(my_ndi PRIVATE ${MY_NDI_INCLUDE})
target_link_libraries(my_ndi MY_NDI_LIBS)

add_executable(my_ndi_source
        my_ndi_source.cpp
)
target_include_directories(my_ndi_source PRIVATE ${MY_NDI_INCLUDE})
target_link_libraries(my_ndi_source
MY_NDI_LIBS
${OpenCV_LIBRARIES}

)


install(TARGETS my_ndi my_ndi_source gst_get_ndi gst_ndi
    CONFIGURATIONS Release
    RUNTIME DESTINATION ${THIS_ROOT}/../deploy-win32
)


