# Find WDK
#
# This module defines
#  WDK_FOUND
#  WDK_INCLUDE_DIRS
#  WDK_LIBRARIES
#
# Copyright (c) 2012 I-maginer
# 
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA, or go to
# http://www.gnu.org/copyleft/lesser.txt
#

if(WIN32)
include(FindPkgMacros)
findpkg_begin(WDK)

# On a new cmake run, we do not need to be verbose
IF(WDK_INCLUDE_DIR AND WDK_LIBRARY)
	SET(WDK_FIND_QUIETLY TRUE)
ENDIF()

# log message
IF (NOT WDK_FIND_QUIETLY)
	MESSAGE(STATUS "Checking for WDK library")
ENDIF()

if( MSVC AND NOT MSVC90 )
  # Windows 10.x SDK
  get_filename_component(kit10_dir "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]" REALPATH)
  file(GLOB W10SDK_VERSIONS RELATIVE ${kit10_dir}/Include ${kit10_dir}/Include/10.*) 			# enumerate pre-release and not yet known release versions
  list(APPEND W10SDK_VERSIONS "10.0.10240.0" "10.0.14393.0" "10.0.15063.0" "10.0.16299.0")	# enumerate well known release versions
  list(REMOVE_DUPLICATES W10SDK_VERSIONS)
  list(SORT W10SDK_VERSIONS)
  list(REVERSE W10SDK_VERSIONS)																# sort from high to low
  if(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
    list(INSERT W10SDK_VERSIONS 0 ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION})				# prefer version passed by CMake, if any
  endif()
  foreach(W10SDK_VER ${W10SDK_VERSIONS})
    find_path(WDK_INCLUDE_DIR NAMES hidpi.h HINTS "${kit10_dir}/Include/${W10SDK_VER}/um" "C:/Program Files (x86)/Windows Kits/10/Include/${W10SDK_VER}/um" "C:/Program Files/Windows Kits/10/Include/${W10SDK_VER}/um")
  endforeach()
  # Windows 8.1 SDK
  if(NOT WDK_INCLUDE_DIR)
    find_path(WDK_INCLUDE_DIR NAMES hidpi.h HINTS "C:/Program Files (x86)/Windows Kits/8.1/include/um" "C:/Program Files/Windows Kits/8.1/include/um")
  endif()
  # Windows 8.0 SDK
  if(NOT WDK_INCLUDE_DIR)
    find_path(WDK_INCLUDE_DIR NAMES hidpi.h HINTS "C:/Program Files (x86)/Windows Kits/8.0/include/um" "C:/Program Files/Windows Kits/8.0/include/um")
  endif()
endif()

if(WDK_INCLUDE_DIR)
		# No need to specify full path to libraries, proper version would be found as part of SDK, in one of the following places
		# "C:/Program Files (x86)/Windows Kits/10/lib/${W10SDK_VER}/um/${MSVC_CXX_ARCHITECTURE_ID}/"
		# "C:/Program Files (x86)/Windows Kits/8.1/lib/winv6.3/um/${MSVC_CXX_ARCHITECTURE_ID}/"
		# "C:/Program Files (x86)/Windows Kits/8.0/lib/win8/um/${MSVC_CXX_ARCHITECTURE_ID}/"
		# "C:/Program Files (x86)/Windows Phone Kits/8.1/lib/${MSVC_CXX_ARCHITECTURE_ID}/"
		# "C:/Program Files (x86)/Windows Phone Kits/8.0/lib/${MSVC_CXX_ARCHITECTURE_ID}/"
		set(WDK_LIBRARY hid.lib SetupAPI.lib) 
		# but it is usefull to output selected version to the log
		message(STATUS "Found WDK headers: ${WDK_INCLUDE_DIR}")
	endif()

if( NOT WDK_INCLUDE_DIR OR NOT WDK_LIBRARY )
  MESSAGE(STATUS "Checking for WDK library legacy mode")
  create_search_paths(WDK)
  # redo search if prefix path changed
  clear_if_changed(WDK_INC_SEARCH_PATH
    WDK_LIBRARY
    WDK_INCLUDE_DIR
  )

  # Search for header files
  FIND_PATH(WDK_INCLUDE_DIR hidpi.h PATHS ${WDK_INC_SEARCH_PATH} PATH_SUFFIXES ${WDK_INCPATH_SUFFIX})

  # Search for libraries files
  FIND_LIBRARY(WDK_LIBRARY hid.lib PATHS ${WDK_LIB_SEARCH_PATH} PATH_SUFFIXES ${WDK_LIBPATH_SUFFIX})
  FIND_LIBRARY(WDK_SETUPAPI_LIBRARY setupapi PATHS ${WDK_LIB_SEARCH_PATH} PATH_SUFFIXES ${WDK_LIBPATH_SUFFIX})
  set(WDK_LIBRARY ${WDK_LIBRARY} ${WDK_SETUPAPI_LIBRARY})
endif() #legacy

# Configure libraries
SET(WDK_INCLUDE_DIRS ${WDK_INCLUDE_DIR} CACHE STRING "Directory containing Ms WDK header files")
SET(WDK_LIBRARIES ${WDK_LIBRARY} CACHE STRING "Ms WDK libraries files")

# Hide those variables in GUI
SET(WDK_INCLUDE_DIR ${WDK_INCLUDE_DIR} CACHE INTERNAL "")
SET(WDK_LIBRARY ${WDK_LIBRARY} CACHE INTERNAL "")
SET(WDK_SETUPAPI_LIBRARY ${WDK_SETUPAPI_LIBRARY} CACHE INTERNAL "")

findpkg_finish(WDK)
endif(WIN32)