# -*- mode: Shell-script; -*-

function rossetrobot() { # 自分のよく使うロボットのhostnameを入れる
    local hostname=${1-"pr1040"}
    local ros_port=${2-"11311"}
    export ROS_MASTER_URI=http://$hostname:$ros_port
    if [[ "${PS1}" =~ \[http://.*:.*\]\ (.*)$ ]] ; then
        export PS1="${BASH_REMATCH[1]}"
    fi
    export PS1="\[\033[00;31m\][$ROS_MASTER_URI]\[\033[00m\] ${PS1}"
    echo -e "\e[1;31mset ROS_MASTER_URI to $ROS_MASTER_URI\e[m"
}

function rossetlocal() {
    export ROS_MASTER_URI=http://localhost:11311
    echo -e "\e[1;31mset ROS_MASTER_URI to $ROS_MASTER_URI\e[m"
}

function rossetip_dev() {
  local device=${1-"(eth0|eth1|eth2|eth3|eth4|wlan0|wlan1|wlan2|wlan3|wlan4)"}
  export ROS_IP=`LANGUAGE=en LANG=C ifconfig | egrep -A1 "${device}"| grep inet\  | grep -v 127.0.0.1 | sed 's/.*inet addr:\([0-9\.]*\).*/\1/' | head -1`
  export ROS_HOSTNAME=$ROS_IP
}

function rossetip_addr() {
  local target_host=${1-"133.11.216.211"}
  ##target_hostip=$(host ${target_host} | sed -n -e 's/.*address \(.*\)/\1/gp')
  target_hostip=$(getent hosts ${target_host} | cut -f 1 -d ' ')
  if [ "$target_hostip" == "" ]; then target_hostip=$target_host; fi
  local mask_target_ip=$(echo ${target_hostip} | cut -d. -f1-3)
  export ROS_IP=$(LANGUAGE=en LANG=C ifconfig | grep inet\ | sed 's/.*inet addr:\([0-9\.]*\).*/\1/' | tr ' ' '\n' | grep $mask_target_ip)
  export ROS_HOSTNAME=$ROS_IP
}

function rossetip() {
  local device=${1-"(eth0|eth1|eth2|eth3|eth4|wlan0|wlan1|wlan2|wlan3|wlan4)"}
  export ROS_IP=""
  local master_host=$(echo $ROS_MASTER_URI | cut -d\/ -f3 | cut -d\: -f1);
  if [ "${master_host}" != "localhost" ]; then rossetip_addr ${master_host} ; fi
  if [ "${ROS_IP}" == "" ]; then rossetip_addr ${device}; fi
  if [ "${ROS_IP}" == "" ]; then rossetip_dev ${device}; fi
  export ROS_HOSTNAME=$ROS_IP
  if [ "${ROS_IP}" == "" ];
  then
      export -n ROS_IP
      export -n ROS_HOSTNAME
      echo -e "\e[1;31munable to set ROS_IP and ROS_HOSTNAME\e[m"
  else
      echo -e "\e[1;31mset ROS_IP and ROS_HOSTNAME to $ROS_IP\e[m";
  fi
}
