#!/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_isig=1" to turn on asynchrononus
# reporting of the BGAN signal strength and wait until the
# filter string is located in the response from the BGAN terminal.
#
# The format for this command is:
#    BGANGetSignalStrength 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 Signal Strength Script

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

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

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

# Turn on asynchronous reporting of the BGAN signal strength.
echo at_isig=1 | ncat -f _ISIG: -C $HOST $PORT > /tmp/ss_temp.txt 2>/dev/null

# Strip off everything except the signal strength value. 
echo -n "Signal Strength: " > $FILENAME
echo $(fgrep "_ISIG" /tmp/ss_temp.txt) | tail -1 | cut -d \: -f 2 | cut -d " " -f 2 >> $FILENAME
level=`cat $FILENAME`
#echo "BGAN Signal Level: "$level
echo "BGAN "$level

./BGANGetGPS $HOST $PORT

