cmake_minimum_required(VERSION 3.8)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_LIST_DIR})

# use dir name for target
get_filename_component(target ${CMAKE_CURRENT_LIST_DIR} NAME)
project(${target})

include(FindOpenGL)
find_package(EasyARSense REQUIRED NO_DEFAULT_PATH PATHS ${CMAKE_CURRENT_LIST_DIR})
find_package(Qt5 5.0 REQUIRED COMPONENTS Core Gui Widgets REQUIRED)
mark_as_advanced(FORCE Qt5Core_DIR Qt5Gui_DIR Qt5Widgets_DIR)

# add library first
add_executable(${target} "")

# add source
file(GLOB srcs *.c* *.h* glew/src/*.c* glew/include/GL/*.h*)
target_sources(${target} PRIVATE ${srcs})
source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${srcs})

file(GLOB_RECURSE rcs *.rc *.ico)
file(GLOB_RECURSE qrcs *.qrc *.ui)
target_sources(${target} PRIVATE ${rcs} ${qrcs})
source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX res FILES ${rcs} ${qrcs})

# compile options
target_include_directories(${target} PRIVATE glew/include)
target_compile_definitions(${target} PRIVATE GLEW_STATIC)

# link options
target_link_libraries(${target} PRIVATE EasyAR::Sense Qt5::Widgets OpenGL::GL)

# target properties
set_target_properties(${target} PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
  CXX_STANDARD 17
  CXX_STANDARD_REQUIRED ON
  WIN32_EXECUTABLE ON
  MACOSX_BUNDLE ON
  AUTOMOC ON
  AUTORCC ON
  AUTOUIC ON
)
set_directory_properties(PROPERTIES VS_STARTUP_PROJECT ${target})

# post build
get_target_property(exe_output_path ${target} RUNTIME_OUTPUT_DIRECTORY)
if(WIN32)
  set(EasyAR_RUNTIMES)
  get_target_property(target_location EasyAR::Sense LOCATION)
  if(target_location)
    list(APPEND EasyAR_RUNTIMES ${target_location})
  endif()
  foreach(dll ${EasyAR_RUNTIMES})
    get_filename_component(dll_name ${dll} NAME)
    add_custom_command(TARGET ${target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND}
      ARGS    -E copy_if_different "${dll}" "${exe_output_path}/${CMAKE_CFG_INTDIR}/${dll_name}"
      DEPENDS ${dll}
      COMMENT "copying ${dll_name}"
    )
  endforeach()
endif()
add_custom_command(TARGET ${target}
  POST_BUILD
  COMMAND ${CMAKE_COMMAND}
  ARGS    -E copy_directory "${CMAKE_CURRENT_LIST_DIR}/assets" "${exe_output_path}/${CMAKE_CFG_INTDIR}"
  COMMENT "copying assets"
)
