#!/usr/bin/env bash

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  echo "ERROR: source this file instead of executing it" >&2
  exit 64
fi

_rdk_dds_profile="/home/sunrise/yiliao_ws/config/dds/fastdds_rdk_164.xml"

if ! ip -4 addr show dev wlx200db0c3fca5 2>/dev/null | grep -q 'inet 192\.168\.10\.164/'; then
  echo "ERROR: wlx200db0c3fca5 does not own 192.168.10.164" >&2
  unset _rdk_dds_profile
  return 1
fi

if [[ ! -r "${_rdk_dds_profile}" ]]; then
  echo "ERROR: unreadable Fast DDS profile: ${_rdk_dds_profile}" >&2
  unset _rdk_dds_profile
  return 1
fi

export ROS_DOMAIN_ID=22
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export ROS_LOCALHOST_ONLY=0
export FASTRTPS_DEFAULT_PROFILES_FILE="${_rdk_dds_profile}"
export FASTDDS_DEFAULT_PROFILES_FILE="${_rdk_dds_profile}"

printf 'DDS link enabled: domain=%s rmw=%s local=192.168.10.164 profile=%s\n' \
  "${ROS_DOMAIN_ID}" "${RMW_IMPLEMENTATION}" "${_rdk_dds_profile}"
unset _rdk_dds_profile
