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

CGI_CMD=$1

#for server in $(cat /etc/hosts | sed -n '/\# PLEASE/,/# DO/p' | egrep 'WZ.*1|WZ.*9' | grep -v -i 'OLD'); do
for server in $(cat /etc/hosts | sed -n '/\# PLEASE/,/# DO/p' | egrep 'WZ.*1' | 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} =~ ^WZ.*1$ ]] || [[ ${server} =~ ^WZ.*9$ ]]; then
                ZONE=$(echo ${server} | sed 's/WZ//;s/^/ZoneServer/')
                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
