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

CGI_CMD=$1

IP=$(cat /etc/hosts | grep ' DATA' | awk -F" " '{print $1}')
TOTAL_DATA_ZONE_INFO=$(ssh DATA "ls -hld ~/servers*/ZoneServer*" | awk -F' ' '{print $NF}')
#echo "TOTAL_DATA_ZONE_INFO: ${TOTAL_DATA_ZONE_INFO}"

for ZONE_INFO in ${TOTAL_DATA_ZONE_INFO}; do
        ZONE=$(basename $(echo ${ZONE_INFO} | awk -F' ' '{print $NF}'))
        PORT=$(ssh DATA "cat ~/servers*/${ZONE}/setup.ini | grep 'CGIPort=' | sed 's/CGIPort=//'")
        #echo "Machine: DATA => 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}
done
