cmake_minimum_required(VERSION 3.16.3)

# Set the project name and version
project(Lunarium VERSION 0.1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# specify the opengl version
set(OpenGL_MAJOR_VERSION 4)
set(OpenGL_MINOR_VERSION 5)

# Option to build without the editor
set(BUILD_NO_EDITOR 0)
option(NO_EDITOR "Building with editor" OFF)
if (NO_EDITOR)
    message(STATUS "Building without the editor")
    set(BUILD_NO_EDITOR 1)
endif ()

configure_file(LunariumConfig.h.in LunariumConfig.h)

# Source Files
set(LUNARIUM_SRC
"src/main.cpp"
"src/core/core.cpp"
"src/core/state.cpp"
"src/core/version.cpp"
"src/core/iRunMode.cpp"
"src/window/window.cpp"
"src/graphics/opengl/glGraphics.cpp"
"src/graphics/opengl/glText.cpp"
"src/graphics/opengl/glShader.cpp"
"src/graphics/internalFont.cpp"
"src/input/keyboard.cpp"
"src/input/inputManager.cpp"
"src/gui/gui.cpp"
"src/gui/logGui.cpp"
"src/gui/luaConsole.cpp"
"src/scripting/scriptManager.cpp"
"src/scripting/coreAPI.cpp"
)

# add the executable
add_executable(${PROJECT_NAME} ${LUNARIUM_SRC})

#       DOWNLOAD ALL SUBMODULES
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
    option(GIT_SUBMODULE "Check submodules during build" ON)
    if(GIT_SUBMODULE)
        message(STATUS "Submodule update")
        execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                        RESULT_VARIABLE GIT_SUBMOD_RESULT)
        if(NOT GIT_SUBMOD_RESULT EQUAL "0")
            message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
        endif()
    endif()
endif()

#       CHECK THAT ALL SUBMODULES EXIST
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/glfw/CMakeLists.txt")
    message(FATAL_ERROR "GLFW submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/glm/CMakeLists.txt")
    message(FATAL_ERROR "GLM submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/lua/CMakeLists.txt")
    message(FATAL_ERROR "LUA submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/pugixml/CMakeLists.txt")
    message(FATAL_ERROR "PUGIXML submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/freetype/CMakeLists.txt")
    message(FATAL_ERROR "FREETYPE submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/box2d/CMakeLists.txt")
    message(FATAL_ERROR "BOX2D submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

# add GLFW
add_subdirectory(external/glfw)

# add glad2
add_subdirectory(external/glad/src)

# add GLM
add_subdirectory(external/glm)

# add dearimgui
add_subdirectory(src/internal_libs/dearimgui)

# add utils
add_subdirectory(src/internal_libs/utils)

# add assets
add_subdirectory(src/internal_libs/assets)

# add lua -- https://github.com/walterschell/Lua
add_subdirectory(external/lua)

# add pugixml
add_subdirectory(external/pugixml)

# add freetype
add_subdirectory(external/freetype)

# add box2d
add_subdirectory(external/box2d)

# add run mode tester
add_subdirectory(src/run_modes/tester)

target_include_directories(${PROJECT_NAME} 
    PUBLIC "${PROJECT_BINARY_DIR}" 
    PUBLIC src
    PUBLIC src/internal_libs
    PUBLIC external
    PUBLIC external/glfw/include
    PUBLIC external/glm
    PUBLIC external/lua/lua5.4.3/include
    PUBLIC src/internal_libs/dearimgui
    PUBLIC external/pugixml/src
    PUBLIC external/glad/include
    PUBLIC external/freetype/include
    PUBLIC external/box2d/include
)

target_link_directories(${PROJECT_NAME}
    PRIVATE external/glfw/src
    PRIVATE external/glm
    PRIVATE src/internal_libs/dearimgui
    PRIVATE src/internal_libs/utils
    PRIVATE src/internal_libs/assets
    PRIVATE src/run_modes/tester
    PRIVATE external/glad/src
    PRIVATE external/freetype/src
    PRIVATE external/box2d/src
)

target_link_libraries(${PROJECT_NAME} glfw glad glm dearimgui utils assets lua_static pugixml freetype box2d tester)

if(WIN32)
    target_link_libraries(${PROJECT_NAME} opengl32.lib)
elseif(UNIX)
    target_link_libraries(${PROJECT_NAME} X11 GL)
endif()
