#!/bin/bash
source $HOME/.gamerc
[ "$1" == "" ] && exec echo "Usage: $0 <WorldID>"

WORLD_ID=$1
SET_ID=$(( ${WORLD_ID} / 100 ))
CGI_CMD="reset_server+${WORLD_ID}"

for server in $(cat /etc/hosts | egrep "SUBMIT" | grep -v -i 'OLD'); do
        if [[ ${server} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
                IP=$(echo ${server})
                ZONE="MissionServer${SET_ID}"
                PORT=$(ssh ${server} "cat ~/servers*/${ZONE}/setup.ini | grep 'CGIPort=' | sed 's/CGIPort=//'")
                #echo "Machine: ${server} => IP: ${IP}, Server: ${ZONE}, Port: ${PORT}"
                #echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGI_CMD}"
                curl -s http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGI_CMD} | grep -q "reset_server send to worldserver \[${WORLD_ID}\]!"
		echo $?
        fi
done
