#############################################################################
#
# ViSP, open source Visual Servoing Platform software.
# Copyright (C) 2005 - 2023 by Inria. All rights reserved.
#
# This software is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# See the file LICENSE.txt at the root directory of this source
# distribution for additional information about the GNU GPL.
#
# For using ViSP with software that can not be combined with the GNU
# GPL, please contact Inria about acquiring a ViSP Professional
# Edition License.
#
# See https://visp.inria.fr for more information.
#
# This software was developed at:
# Inria Rennes - Bretagne Atlantique
# Campus Universitaire de Beaulieu
# 35042 Rennes Cedex
# France
#
# If you have questions regarding the use of this file, please contact
# Inria at visp@inria.fr
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Description:
# CMake package config file for ViSP.
#
# ** File generated automatically, do not modify **
#
# This file will define the following CMake variables:
#   - VISP_INCLUDE_DIRS   : ViSP and third-party include directories
#   - VISP_LIBRARIES      : ViSP library to link against. Third-party libraries are
#                           linked automatically thanks to cmake export file VISPTargets.cmake
#   - VISP_VERSION_STRING : Full ViSP version that is build. Example: "2.10.0"
#   - VISP_VERSION_MAJOR  : Major version part of VISP_VERSION. Example: "2"
#   - VISP_VERSION_MINOR  : Minor version part of VISP_VERSION. Example: "10"
#   - VISP_VERSION_PATCH  : Patch version part of VISP_VERSION. Example: "0"
#
# Advanced variables:
#   - VISP_SHARED        : Use ViSP as shared library
#   - VISP_CONFIG_PATH   : Path to this VISPConfig.cmake
#   - VISP_FIND_QUIETLY  : If set to TRUE turn off messages during configuration
#   - VISP_USE_FILE      : File to include to use ViSP without specific cmake code
#
# Windows specific variables:
#   - VISP_STATIC        : If set to TRUE uses ViSP static library (.lib) rather then dynamic (.dll)
#
# Typical usage in user project:
#
#   find_package(VISP)
#   include_directories(${VISP_INCLUDE_DIRS})
#   target_link_libraries(MY_TARGET_NAME ${VISP_LIBRARIES})
#
# It is also possible to build your project using VISP_USE_FILE.
#
#   find_package(VISP)
#   if(VISP_FOUND)
#     include(${VISP_USE_FILE})
#   endif()
#
#############################################################################

if(NOT DEFINED VISP_STATIC)
  # look for global setting
  if (NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS)
    set(VISP_STATIC OFF)
  else()
    set(VISP_STATIC ON)
  endif()
endif()

function(check_one_config RES)
  set($ { RES } "" PARENT_SCOPE)
  if (NOT VISP_RUNTIME OR NOT VISP_ARCH)
    return()
  endif()
  set(candidates)
  if (VISP_STATIC)
    list(APPEND candidates "${VISP_ARCH}/${VISP_RUNTIME}/staticlib")
  endif()
  list(APPEND candidates "${VISP_ARCH}/${VISP_RUNTIME}/lib")
  foreach(c $ { candidates })
    set(p "${VISP_CONFIG_PATH}/${c}")
    if (EXISTS "${p}/VISPConfig.cmake")
      set($ { RES } "${p}" PARENT_SCOPE)
      return()
    endif()
  endforeach()
endfunction()

