#!/bin/bash

GPS_CHECK_LOG=/var/log/checkgps.log

DEV_MODEM=/dev/ttyUSB3
DEV_GPS=/dev/ttyUSB1
GPS_ON=/tmp/gpson

TIMESTAMP=$(date "+%Y-%m-%d %T")

GNXIO=/usr/bin/gnxio

echo "Check GPS: $TIMESTAMP" >> "${GPS_CHECK_LOG}"
if [ -f $GPS_ON ] ; then
    #logger "GPS fine"
    echo "GPS already checked." >> "${GPS_CHECK_LOG}"
    exit 0
fi

function get_response
{
        local ECHO
        # cat will read the response, then die on timeout
	IFS=$'\r\n'
        cat <&5 >$TMP &
        echo "$1" >&5
        # wait for cat to die
        wait $!
        # Check if the modem echoed our command
        read ECHO <$TMP
        [ "$ECHO" != "$1" ] && return 1
        return 0
}

function restart_modem
{
	# Power down modem
	PWR=`${GNXIO} get GSM_OFF`
	if [[ "${PWR}" == "low" ]] ; then
		echo "Modem is ON, power down." >> "${GPS_CHECK_LOG}"
		# Modem is ON restart it
 		echo "Ignition start" >> "${GPS_CHECK_LOG}"
                ${GNXIO} set GSM_IGT high
                sleep 1
                echo "Ignition release" >> "${GPS_CHECK_LOG}"
                ${GNXIO} set GSM_IGT low
		echo "Modem power OFF" >> "${GPS_CHECK_LOG}"
        	${GNXIO} set GSM_OFF high
        	sleep 1
	fi
 	echo "Modem is OFF, power up." >> "${GPS_CHECK_LOG}"
	# Modem is off switch it on
 	echo "Modem power ON"  >> "${GPS_CHECK_LOG}"
   	${GNXIO} set GSM_OFF low
	sleep 1		
	echo "Ignition start" >> "${GPS_CHECK_LOG}"
    	${GNXIO} set GSM_IGT high
    	sleep 1
    	echo "Ignition release" >> "${GPS_CHECK_LOG}"
    	${GNXIO} set GSM_IGT low
	
	MAX_WAIT=10
	WAIT_COUNTER=0
 	while [[ ! -c "${DEV_GPS}" ]] && [[ $WAIT_COUNTER -lt $MAX_WAIT ]]; do
		let WAIT_COUNTER=WAIT_COUNTER+1
        	sleep 1
        	#echo -n "."
		echo "Wait for modem $WAIT_COUNTER/$MAX_WAIT" >> "${GPS_CHECK_LOG}"
	done
	echo "Restarting services." >> "${GPS_CHECK_LOG}"
	# Restart bfglink
	sv restart bfglink
	sv restart pppd
	sv restart openvpn
}

TMP="/tmp/response"

# Clear out old response
: > $TMP

# Set modem with timeout of 5/10 a second.  Things reading will read until
# data stops, wait 1/2 second, then quit.
stty -F $DEV_MODEM 9600 -echo igncr -icanon onlcr ixoff min 0 time 5

# Open modem on FD 5
exec 5<>$DEV_MODEM

MAX_TRIES=5
COUNTER=0

while [ $COUNTER -lt $MAX_TRIES ]; do
    get_response "AT+CGPS?" || echo "Bad response AT+CGPS?" >> "${GPS_CHECK_LOG}"
    RESPONSE=$(cat $TMP)
    REGEX="*\+CGPS: 1,1*"
    if [[ $RESPONSE == *"+CGPS: 1,1"* ]] ; then
	#logger "GPS is enabled"
	echo "GPS is enabled." >> "${GPS_CHECK_LOG}"
	touch $GPS_ON
	break;
    else
 	#logger "Try to enable GPS"
	echo "Try to enable GPS." >> "${GPS_CHECK_LOG}"
	get_response "AT+CGPS=1"
	RESPONSE=$(cat $TMP)
 	if [[ $RESPONSE == *"OK"* ]] ; then
		#logger "GPS has been enabled"
 		echo "GPS has been enabled." >> "${GPS_CHECK_LOG}"
	fi
    	let COUNTER=COUNTER+1
	sleep 1
    fi
done

# Close connection to modem
exec 5<&-

# Uncomment to debug modem power cycle
# let COUNTER=$MAX_TRIES

# If we failed to get the status of the GPS for more 
# than 10 retries, toggle the modem power
if [ $COUNTER -ge $MAX_TRIES ]; then
	echo "Cannot determine if modem GPS is enabled...restarting modem!" >> "${GPS_CHECK_LOG}"
	restart_modem
fi
