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

CGI_CMD=$1

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

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