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

CGI_CMD=$1

#if [ ! -z "${SET_ID_PUBLIC}" ]; then
#	SET_ID=$(echo ${SET_ID_PUBLIC} | sed 's/|//g')
#else
#	SET_ID=$(ssh CTRL "psql -U postgres WebTool -c \"COPY (SELECT set_id FROM db_setup WHERE is_commercial = 1 ORDER BY set_id) TO STDOUT CSV;\"" | tr '\n' '|' | sed 's/|//g')
#fi

for server in $(cat /etc/hosts | sed -n '/\# PLEASE/,/# DO/p' | egrep 'MS*' | 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
