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()

# 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)
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})


# 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})
