cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
# FindPkgConfig was introduced after 2.4.6, so we provide a copy of it
#include(FindPkgConfig)
include($ENV{ROS_ROOT}/core/rosbuild/FindPkgConfig.cmake)

set(ROS_BUILD_TYPE RelWithDebInfo)
#set(ROS_BUILD_TYPE Debug)

rosbuild_init()

rosbuild_add_boost_directories()

#set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

find_package(Qt4 COMPONENTS QtCore QtGui QtOpenGL REQUIRED)
include(${QT_USE_FILE})
add_definitions(-DQT_NO_KEYWORDS)

find_package(OpenGL REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIR} )

# make sure we set the gl and glu libraries only if they are avilable
set(OGRE_TOOLS_GL_LIBS "")
if(OPENGL_gl_LIBRARY)
  set(OGRE_TOOLS_GL_LIBS ${OGRE_TOOLS_GL_LIBS} ${OPENGL_gl_LIBRARY})
endif(OPENGL_gl_LIBRARY)
if(OPENGL_glu_LIBRARY)
  set(OGRE_TOOLS_GL_LIBS ${OGRE_TOOLS_GL_LIBS} ${OPENGL_glu_LIBRARY})
endif(OPENGL_glu_LIBRARY)

set(X11_LIBS "")
if (UNIX AND NOT APPLE)
  include(FindX11)
  if (NOT X11_FOUND)
    message(send_error "X11 not found")
  endif(NOT X11_FOUND)
  
  set(X11_LIBS ${X11_LIBRARIES})
endif(UNIX AND NOT APPLE)

find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_PATH})

# # Find the combined swig flags for this project
# rosbuild_invoke_rospack(${PROJECT_NAME} ${PROJECT_NAME} SWIG_FLAGS "export" "--lang=swig" "--attrib=flags")
# set(SWIG_FLAGS ${${PROJECT_NAME}_SWIG_FLAGS})
# 
# # Find the wxswig executable
# rosbuild_find_ros_package(wxswig)
# set(WXSWIG_EXECUTABLE ${wxswig_PACKAGE_PATH}/bin/swig)
# 
# # Add a custom command for generating the swig output files
# set(SWIG_INTERFACE_FILE ${PROJECT_SOURCE_DIR}/src/ogre_tools/ogre_tools.i)
# set(SWIG_OUTPUT_PYTHON_FILE ${PROJECT_SOURCE_DIR}/lib/ogre_tools.py)
# 
# # Deal with CMake 2.4 vs. 2.6
# if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" GREATER 2.4)   # CMake 2.6
#   set(SWIG_OUTPUT_CPP_FILE ${PROJECT_SOURCE_DIR}/src/ogre_tools/ogre_tools_swig_generated.cpp)
#   set(SWIG_COMMAND ${WXSWIG_EXECUTABLE} ${SWIG_FLAGS} -o ${SWIG_OUTPUT_CPP_FILE} -outdir ../lib -module ${PROJECT_NAME} ${SWIG_INTERFACE_FILE})
# else("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" GREATER 2.4)    # CMake 2.4
#   set(SWIG_OUTPUT_CPP_FILE src/ogre_tools/ogre_tools_swig_generated.cpp)
#   set(SWIG_COMMAND ${WXSWIG_EXECUTABLE} ${SWIG_FLAGS} -o ../${SWIG_OUTPUT_CPP_FILE} -outdir ../lib -module ${PROJECT_NAME} ${SWIG_INTERFACE_FILE})
# endif("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" GREATER 2.4)
# 
# set_source_files_properties(${SWIG_OUTPUT_CPP_FILE} PROPERTIES GENERATED true)
# add_custom_command(OUTPUT   ${SWIG_OUTPUT_CPP_FILE} 
#                             ${SWIG_OUTPUT_PYTHON_FILE}
#                    COMMAND  ${SWIG_COMMAND}
#                    DEPENDS  ${SWIG_INTERFACE_FILE}
#                    src/ogre_tools/initialization.i
#                    src/ogre_tools/initialization.h)

