#=============================================================================
# Copyright 2013-2014 Istituto Italiano di Tecnologia (IIT)
#   Authors: Daniele E. Domenichelli <daniele.domenichelli@iit.it>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of YCM, substitute the full
#  License text for the above reference.)


set(YCM_VERSION @YCM_VERSION@)
set(YCM_VERSION_MAJOR @YCM_VERSION_MAJOR@)
set(YCM_VERSION_MINOR @YCM_VERSION_MINOR@)
set(YCM_VERSION_PATCH @YCM_VERSION_PATCH@)
set(YCM_VERSION_REVISION @YCM_VERSION_REVISION@)
set(YCM_VERSION_DATE @YCM_VERSION_DATE@)
set(YCM_VERSION_DATE_REVISION @YCM_VERSION_DATE_REVISION@)
set(YCM_VERSION_API @YCM_VERSION_API@)
set(YCM_VERSION_SHORT @YCM_VERSION_SHORT@)
set(YCM_VERSION_SOURCE @YCM_VERSION_SOURCE@)
set(YCM_VERSION_DIRTY @YCM_VERSION_DIRTY@)

@PACKAGE_INIT@

set_and_check(YCM_MODULE_DIR "@PACKAGE_YCM_MODULE_DIR@")

unset(YCM_MODULE_PATH)
list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/modules")
list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/find-modules")
list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/build-modules")
list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/style-modules")

# "Newer modules should be found earlier in case of files with the same name,
# therefore the order is
#  - proposed
#  - next
#  - old releases (newer releases first)
#  - 3rd party
#  - deprecated

# Use modules from CMake (default ON)
if(NOT DEFINED YCM_USE_CMAKE OR YCM_USE_CMAKE)

  # Use modules including unmerged CMake patches (default OFF)
  if(YCM_USE_CMAKE_PROPOSED)
    list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/cmake-proposed/Modules")
  endif()

  # Use modules from unreleased CMake (default ON)
  if(NOT DEFINED YCM_USE_CMAKE_NEXT OR YCM_USE_CMAKE_NEXT)
    list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/cmake-next/Modules")
  endif()

  # Use modules from specific CMake versions (default ON)
  foreach(_version 3.15
                   3.14
                   3.13)
    if(EXISTS "${YCM_MODULE_DIR}/cmake-${_version}"
      AND CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS ${_version}
      AND CMAKE_VERSION VERSION_LESS ${_version})
      string(REPLACE "." "_" _VERSION ${_version})
      if(NOT DEFINED YCM_USE_CMAKE_${_VERSION} OR YCM_USE_CMAKE_${_VERSION})
        list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/cmake-${_version}/Modules")
      endif()
    endif()
  endforeach()

endif()

# Use YCM 3rd party modules (default ON)
if(EXISTS "${YCM_MODULE_DIR}/3rdparty")
  if(NOT DEFINED YCM_USE_3RDPARTY OR YCM_USE_3RDPARTY)
    list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/3rdparty")
  endif()
endif()

# Use YCM deprecated modules (default ON)
if(EXISTS "${YCM_MODULE_DIR}/deprecated")
  if(NOT DEFINED YCM_USE_DEPRECATED OR YCM_USE_DEPRECATED)
    list(APPEND YCM_MODULE_PATH "${YCM_MODULE_DIR}/deprecated")
    set(YCM_NO_DEPRECATED 0)
  endif()
endif()

list(APPEND CMAKE_MODULE_PATH "${YCM_MODULE_PATH}")


if(COMMAND set_package_properties)
  set_package_properties(YCM PROPERTIES DESCRIPTION "A collection of CMake modules and utilities for YARP and friends"
                                        URL "https://github.com/robotology/ycm")
endif()


if(NOT YCM_FIND_QUIETLY)
  message(STATUS "Found YCM: ${YCM_DIR} (found version \"${YCM_VERSION}\")")
endif()
