#!/bin/sh
#
# This script connects to the BGAN terminal on port 9998
# using "ncat" (modified for LRI to accept a filter string)
# "ncat" sends the string "at_igps?" to get the current BGAN
# GPS position. 
#
# The format for this command is:
#    BGANGetGPS HOST PORT {FILENAME}
#
# If no filename is given, the results are written to
# the file BGANSignalStrength.txt in the local directory.

#echo Running BGAN Get GPS Script

HOST="$1"
PORT="$2"

FILENAME="$3"
if [ -z "$FILENAME" ]
then
	FILENAME=BGANGps.txt
fi

# Make sure that the BGAN terminal is alive.
(sudo ping -c1 -w1 $HOST > /dev/null) || exit 1 

# Get the BGAN GPS position.
echo at_igps? | ncat -f _IGPS: -C $HOST $PORT > /tmp/gps_temp.txt 2>/dev/null
echo `fgrep "_IGPS" /tmp/gps_temp.txt`| tail -1 | cut -d \: -f 2 | cut -d " " -f 2 > /tmp/$FILENAME
gps_position=`cat /tmp/$FILENAME`
echo "BGAN GPS Position: "$gps_position