get_filename_component(VISP_CONFIG_PATH "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY)

function(check_one_config RES)
  set(${RES} "" PARENT_SCOPE)
  if(NOT VISP_RUNTIME OR NOT VISP_ARCH)
    return()
  endif()
  set(candidates)
  if(VISP_STATIC)
    list(APPEND candidates "${VISP_ARCH}/${VISP_RUNTIME}/staticlib")
  endif()
  list(APPEND candidates "${VISP_ARCH}/${VISP_RUNTIME}/lib")
  foreach(c ${candidates})
    set(p "${VISP_CONFIG_PATH}/${c}")
    if(EXISTS "${p}/VISPConfig.cmake")
      set(${RES} "${p}" PARENT_SCOPE)
      return()
    endif()
  endforeach()
endfunction()

# similar code exist in VISPDetectPlatform.cmake
if(DEFINED VISP_ARCH AND DEFINED VISP_RUNTIME)
  # custom overridden values
elseif(MSVC)
  if(CMAKE_CL_64)
    set(VISP_ARCH x64)
  elseif((CMAKE_GENERATOR MATCHES "ARM") OR ("${arch_hint}" STREQUAL "ARM") OR (CMAKE_VS_EFFECTIVE_PLATFORMS MATCHES "ARM|arm"))
    set(VISP_ARCH ARM)
  else()
    set(VISP_ARCH x86)
  endif()
  if(MSVC_VERSION EQUAL 1400)
    set(VISP_RUNTIME vc8)
  elseif(MSVC_VERSION EQUAL 1500)
    set(VISP_RUNTIME vc9)
  elseif(MSVC_VERSION EQUAL 1600)
    set(VISP_RUNTIME vc10)
  elseif(MSVC_VERSION EQUAL 1700)
    set(VISP_RUNTIME vc11)
  elseif(MSVC_VERSION EQUAL 1800)
    set(VISP_RUNTIME vc12)
  elseif(MSVC_VERSION EQUAL 1900)
    set(VISP_RUNTIME vc14)
  elseif(MSVC_VERSION MATCHES "^191[0-9]$")
    set(VISP_RUNTIME vc15)
    check_one_config(has_VS2017)
    if(NOT has_VS2017)
      set(VISP_RUNTIME vc14) # selecting previous compatible runtime version
    endif()
  elseif(MSVC_VERSION MATCHES "^192[0-9]$")
    set(VISP_RUNTIME vc16)
    check_one_config(has_VS2019)
    if(NOT has_VS2019)
      set(VISP_RUNTIME vc15) # selecting previous compatible runtime version
      check_one_config(has_VS2017)
      if(NOT has_VS2017)
        set(VISP_RUNTIME vc14) # selecting previous compatible runtime version
      endif()
    endif()
  elseif(MSVC_VERSION MATCHES "^193[0-9]$")
    set(VISP_RUNTIME vc17)
    check_one_config(has_VS2022)
    if(NOT has_VS2022)
     set(VISP_RUNTIME vc16)
     check_one_config(has_VS2019)
     if(NOT has_VS2019)
       set(VISP_RUNTIME vc15) # selecting previous compatible runtime version
       check_one_config(has_VS2017)
       if(NOT has_VS2017)
         set(VISP_RUNTIME vc14) # selecting previous compatible runtime version
       endif()
     endif()
    endif()
  else()
    message(WARNING "ViSP does not recognize MSVC_VERSION \"${MSVC_VERSION}\". Cannot set VISP_RUNTIME")
  endif()
elseif(MINGW)
  set(VISP_RUNTIME mingw)

  if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
    set(VISP_ARCH x64)
  else()
    set(VISP_ARCH x86)
  endif()
endif()

check_one_config(VISP_LIB_PATH)

if(NOT VISP_FIND_QUIETLY)
  message(STATUS "ViSP ARCH: ${VISP_ARCH}")
  message(STATUS "ViSP RUNTIME: ${VISP_RUNTIME}")
  message(STATUS "ViSP STATIC: ${VISP_STATIC}")
endif()

if(VISP_LIB_PATH AND EXISTS "${VISP_LIB_PATH}/VISPConfig.cmake")
  include("${VISP_LIB_PATH}/VISPConfig.cmake")

  if(NOT VISP_FIND_QUIETLY)
    message(STATUS "Found VISP ${VISP_VERSION} in ${VISP_LIB_PATH}")
    if(NOT VISP_LIB_PATH MATCHES "/staticlib")
      get_filename_component(_VISP_LIB_PATH "${VISP_LIB_PATH}/../bin" ABSOLUTE)
      file(TO_NATIVE_PATH "${_VISP_LIB_PATH}" _VISP_LIB_PATH)
      message(STATUS "You might need to add ${_VISP_LIB_PATH} to your PATH to be able to run your applications.")
    endif()
  endif()
else()
  if(NOT VISP_FIND_QUIETLY)
    message(WARNING
"Found ViSP for Windows but it has no binaries compatible with your configuration.
You should manually point CMake variable VISP_DIR to your build of ViSP library."
    )
  endif()
  set(VISP_FOUND FALSE)
endif()
