#!/bin/sh

log() {
	logger -t "gcom-locked " "$@"
}

ROOTER_GCOM="/usr/lib/rooter/gcom/"

if [ -e /tmp/timecnt ]; then
	source /tmp/timecnt
else
	timecnt=0
fi

PORT=$1
GCOM=$2
CURRMODEM=$3
ATC=$4

LOCKDIR="/tmp/lockgcom$CURRMODEM"
PIDFILE="${LOCKDIR}/PID"

while [ 1 -lt 6 ]; do
	if mkdir "${LOCKDIR}" &>/dev/null; then
		echo "$$" > "${PIDFILE}"
		if [ ! -z "$ATC" ]; then
			export ATCMD="$ATC"
		fi
		OX=$(gcom -d $PORT -s $ROOTER_GCOM$GCOM 2>/dev/null)
		if [ ! -e /tmp/block ]; then
			/usr/lib/rooter/log/at-logger "$PORT $OX"
		fi
		tout=$(echo "$OX" | grep "TIMEOUT ERROR")
		if [ ! -z "$tout" ]; then
			let timecnt=$timecnt+1
			echo 'timecnt="'"$timecnt"'"' > /tmp/timecnt
			if [ "$timecnt" gt 4 ]; then
				action=$(uci -q get profile.simmiss.toaction)
				if [ "$action" = "1" ]; then
					sleep 10 && touch /etc/banner
					reboot -f
				else
					timecnt=0
					echo 'timecnt="'"$timecnt"'"' > /tmp/timecnt
				fi
			fi
		else
			timecnt=0
			echo 'timecnt="'"$timecnt"'"' > /tmp/timecnt
		fi
		break
	else
		OTHERPID="$(cat "${PIDFILE}" 2>/dev/null)"
		if [ $? = 0 ]; then
			if ! kill -0 $OTHERPID &>/dev/null; then
 				rm -rf "${LOCKDIR}"
			fi
		fi
		sleep 1
	fi
done

rm -rf "${LOCKDIR}"
echo "$OX"