#!/bin/bash

# receive parameters:
# $1 (store|retrieve)
# $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 

service=auction
#LOG=/dev/stderr
LOG=/dev/null
#LOG=/tmp/$service.log

## use first if to prevent running from gameserver
#if [ $# -lt 5 ] ; then
if [ $# -lt 4 ] ; then
        echo "script called with f** bad parameters"
        exit 3
fi

echo $@ >>$LOG

##
## Essential setup
##
ROBOFOX=/home/gameserver/robofox/bin/robofox
ID2TEXT=/var/www/da.op3n/gameserver/id2text
WORKDIR=/var/www/da.op3n/gameserver/service-scripts/auction
export DISPLAY=:1.0
cd $WORKDIR

##
## Parameters
##
myaction=$1
myip=$2
myid=$3
myflag=$4

myurl="http://$myip/~$service/AuctionHouse/index.html"
myrealm="$service$myip"

##
## Generate user input
##
firstn=$( $ID2TEXT --name $myid )
lastn=$( $ID2TEXT --familyname $myid )
login=$( $ID2TEXT --login $myid )
passw=$( $ID2TEXT --passwd $myid )
email=$( $ID2TEXT --email $myid )
dummy=$( $ID2TEXT --text $myid )
dummy2=$( $ID2TEXT --text2 $myid  )  
flag="$dummy $myflag $dummy2"
 
##
## Decide which alternative way use for this
## flag
##
#myway=$(echo $myid % 2)
myway=0

##
## check if service is up
##
upcheck=$(mktemp)
rm -f $upcheck
wget -O /dev/null -t 1 $myurl -o $upcheck
echo $myip $myaction upcheck >>$LOG
#cat $upcheck >>$LOG
TEMP=$(grep saved $upcheck | wc -l)
if [ "$TEMP" == "0" ]; then
    # service unreachable
    echo $myip auction is down >>$LOG
    rm -f $upcheck
    exit 0;
else
    echo $myip auction is up >>$LOG
fi;
rm -f $upcheck

##exit 0 => service (partially down)
##exit 1 => service up
##exit 2 => + flag correct
##

### store flag 
echo $myip action is $myaction >>$LOG

if [ $myaction == "store" ] ; then

    echo $myip begin store >> $LOG
    
    $ROBOFOX $myrealm $service-$myaction-$myway.txt @@IP@@ $myip @@FLAG@@ "$flag" @@PASSW@@ "$passw" @@EMAIL@@ "$email" @@FIRSTN@@ "$firstn" @@LASTN@@ "$lastn" @@LOGIN@@ "$login"

    echo    $ROBOFOX $myrealm $service-$myaction-$myway.txt @@IP@@ $myip @@FLAG@@ "$flag" @@PASSW@@ "$passw" @@EMAIL@@ "$email" @@FIRSTN@@ "$firstn" @@LASTN@@ "$lastn" @@LOGIN@@ "$login" >> $LOG

    ## check log for HTTP errors
    exit 2

### retrieve flag
elif [ $myaction == "retrieve" ] ; then

    echo $myip begin retrieve >> $LOG
    mylog=$(mktemp /tmp/$service-dump.XXXXXX)
    rm $mylog
    echo $logging to $mylog >> $LOG
 
    $ROBOFOX $myrealm $service-$myaction-$myway.txt @@IP@@ $myip @@FLAG@@ "$myflag" @@PASSW@@ "$passw" @@EMAIL@@ "$email" @@FIRSTN@@ "$firstn" @@LASTN@@ "$lastn" @@LOGIN@@ "$login" > $mylog

    ## check log for flag
    status=3 ## assume corruption
   
    echo $myip FLAG: $myflag >>$LOG
    cat $mylog >>$LOG
    echo $myip GRAB >>$LOG
    grep $myflag $mylog >>$LOG

    TEMP=$(grep $myflag $mylog | wc -l)
    if [ "$TEMP" == "0" ]; then
	echo $myip FLAG FOUND >>$LOG
	status=2
    else
	echo $myip NO FLAG >>$LOG
	status=1
    fi;
    
    rm $mylog
    exit $status
    
### wrong command
else
        echo "$0 script called with unknown operation"
        exit 3
fi  

## should never be reached 
echo $0: FATAL 
exit 3
