#!/bin/bash
source ~/.gamerc

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

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> <open/close>"
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"
	SWITCH=$2

	if [ "${SWITCH}" == "open" ]; then
		CGIPARAMETER="1"
	elif [ "${SWITCH}" == "close" ]; then
		CGIPARAMETER="0"
	fi

	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}"
			echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGICMD}+${CGIPARAMETER}"
			curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGICMD}+${CGIPARAMETER}
		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}"
			echo "curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGICMD}+${CGIPARAMETER}"
			curl http://127.0.0.1/cgi-bin/CGIAgent.fcgi?${IP},${PORT},0KjaM85BjfqjA,${CGICMD}+${CGIPARAMETER}
		done
	fi
done
