#!/bin/sh
#
# Continuosly run the UT-Robot client in an endless loop
# to overcome the fragility of the bluetooth GPS link and
# aborts from the GPSD library...
#

# Server IP address (or hostname) and UDP port number (1979 by default)
UT_SERVER=planetlab2.dcs.bbk.ac.uk:1979

# Network interface to get MAC address from (used as 'robot id' in hex)
NET_IF=wlan0
ROBOT_ID=`ifconfig $NET_IF | awk '/HWaddr/ {gsub(":", "", $5); print $5}'`

# Terminal settings must be previously set (baud rate, flow control, etc)
ADC_PORT=/dev/ttyS2

# Files to log status messages (stderr) and data output (stdout), respectively
LOG_STAT=/var/lib/utstat.log
LOG_DATA=/var/lib/utdata.log

echo "==================" >>$LOG_STAT
echo "STARTING 'utrobot'" >>$LOG_STAT
date >>/var/lib/utstat.log
echo "==================" >>$LOG_STAT

# Assumes that gpsd is already running on the localhost (port 2947)
utrobot -i $ROBOT_ID -u $UT_SERVER -a $ADC_PORT -vl >>$LOG_DATA 2>>$LOG_STAT

# React to exit code:
#   100 means that battery voltage dropped below acceptable level - shutdown
if [ $? -eq 100 ]; then
	init 0
fi

