#TAG = master
TAG = rc3
PKG_DIR = $(shell pwd)
SOURCE_DIR = $(PKG_DIR)/build/source
BUILD_DIR = $(PKG_DIR)/build/build
GIT_URL = git://github.com/wg-perception/recognition_kitchen.git
GIT_DIR = $(SOURCE_DIR)
GIT_REVISION = $(TAG)
ROS_PARALLEL_JOBS = -j8
CMAKE_FLAGS = \
	-DCMAKE_BUILD_TYPE=Release \
	-DG2O_BUILD_APPS=OFF \
	-DG2O_BUILD_EXAMPLES=OFF \
	-DG2O_USE_OPENGL=OFF \
	-DCMAKE_INSTALL_PREFIX=$(PKG_DIR) \
	-DCMAKE_PREFIX_PATH="/opt/ros/electric"

all: make_it
	touch $(PKG_DIR)/build/rospack_nosubdirs

update_to_trunk_of_trunk:
	cd $(SOURCE_DIR) && git submodule foreach git checkout master && git submodule foreach git pull

$(GIT_DIR):
	git clone https://github.com/wg-perception/recognition_kitchen $(SOURCE_DIR)
	cd $(SOURCE_DIR) && git clone https://github.com/ros/catkin && cd catkin && git checkout 0.3.29 ./
	cd $(SOURCE_DIR) && git clone https://github.com/ros/genmsg && cd genmsg && git checkout 0.3.5 ./ && patch -p1 < ../../../genmsg.patch
	cd $(SOURCE_DIR) && git clone https://github.com/ros/gencpp && cd gencpp && git checkout 0.2.3 ./
	cd $(SOURCE_DIR) && git clone https://github.com/plasmodic/ecto.git && cd ecto && git checkout 0.3.8 ./
	cd $(SOURCE_DIR) && git clone https://github.com/plasmodic/ecto_image_pipeline.git && cd ecto_image_pipeline && git checkout 0.3.4 ./
	cd $(SOURCE_DIR) && git clone https://github.com/plasmodic/ecto_opencv.git && cd ecto_opencv && git checkout 0.3.6 ./
	cd $(SOURCE_DIR) && git clone https://github.com/plasmodic/ecto_openni.git && cd ecto_openni && git checkout 0.3.2 ./
	cd $(SOURCE_DIR) && git clone https://github.com/plasmodic/ecto_pcl.git && cd ecto_pcl && git checkout 0.3.2 ./
	cd $(SOURCE_DIR) && git clone https://github.com/plasmodic/ecto_ros.git && cd ecto_ros && git checkout 0.3.7 ./
	cd $(SOURCE_DIR) && git clone https://github.com/wg-debs/libg2o-release.git
	cd $(SOURCE_DIR) && git clone https://github.com/wg-perception/capture.git && cd capture && git checkout 0.2.8 ./
	cd $(SOURCE_DIR) && git clone https://github.com/wg-perception/object_recognition_core.git && cd object_recognition_core && git checkout 0.3.4 ./
	cd $(SOURCE_DIR) && git clone https://github.com/wg-perception/object_recognition_msgs.git && cd object_recognition_msgs && git checkout 0.3.0 ./
	cd $(SOURCE_DIR) && git clone https://github.com/wg-perception/tod.git && cd tod && git checkout 0.2.7 ./
	cd $(SOURCE_DIR) && git clone https://github.com/wg-perception/reconstruction.git && cd reconstruction && git checkout 0.2.6 ./
	cd $(SOURCE_DIR) && rm CMakeLists.txt
	ln -s $(SOURCE_DIR)/catkin/toplevel.cmake $(SOURCE_DIR)/CMakeLists.txt

make_it: $(BUILD_DIR)/cmaked
	cd $(BUILD_DIR) && eval `setup.sh` && $(MAKE) $(ROS_PARALLEL_JOBS) && ctest -R object_recognition -E db && make install
	cd $(PKG_DIR) && ./pypath.sh #create a soft link to our python path
	cd $(PKG_DIR) && cp -fr ./pyshared/object_recognition_msgs/* ../object_recognition_msgs/src/object_recognition_msgs
	find . -name "*.cmake" -print | xargs sed -i 's/\/tmp\/buildd\/ros-electric-object-recognition-[0-9\.]*\/debian\/ros-electric-object-recognition//g'

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

cmake:
	cd $(BUILD_DIR) && cmake $(CMAKE_FLAGS) $(SOURCE_DIR)

$(BUILD_DIR)/cmaked: $(SOURCE_DIR) $(BUILD_DIR)
	cd $(BUILD_DIR) && cmake $(CMAKE_FLAGS) $(SOURCE_DIR)
	touch $(BUILD_DIR)/cmaked

clean:
	cd $(BUILD_DIR) && $(MAKE) clean

wipe:
	rm -rf $(BUILD_DIR)

