# receive parameters:
# $1 (store|receive)
# $2 IP
# $3 ID
# $4 Flag
#
# Returns:
# 0: remote malfunction, service not up, answer garbled, other remote failures
# 1: service up, no flag  (not used for 'store')
# 2: success
# 3: internal error/local failure 


export DISPLAY=:1.0

if [ $# -lt 4 ] ; then
        echo "script called with f** bad parameters"
        exit 3
fi

test -d /tmp/hovtest$2
if [ $? == "1" ] ; then
	mkdir /tmp/hovtest$2
fi

wget -O /dev/null -t 1 $2:8180/Hoverboard -o /tmp/hovtest$2/status
TEMP=`grep saved /tmp/hovtest$2/status | wc -l`
rm /tmp/hovtest$2/status
if [ "$TEMP" == "0" ] ; then
	exit 0
fi

WorkDir=$(pwd)/Checkers/hoverboard

NS=`date +%N`
TEMP=`echo "$NS % 3" | bc`
if [ "$TEMP" == "0" ] ; then
	DOM="@fbiclub.xxx"
elif [ "$TEMP" == "1" ] ; then
	DOM="@dukat.eu"
else
	DOM="@ally.co"
fi

FN=`/var/www/da.op3n/gameserver/id2text --name $3`
LN=`/var/www/da.op3n/gameserver/id2text --familyname $3`
UN=`/var/www/da.op3n/gameserver/id2text --login $3`
PW=`echo $4 | cut -b1-9`
EM=`echo $4$DOM`
IP=$2

if [ "$1" == "store" ] ; then
	TEMP=`echo "$3 % 2" | bc`

	if [ "$TEMP" == "0" ] ; then

echo "DEBUG: AccS / START / $(date), $2, $1, $3" >> HOVTESTDEBUG

		/home/gameserver/robofox/bin/robofox hovtest$2 $WorkDir/hovcheck-AccS.txt @@IP@@ $IP @@UN@@ $UN @@EM@@ $EM @@FN@@ $FN @@LN@@ $LN @@PW@@ $PW

echo "DEBUG: AccS / ENDEE / $(date), $2, $1, $3" >> HOVTESTDEBUG

	else

		EM=`echo $4 | cut -b2-6`
		EM=`echo $EM$DOM`
		CT=$4

echo "DEBUG: BloS / START / $(date), $2, $1, $3" >> HOVTESTDEBUG

		/home/gameserver/robofox/bin/robofox hovtest$2 $WorkDir/hovcheck-BloS.txt @@IP@@ $IP @@UN@@ $UN @@EM@@ $EM @@FN@@ $FN @@LN@@ $LN @@PW@@ $PW @@CT@@ $CT

echo "DEBUG: BloS / ENDEE / $(date), $2, $1, $3" >> HOVTESTDEBUG

	fi

        exit 2

elif [ "$1" == "retrieve" ] ; then
        TEMP=`echo "$3 % 2" | bc`
        if [ "$TEMP" == "0" ] ; then

echo "***DEBUG: AccR / START / $(date), $2, $1, $3" >> HOVTESTDEBUG
	        /home/gameserver/robofox/bin/robofox hovtest$2 $WorkDir/hovcheck-AccR.txt @@IP@@ $IP @@UN@@ $UN @@PW@@ $PW > /home/gameserver/robofox/realms/hovtest$2/HVDUMP

		TEMP=`grep "<td>$4" /home/gameserver/robofox/realms/hovtest$2/HVDUMP | wc -l`
		TEMP2=`grep "<td> $4" /home/gameserver/robofox/realms/hovtest$2/HVDUMP | wc -l`
		rm /home/gameserver/robofox/realms/hovtest$2/HVDUMP
	        if [ "$TEMP" == "0" -a "$TEMP2" == "0" ] ; then
echo "DEBUG: AccR / ENDEE / $(date), $2, $1, $3, BAD" >> HOVTESTDEBUG
        		exit 1
	        else
echo "DEBUG: AccR / ENDEE / $(date), $2, $1, $3, SUC" >> HOVTESTDEBUG
			exit 2
		fi

	else

echo "***DEBUG: BloR / START / $(date), $2, $1, $3" >> HOVTESTDEBUG
		/home/gameserver/robofox/bin/robofox hovtest$2 $WorkDir/hovcheck-BloR.txt @@IP@@ $IP @@UN@@ $UN @@PW@@ $PW > /home/gameserver/robofox/realms/hovtest$2/HVDUMP

                TEMP=`grep "<p>$4" /home/gameserver/robofox/realms/hovtest$2/HVDUMP | wc -l`
                TEMP2=`grep "<p> $4" /home/gameserver/robofox/realms/hovtest$2/HVDUMP | wc -l`
		rm /home/gameserver/robofox/realms/hovtest$2/HVDUMP
                if [ "$TEMP" == "0" -a "$TEMP2" == "0" ] ; then
echo "DEBUG: BloR / ENDEE / $(date), $2, $1, $3, BAD" >> HOVTESTDEBUG
                        exit 1
                else
echo "DEBUG: BloR / ENDEE / $(date), $2, $1, $3, SUC" >> HOVTESTDEBUG
                        exit 2
                fi

 	fi

else
        echo "script called with unknown operation"
        exit 3
fi  
