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

CGI_CMD=$1
RET_KEYWORD=$2

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

for ZONE_INFO in ${TOTAL_TEST_ZONE_INFO}; do
    ZONE=$(basename $(echo ${ZONE_INFO} | awk -F' ' '{print $NF}'))
    PORT=$(ssh TEST "cat ~/servers*/${ZONE}/setup.ini | grep 'CGIPort=' | sed 's/CGIPort=//'")
    #echo "Machine: TEST => IP: ${IP}, Server: ${ZONE}, Port: ${PORT}"
    #echo "curl -s http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGI_CMD} | grep -q '${RET_KEYWORD}'"
    curl -s http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGI_CMD} | grep -q "${RET_KEYWORD}"
    echo $?
done
