#!/bin/bash
#===============================================================================
#
#          FILE: gmtool_config_gen_test
#
#         USAGE: gmtool_config_gen_test
#
#   DESCRIPTION: Automatically generate setup.xml for GMTool
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Sun Jun 19 23:14:44 EDT 2011
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

source ~/.gamerc

MAX_WORLDS="$(wc -l < ~/bin/worlds.test)"

cat << __EOF__
<?xml version="1.0" ?>
<ChannelSetting LogDataLimit="$GM_TOOL_LOG_DATA_LIMIT" />
<Locate country="$COUNTRY_CODE" Path="$GAME_CLIENT_DEFAULT_INSTALL_PATH"/>
<ComboNameSetting ComboFirst="1" />
<ServerInfo>
__EOF__

for((SET=1;SET<=$MAX_WORLDS;SET++));do
   MAX_CHANNELS="$(ls ~/servers$SET/ 2>&1|grep "^ZoneServer[0-9]\+$"|wc -l)"
   [ "$MAX_CHANNELS" == "0" ] || echo "   <Set SetID=\"$SET\" SetName=\"$(get_world_name $SET)\">"
        for((CH=$CHANNEL_NUMBER_SEQUENCE_START_WITH;CH<=$(($MAX_CHANNELS-1));CH++));do
                echo -n "      <Server ServerID=\"$(get_server_id $SET $CH z)\" ServerName=\"$(get_world_name $SET)-$CHANNEL_PREFIX$CH$CHANNEL_SUFFIX\" "
                echo "IP=\"$(another_ip $(print_serverip TEST))\" Port=\"$(grep GM $WORKING_DIRECTORY/servers*/ZoneServer$(get_wz_num $SET $CH)/setup.ini|sed 's/GMToolPort=//g')\" />"
        done
   [ "$MAX_CHANNELS" == "0" ] || echo "   </Set>"
done
echo "</ServerInfo>"
