#!/bin/bash

if [[ -z "${ROS_PACKAGE_PATH}" ]]; then
   . $(find /opt/ros/ -name "setup.bash")
fi

if [[ -z "${ROS_DISTRO}" ]]; then
   export ROS_DISTRO=$(basename $(find /opt/ros/* -maxdepth 0 -type d | head -1))
fi

pkg_name=$(basename $(find /workspaces/ -maxdepth 1 -mindepth 1 -type d))
mkdir -p ~/robot_ws/src
cd ~/robot_ws/src
ln -s /workspaces/${pkg_name}
cd ..
wstool init src
if [ -f "src/${pkg_name}/.rosinstall" ]; then
    wstool merge "src/${pkg_name}/.rosinstall" -t src
fi

mkdir -p ~/.ssh/
cat << EOF >> ~/.ssh/config
Host *
     StrictHostKeyChecking no
EOF
wstool update -t src
find -L src -name ci_entry_script.bash -exec bash -c 'echo Running entry script from $(basename $(dirname $1)) ; $1 ' _ {} \;
rosdep update
rosdep install --from-paths src --ignore-src --rosdistro $ROS_DISTRO -r -y
catkin init
catkin config --cmake-args -DCMAKE_CXX_FLAGS=-w --

echo "source $(find /opt/ros/ -name 'setup.bash')"  >>  ~/.bashrc
