#!/bin/bash
# this file is generated by robot_bringup/upstart/install.bash
# substitutions: interface0, portnum, robot, job, release, user

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

echo "$DATE: robot job using setup file /etc/ros/setup.bash" >> $log_file
source /etc/ros/setup.bash

echo "$DATE: robot job on interface interface0" >> $log_file

export ROS_IP=`ifconfig interface0 | grep -o 'inet addr:[^ ]*' | cut -d: -f2`
export ROS_MASTER_URI=http://$ROS_IP:portnum
export ROS_LOG_DIR=/tmp

echo "$DATE: robot job launching with ROS_MASTER_URI=$ROS_MASTER_URI" >> $log_file

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

LAUNCH_FILENAME=/tmp/robot-job.launch
/usr/sbin/mklaunch /etc/ros/release/robot/job.d > $LAUNCH_FILENAME

setuidgid user roslaunch $LAUNCH_FILENAME
