#!/bin/bash
source ~/.gamerc

[ "$1" == "" ] && exec echo "Usage: $0 <asia/us/europe/all>"

AREA=$1

if [ "${AREA}" == "all" ]; then
	SEARCHSTR="^1"
elif [ "${AREA}" == "asia" ]; then
	SEARCHSTR="10.8.17." 
elif [ "${AREA}" == "us" ]; then
	SEARCHSTR="10.8.16." 
elif [ "${AREA}" == "europe" ]; then
	SEARCHSTR="10.8.18." 
else
	exec echo "Usage: $0 <asia/us/europe/all>"
fi

for IP in $(cat /etc/hosts | sed -n '/\# PLEASE/,/# DO/p' | egrep 'WZ' | egrep ${SEARCHSTR} | awk -F" " '{print $1}'); do
	echo ${IP} | colorize blue black
	print_servername ${IP} | colorize yellow black
	SET=$(print_servername ${IP} | awk -F"_" '{print $2}' | sed 's/WZ//;s/..$//')
	CGICMD="set_formula_log_level"

	if [ "${SET}" == "23" ]; then
		for ZONE in 1; do
			PORT=$(ssh ${IP} "cat ~/servers${SET}/ZoneServer${SET}0${ZONE}/setup.ini | grep CGIPort | sed 's/CGIPort=//'")
			echo "SET: ${SET}, Zone: ${ZONE}, Port: ${PORT}"
			ssh ${IP} "cd ~/servers${SET}/ZoneServer${SET}0${ZONE}/; cat GMCommand* | grep ${CGICMD}"
		done	
	else
		for ZONE in 1 2 3 6; do
			PORT=$(ssh ${IP} "cat ~/servers${SET}/ZoneServer${SET}0${ZONE}/setup.ini | grep CGIPort | sed 's/CGIPort=//'")
			echo "SET: ${SET}, Zone: ${ZONE}, Port: ${PORT}"
			ssh ${IP} "cd ~/servers${SET}/ZoneServer${SET}0${ZONE}/; cat GMCommand* | grep ${CGICMD}"
		done
	fi
done
