#!/bin/sh

PIDFILE="/var/run/tk103-query.pid"

{
	. /etc/opengts-tk103/query.conf
	while :; do
		echo "select simPhoneNumber,query from Device;" | mysql -u opengts -popengts -h $MYSQL_SERVER -s gts | while read GPS_TEXT
		do
			GPS_NUMBER=`echo $GPS_TEXT | cut --delimiter=" " -f1`
			GPS_QUERY=`echo $GPS_TEXT | cut --delimiter=" " -f2`
			if [ $GPS_NUMBER != "0" ] && [ $GPS_NUMBER != "1" ] && [ $GPS_QUERY = "1" ];
			then
				scmxx -d $MODEM_PORT --send --sms --direct --number="$GPS_NUMBER" --text="fix001s001n123456"
				echo "update Device set query=0 where simPhoneNumber='$GPS_NUMBER';" | mysql -u opengts -popengts -h $MYSQL_SERVER -s gts
			fi
		done
		sleep 15
	done
} &

echo -n $! > $PIDFILE

exit 0
