cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
include(FindPkgConfig)
set(ROS_BUILD_TYPE RelWithDeb)
rosbuild_init()

rosbuild_genmsg()
rosbuild_gensrv()

rosbuild_add_boost_directories()

# add dynamic reconfigure api for hokuyo
rosbuild_find_ros_package(dynamic_reconfigure)
include(${dynamic_reconfigure_PACKAGE_PATH}/cmake/cfgbuild.cmake)
gencfg()

# fixed joint reduction needs multiple visual/collision
# released diamondback branch 1.4.0 does not have multiple visual/collision
# only 1.5.0 and after
execute_process(COMMAND rosversion robot_model OUTPUT_VARIABLE robot_model_version)
if (${robot_model_version} VERSION_LESS "1.5.0")
  MESSAGE(STATUS "OUTPUT_VARIABLE: ${robot_model_version}Disabling fixed joint reduction")
else (${robot_model_version} VERSION_LESS "1.5.0")
  MESSAGE(STATUS "OUTPUT_VARIABLE: ${robot_model_version}Enabling fixed joint reduction")
  add_definitions(-DROBOT_MODEL_MULTI_VISUAL_COLLISION)
endif (${robot_model_version} VERSION_LESS "1.5.0")

# Build Gazebo
execute_process(COMMAND cmake -E chdir ${PROJECT_SOURCE_DIR} make -f Makefile.gazebo.tarball
                RESULT_VARIABLE _make_failed)
if(_make_failed)
  message(FATAL_ERROR "Build of Gazebo failed")
endif(_make_failed)

set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES build/gazebo)

# Find the build flags for Gazebo server
#set(ENV{PKG_CONFIG_PATH} "${PROJECT_SOURCE_DIR}/gazebo/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}")
set(ENV{PKG_CONFIG_PATH} "${PROJECT_SOURCE_DIR}/gazebo/lib/pkgconfig")
pkg_check_modules(GAZEBO REQUIRED gazeboserver)

include_directories(${GAZEBO_INCLUDE_DIRS})
link_directories(${GAZEBO_LIBRARY_DIRS})
#message(status "\n\n\n\n\n\nDEBUG:\n\n" $ENV{PKG_CONFIG_PATH} "\n\n\n\ninclude_dirs\n" ${GAZEBO_INCLUDE_DIRS} "\n\n\n\ncflags_others\n" ${GAZEBO_CFLAGS_OTHERS} "\n\n\n\ncflags (not consistent with gazeboserver.pc?)\n" ${GAZEBO_CFLAGS} "\n\n\n\nldflags_other\n" ${GAZEBO_LDFLAGS_OTHERS} "\n\n\n\nldflags (missing space?)\n" ${GAZEBO_LDFLAGS} "\n\n\n\n\n")

# add sources and link boost
rosbuild_add_library(urdf2gazebo src/urdf2gazebo.cpp)
rosbuild_add_executable(bin/gazebo src/gazeboros.cpp)
rosbuild_link_boost(bin/gazebo thread)
target_link_libraries(bin/gazebo tinyxml)
add_dependencies(bin/gazebo build_gazebo urdf2gazebo)
rosbuild_add_compile_flags(bin/gazebo ${GAZEBO_CFLAGS_OTHERS} ${GAZEBO_CFLAGS})
target_link_libraries(bin/gazebo ${GAZEBO_LIBRARIES} urdf2gazebo)
rosbuild_add_link_flags(bin/gazebo ${GAZEBO_LDFLAGS_OTHERS} ${GAZEBO_LDFLAGS})

rosbuild_add_executable(bin/urdf2model src/urdf2model.cpp)
target_link_libraries(bin/urdf2model urdf2gazebo)
rosbuild_link_boost(bin/urdf2model program_options)
target_link_libraries(bin/urdf2model tinyxml)

# extra utility for setting complex model pose
rosbuild_add_executable(bin/set_model_pose src/set_model_pose.cpp)
rosbuild_add_compile_flags(bin/set_model_pose ${GAZEBO_CFLAGS_OTHERS} ${GAZEBO_CFLAGS})
target_link_libraries(bin/set_model_pose ${GAZEBO_LIBRARIES})
rosbuild_add_link_flags(bin/set_model_pose ${GAZEBO_LDFLAGS_OTHERS} ${GAZEBO_LDFLAGS})
