#!/bin/sh
#
# This script reads the signal strength (RSSI) from  
# the MultiModem by viewing the webpage at 
# 192.168.2.1/modeminfor.html.
#
# (Note: The modem has to configured properly so
# that the AT+CSQ is executed)
#
# GetSS_MM2 {FILENAME}
#
# If no filename is given, the results are appended to
# the file CellModemTelnet.txt in the local directory.

FILENAME="$1"
if [ -z "$FILENAME" ]
then
	FILENAME=CellModemTelnet.txt
fi

echo -e "\n----------------\n" >> $FILENAME
date >> $FILENAME

(sudo ping -c1 -w1 192.168.2.1 > /dev/null) || exit 1 

echo "Reading Modem Info" >> $FILENAME

curl 192.168.2.1/modeminfor.html >> $FILENAME

# Find last AT+CSQ result
rssi=$(fgrep +CSQ: $FILENAME | tail -1 | cut -d \: -f 2 | cut -d \, -f 1 | sed -e 's/^[ ]*//')

if [ $rssi -ne 99 ]
then 
	echo -e "\n*************************" >> $FILENAME
	echo -n "Signal Strength: " >> $FILENAME 
	echo $rssi "bars" >> $FILENAME
	echo "*************************" >> $FILENAME
else
	echo -e "\nNot valid" >> $FILENAME
	exit 2
fi
