#!/bin/bash


#-# install SDK

#-# path used to install the SDK:
export ROS2_SDK_PATH="/opt/poky/3.1.26_RZV2H_ROS2"

#-# yocto build directory path (the SDK ROS2 libraries refer to that path)
export ROS2_BSP_YOCTO_BUILD_PATH="/home/stefan/rzg_ubuntu-20.04/rzv2h_EVK_v3.0.0_ROS2/build/tmp"

export ROS2_SDK_PATH2=${ROS2_SDK_PATH//\//\\/}
export ROS2_BSP_YOCTO_BUILD_PATH2=${ROS2_BSP_YOCTO_BUILD_PATH//\//\\/}

# 

sudo rm -rf ${ROS2_SDK_PATH}

./poky-glibc-x86_64-core-image-weston-aarch64-rzv2h-evk-ver1-toolchain-3.1.26.sh
#-# choose directory: ${ROS2_SDK_PATH}


#-# patch SDK for ROS2
#-# Note: Modify the "SDK build path" used for "INTERFACE_LINK_LIBRARIES" to the new path of poky's sysroots.

cp  ros2_ws/patch_sdk.py .

sed -i "s/\/opt\/poky\/3.1.31/$ROS2_SDK_PATH2/g"                     patch_sdk.py
sed -i "s/~\/ws\/ros2-install\/build\/tmp/$ROS2_BSP_YOCTO_BUILD_PATH2/g" patch_sdk.py

sudo python3 patch_sdk.py

#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/share/fastrtps/cmake/fastrtps-dynamic-targets.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/share/cartographer/cmake/CartographerTargets.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/share/geometric_shapes/cmake/export_geometric_shapesExport.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/share/rcl_yaml_param_parser/cmake/rcl_yaml_param_parserExport.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/share/v4l2_camera/cmake/export_v4l2_cameraExport.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/share/rcl_logging_log4cxx/cmake/rcl_logging_log4cxxExport.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/lib/BehaviorTreeV3/cmake/BehaviorTreeV3Config.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/lib64/cmake/benchmark/benchmarkTargets.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/lib64/cmake/opencv4/OpenCVModules.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/lib64/cmake/realsense2/realsense2Targets.cmake]
#-# target[/opt/poky/3.1.26_RZV2H_ROS2/sysroots/aarch64-poky-linux/usr/lib64/urdfdom/cmake/urdfdomExport.cmake]



### It may be that the patch script does not modify all files required.
### If you execute the following command and find some files, please correct the INTERFACE_LINK_LIBRARIES paths of all files manually.
### sudo find ${ROS2_SDK_PATH}/ -name "*Export.cmake" | xargs grep -l "build/tmp" | grep INTERFACE_LINK_LIBRARIES
#-# => no result
#-#   => All files of the current SDK have been patched with the patch_sdk.py script.


