# Produces dependencies of Klampt
# Given KLAMPT_ROOT (optional)
# Produces
# - KLAMPT_LIBRARIES
# - KLAMPT_INCLUDE_DIRS
# - KLAMPT_DEFINITIONS
#

IF(NOT KLAMPT_ROOT)
  MESSAGE("KLAMPT_ROOT not defined, setting to .")
  SET(KLAMPT_ROOT .)
ENDIF( )

SET(KLAMPT_CPP_ROOT ${KLAMPT_ROOT}/Cpp)
SET(KLAMPT_PYTHON_ROOT ${KLAMPT_ROOT}/Python)
SET(KLAMPT_DEPENDENCIES "${KLAMPT_CPP_ROOT}/Dependencies" CACHE PATH "Klamp't C++ dependency folder" FORCE)

#pick up stuff from KrisLibrary/CMakeModules
list(APPEND CMAKE_MODULE_PATH ${KLAMPT_CPP_ROOT}/Dependencies/KrisLibrary/CMakeModules)

IF(WIN32)
  SET(KRISLIBRARY_ROOT ${KLAMPT_DEPENDENCIES} CACHE PATH "KrisLibrary parent directory" FORCE)
  #some weird windows setting regarding QT
  if(POLICY CMP0020)
	cmake_policy(SET CMP0020 OLD)
  endif()
  
  IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
    SET(KLAMPT_DEPENDENCY_LIB_DIR ${KLAMPT_DEPENDENCIES}/x64 CACHE PATH "Klamp't dependency folder for Windows libs")
  ELSE()
    SET(KLAMPT_DEPENDENCY_LIB_DIR ${KLAMPT_DEPENDENCIES} CACHE PATH "Klamp't dependency folder for Windows libs")
  ENDIF()
  
  SET(OPENGL_LIBRARY_DIR ${KLAMPT_DEPENDENCY_LIB_DIR})  #this is needed for glut32.lib / glui32.lib to be found in Cpp/Dependencies
  SET(GLEW_INCLUDE_DIR "${KLAMPT_DEPENDENCIES}/glew-2.0.0")
  SET(GLEW_LIBRARY "${KLAMPT_DEPENDENCY_LIB_DIR}/glew32.lib")
  
  FIND_PACKAGE(KrisLibrary REQUIRED)
  
  FIND_PATH(ODE_INCLUDE_DIR ode/ode.h
    PATHS ${KRISLIBRARY_ROOT}/ode-0.14/include  )
  FIND_LIBRARY(ODE_LIBRARY_DEBUG 
	NAMES ode_doubled
	PATHS ${KLAMPT_DEPENDENCY_LIB_DIR})
  FIND_LIBRARY(ODE_LIBRARY_RELEASE
	NAMES ode_double
	PATHS ${KLAMPT_DEPENDENCY_LIB_DIR})
  find_package_handle_standard_args(ODE
	DEFAULT_MSG
	ODE_INCLUDE_DIR
	ODE_LIBRARY_DEBUG
	ODE_LIBRARY_RELEASE)
  if(NOT ODE_FOUND)
    MESSAGE("ODE not found!")
  endif( )
  SET(ODE_DEFINITIONS "-DdDOUBLE" CACHE STRING "Open Dynamics Engine defines" FORCE)  
  SET(KLAMPT_DEFINITIONS ${KRISLIBRARY_DEFINITIONS} ${ODE_DEFINITIONS})
  SET(KLAMPT_LIBRARIES ${KRISLIBRARY_LIBRARIES} debug ${ODE_LIBRARY_DEBUG} optimized ${ODE_LIBRARY_RELEASE} )
  SET(KLAMPT_INCLUDE_DIRS  ${KRISLIBRARY_INCLUDE_DIRS} ${ODE_INCLUDE_DIR} )

ELSE(WIN32)

  SET(KRISLIBRARY_ROOT ${KLAMPT_DEPENDENCIES})
  FIND_PACKAGE(KrisLibrary REQUIRED)
  SET(KLAMPT_DEFINITIONS ${KRISLIBRARY_DEFINITIONS})
  SET(KLAMPT_INCLUDE_DIRS ${KRISLIBRARY_INCLUDE_DIRS})
  SET(KLAMPT_LIBRARIES ${KRISLIBRARY_LIBRARIES})

  # ODE
  SET(ODE_ROOT "${KLAMPT_DEPENDENCIES}/ode-0.14" CACHE PATH "Open Dynamics Engine path" FORCE)
  FIND_PACKAGE(ODE REQUIRED)
  IF(ODE_FOUND)
    MESSAGE("Open Dynamics Engine library found")
    MESSAGE("  Compiler definitions: ${ODE_DEFINITIONS}") 
    SET(KLAMPT_DEFINITIONS ${KLAMPT_DEFINITIONS} ${ODE_DEFINITIONS})
    SET(KLAMPT_INCLUDE_DIRS ${KLAMPT_INCLUDE_DIRS} ${ODE_INCLUDE_DIRS})
    SET(KLAMPT_LIBRARIES ${KLAMPT_LIBRARIES} ${ODE_LIBRARIES})

    #pthreads is needed -- but for some reason ODE does not report it
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
    FIND_LIBRARY(PTHREAD_LIBRARY "pthread")
    SET(KLAMPT_LIBRARIES ${KLAMPT_LIBRARIES} ${PTHREAD_LIBRARY})
  ENDIF(ODE_FOUND)

ENDIF(WIN32)

SET(ROSDEPS tf rosconsole roscpp roscpp_serialization rostime )
FIND_PACKAGE(ROS)
IF(ROS_FOUND)
  MESSAGE("ROS found, version " ${ROS_VERSION})
  LIST(APPEND KLAMPT_INCLUDE_DIRS ${ROS_INCLUDE_DIR})
  LIST(APPEND KLAMPT_LIBRARIES ${ROS_LIBRARIES})
  LIST(APPEND KLAMPT_DEFINITIONS "-DHAVE_ROS=1")
ENDIF(ROS_FOUND)

LIST(REMOVE_DUPLICATES KLAMPT_INCLUDE_DIRS)