# - Try to find fbxsdk lib
#
# This module supports requiring a minimum version, e.g. you can do
#   find_package(fbxsdk 3.1.2)
# to require version 3.1.2 or newer of fbxsdk.
#
# Once done this will define
#
#  fbxsdk_FOUND - system has eigen lib with correct version
#  fbxsdk_INCLUDE_DIR - the eigen include directory
#  fbxsdk_VERSION - eigen version

# Copyright (c) 2006, 2007 Montel Laurent, <montel@kde.org>
# Copyright (c) 2008, 2009 Gael Guennebaud, <g.gael@free.fr>
# Copyright (c) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
# Redistribution and use is allowed according to the terms of the 2-clause BSD license.

if(NOT fbxsdk_FIND_VERSION)
  if(NOT fbxsdk_FIND_VERSION_MAJOR)
    set(fbxsdk_FIND_VERSION_MAJOR 2)
  endif(NOT fbxsdk_FIND_VERSION_MAJOR)
  if(NOT fbxsdk_FIND_VERSION_MINOR)
    set(fbxsdk_FIND_VERSION_MINOR 91)
  endif(NOT fbxsdk_FIND_VERSION_MINOR)
  if(NOT fbxsdk_FIND_VERSION_PATCH)
    set(fbxsdk_FIND_VERSION_PATCH 0)
  endif(NOT fbxsdk_FIND_VERSION_PATCH)

  set(fbxsdk_FIND_VERSION "${fbxsdk_FIND_VERSION_MAJOR}.${fbxsdk_FIND_VERSION_MINOR}.${fbxsdk_FIND_VERSION_PATCH}")
endif(NOT fbxsdk_FIND_VERSION)

macro(_fbxsdk_check_version)
  file(READ "${fbxsdk_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _fbxsdk_version_header)

  string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _fbxsdk_world_version_match "${_fbxsdk_version_header}")
  set(fbxsdk_WORLD_VERSION "${CMAKE_MATCH_1}")
  string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _fbxsdk_major_version_match "${_fbxsdk_version_header}")
  set(fbxsdk_MAJOR_VERSION "${CMAKE_MATCH_1}")
  string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _fbxsdk_minor_version_match "${_fbxsdk_version_header}")
  set(fbxsdk_MINOR_VERSION "${CMAKE_MATCH_1}")

  set(fbxsdk_VERSION ${fbxsdk_WORLD_VERSION}.${fbxsdk_MAJOR_VERSION}.${fbxsdk_MINOR_VERSION})
  if(${fbxsdk_VERSION} VERSION_LESS ${fbxsdk_FIND_VERSION})
    set(fbxsdk_VERSION_OK FALSE)
  else(${fbxsdk_VERSION} VERSION_LESS ${fbxsdk_FIND_VERSION})
    set(fbxsdk_VERSION_OK TRUE)
  endif(${fbxsdk_VERSION} VERSION_LESS ${fbxsdk_FIND_VERSION})

  if(NOT fbxsdk_VERSION_OK)

    message(STATUS "fbxsdk version ${fbxsdk_VERSION} found in ${fbxsdk_INCLUDE_DIR}, "
                   "but at least version ${fbxsdk_FIND_VERSION} is required")
  endif(NOT fbxsdk_VERSION_OK)
endmacro(_fbxsdk_check_version)

if (fbxsdk_INCLUDE_DIR)

  # in cache already
  _fbxsdk_check_version()
  set(fbxsdk_FOUND ${fbxsdk_VERSION_OK})

else (fbxsdk_INCLUDE_DIR)

  find_path(fbxsdk_INCLUDE_DIR NAMES signature_of_fbxsdk_matrix_library
      PATHS
	  ${PROJECT_SOURCE_DIR}/extern/eigen
      ${CMAKE_INSTALL_PREFIX}/include
      ${KDE4_INCLUDE_DIR}	  
      PATH_SUFFIXES fbxsdk eigen
    )

  if(fbxsdk_INCLUDE_DIR)
    _fbxsdk_check_version()
  endif(fbxsdk_INCLUDE_DIR)

  include(FindPackageHandleStandardArgs)
  find_package_handle_standard_args(fbxsdk DEFAULT_MSG fbxsdk_INCLUDE_DIR fbxsdk_VERSION_OK)

  mark_as_advanced(fbxsdk_INCLUDE_DIR)

endif(fbxsdk_INCLUDE_DIR)

