cmake_minimum_required(VERSION 3.10)
project(TerraVisor)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Define output directories for different configurations
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

#include(C:/users/jmchr/vcpkg/scripts/buildsystems/vcpkg.cmake)

# Use the environment variable if it is set
if(DEFINED ENV{VCPKG_TOOLCHAIN})
    include("$ENV{VCPKG_TOOLCHAIN}")
endif()

# Find packages using vcpkg
find_package(GLEW CONFIG REQUIRED)
find_package(glfw3 CONFIG REQUIRED)
find_package(GLM CONFIG REQUIRED)
find_package(assimp CONFIG REQUIRED)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}")

# Add source files
file(GLOB_RECURSE SOURCES
  "${CMAKE_SOURCE_DIR}/source/*.cpp"
)

# Add header files
file(GLOB_RECURSE HEADERS
  "${CMAKE_SOURCE_DIR}/include/*.h"
)

# Add shader files
file(GLOB SHADERS
  "${CMAKE_SOURCE_DIR}/shaders/*.glsl"
)

# Add ImGui source files
file(GLOB IMGUI_SOURCES
  "${CMAKE_SOURCE_DIR}/imgui/*.cpp"
  "${CMAKE_SOURCE_DIR}/imgui/backends/imgui_impl_glfw.cpp"
  "${CMAKE_SOURCE_DIR}/imgui/backends/imgui_impl_opengl3.cpp"
)

# Add the executable
add_executable(TerraVisor ${SOURCES} ${HEADERS} ${IMGUI_SOURCES})

# Include directories
include_directories(
  ${CMAKE_SOURCE_DIR}/include
  ${CMAKE_SOURCE_DIR}/imgui
  ${CMAKE_SOURCE_DIR}/imgui/backends  
)

# Link libraries using vcpkg
target_link_libraries(TerraVisor PRIVATE GLEW::GLEW glfw glm::glm assimp::assimp)

# Custom target for shaders
add_custom_target(copy_shaders ALL
  COMMAND ${CMAKE_COMMAND} -E copy_directory
  ${CMAKE_SOURCE_DIR}/shaders ${CMAKE_BINARY_DIR}/shaders
)

# Set subsystem based on build type
if (WIN32)
    if (CMAKE_BUILD_TYPE STREQUAL "Release")
        set_target_properties(TerraVisor PROPERTIES LINK_FLAGS "-Wl,--subsystem,windows")
    else()
        set_target_properties(TerraVisor PROPERTIES LINK_FLAGS "-Wl,--subsystem,console")
    endif()
endif()

# Ensure shaders are copied before building the executable
add_dependencies(TerraVisor copy_shaders)