# Build the SVN version number or (if not in an SVN directory) ROS
# stack version number into the source so it can be printed at
# run-time.
set(VERSION_FILE ${PROJECT_SOURCE_DIR}/build/version.cpp)
execute_process(COMMAND          svnversion -n ${PROJECT_SOURCE_DIR}
                OUTPUT_VARIABLE  SVN_VERSION)
if(${SVN_VERSION} MATCHES "^exported$")
  execute_process(COMMAND          rosversion visualization_common
                  OUTPUT_VARIABLE  SVN_VERSION
                  OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
configure_file(src/ogre_tools/version.cpp.in ${VERSION_FILE} @ONLY)


# We add src/ogre_tools to the include list so that
# ogre_tools_swig_generated.cpp can find its headers.  This is part of a
# workaround for a deficiency in CMake 2.4's handling of generated sources.
include_directories(${PROJECT_SOURCE_DIR}/src/ogre_tools)

# We make one library with the basic C++ code...
rosbuild_add_library(${PROJECT_NAME}
  src/ogre_tools/initialization.cpp
  src/ogre_tools/render_system.cpp
  src/ogre_tools/render_widget.cpp
  src/ogre_tools/point_cloud.cpp
  src/ogre_tools/object.cpp
  src/ogre_tools/shape.cpp
  src/ogre_tools/grid.cpp
  src/ogre_tools/arrow.cpp
  src/ogre_tools/axes.cpp
  src/ogre_tools/billboard_line.cpp
  src/ogre_tools/camera_base.cpp
  src/ogre_tools/fps_camera.cpp
  src/ogre_tools/orbit_camera.cpp
  src/ogre_tools/ortho_camera.cpp
  src/ogre_tools/movable_text.cpp
  src/ogre_tools/qt_ogre_render_window.cpp
  src/ogre_tools/orthographic.cpp
  src/ogre_tools/stl_loader.cpp
  ${VERSION_FILE})

target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES} ${OGRE_TOOLS_GL_LIBS} ${OGRE_LIBRARIES} ${X11_LIBS})

# # ...and we make another library with just the SWIG wrappers, linked
# # against the C++ lib.
# rosbuild_add_library(python_${PROJECT_NAME} ${SWIG_OUTPUT_CPP_FILE})
# target_link_libraries(python_${PROJECT_NAME} ${PROJECT_NAME} ${PYTHON_LIBRARIES})
# 
# # swig python needs a shared library named _<modulename>.[so|dll|...]
# # this renames the output file to conform to that by prepending an underscore and removing the "lib" prefix
# set_target_properties(python_${PROJECT_NAME}
#                       PROPERTIES OUTPUT_NAME _${PROJECT_NAME}
#                       PREFIX "")

qt4_wrap_cpp(MOC_FILES
  src/test/grid_test.h
  )
rosbuild_add_executable(bin/grid_test EXCLUDE_FROM_ALL src/test/grid_test.cpp ${MOC_FILES})
target_link_libraries(bin/grid_test ${PROJECT_NAME} ${QT_LIBRARIES})
rosbuild_declare_test(bin/grid_test)

rosbuild_add_executable(bin/two_render_widgets EXCLUDE_FROM_ALL src/test/two_render_widgets.cpp)
target_link_libraries(bin/two_render_widgets ${PROJECT_NAME} ${QT_LIBRARIES})
rosbuild_declare_test(bin/two_render_widgets)

include_directories(src)
rosbuild_add_executable(bin/stl_to_mesh src/stl_to_mesh/stl_to_mesh.cpp)
target_link_libraries(bin/stl_to_mesh ${OGRE_LIBRARIES} ${PROJECT_NAME})
rosbuild_link_boost(bin/stl_to_mesh filesystem system)

#rosbuild_add_gtest_future(test/utest test/utest.cpp)
#target_link_libraries(test/utest ${PROJECT_NAME})
