#!/bin/bash
source $HOME/.gamerc
[ "$2" == "" ] && exec echo "Usage: $0 <FastCGI Command> <SetID>"

CGI_CMD=$1
SET_ID=$2

for server in $(cat /etc/hosts | sed -n '/\# PLEASE/,/# DO/p' | egrep "MS${SET_ID}" | 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})
        elif [[ ${server} =~ MS ]]; then
                ZONE=$(echo ${server} | sed 's/MS//;s/^/MissionServer/')
                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 http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGI_CMD}
        fi
done
