#
#  For more information, please see: http://software.sci.utah.edu
#
#  The MIT License
#
#  Copyright (c) 2020 Scientific Computing and Imaging Institute,
#  University of Utah.
#
#  Permission is hereby granted, free of charge, to any person obtaining a
#  copy of this software and associated documentation files (the "Software"),
#  to deal in the Software without restriction, including without limitation
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
#  and/or sell copies of the Software, and to permit persons to whom the
#  Software is furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included
#  in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#  DEALINGS IN THE SOFTWARE.
#


MESSAGE(STATUS "Configuring 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()

IF(BUILD_TESTING)
  if(NOT EXISTS "${PROJECT_SOURCE_DIR}/Externals/submodules/googletest/CMakeLists.txt")
    message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
  endif()
ENDIF()

IF(BUILD_TESTING)
  SET(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
  SET(_saved_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
  SET(_saved_CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE})
  SET(BUILD_SHARED_LIBS OFF)
  SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
  SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  ADD_SUBDIRECTORY(googletest)
  SET(BUILD_SHARED_LIBS ${_saved_BUILD_SHARED_LIBS})
  SET(CMAKE_POSITION_INDEPENDENT_CODE ${_saved_CMAKE_POSITION_INDEPENDENT_CODE})
ENDIF()

#IF(WITH_OSPRAY)
#  set(OSPRAY_TASKING_SYSTEM "TBB")
#  set(OSPRAY_ENABLE_TESTING OFF)
#  set(OSPRAY_SG_CHOMBO OFF)
#  set(OSPRAY_SG_OPENIMAGEIO OFF)
#  set(OSPRAY_SG_VTK OFF)
#  set(ISPC_EXECUTABLE ${Ispc_DIR}/ispc)
#  ADD_SUBDIRECTORY(ospray)
#ENDIF()
