#!/bin/bash
# this file is generated by clearpath_bringup/upstart/install.bash

log_file=/tmp/clearpath-upstart.log
DATE=`date`

if [ -s /etc/ros/setup.bash ]; then
    echo "$DATE: clearpath-start using setup file /etc/ros/setup.bash" >> $log_file
    source /etc/ros/setup.bash
else
    echo "$DATE: clearpath-start using setup file /opt/ros/electric/setup.bash" >> $log_file
    source /opt/ros/electric/setup.bash
fi

interface=wlan0
echo "$DATE: clearpath-start on interface $interface" >> $log_file

export ROS_IP=`ifconfig $interface | grep -o 'inet addr:[^ ]*' | cut -d: -f2`

echo "$DATE: clearpath-start setting ROS_IP=$ROS_IP" >> $log_file

if [ "$ROS_IP" = "" ]; then
    echo "$DATE: clearpath-start can't run with empty ROS_IP." >> $log_file
    exit 1
fi

LAUNCH=/etc/ros/electric/clearpath.launch      

if [ -s $LAUNCH ]; then
    export HOME=/home/administrator
    setuidgid administrator roslaunch $LAUNCH
else
  echo "$DATE: Launch file does not exist: $LAUNCH" >> $log_file
fi
