#!/bin/bash
source ~/.gamerc

XGA_LOG_IP="104.199.226.218"

# 遠端 XLOG 遊戲名稱接收目錄不存在, 協助新增目錄.
rsync /dev/null ${XGA_LOG_IP}::UNCLEAN/${GAME_TYPE}${COUNTRY_CODE}/

for FULL_PATH_DIRECTORY in $(ls -hld ~/servers*/*Server* | egrep 'Mission|World|Zone|Society' | grep -v core | awk -F" " '{print $NF}'); do
	#SET_NUMBER=$(basename ${FULL_PATH_DIRECTORY} | sed 's/.*Server//' | cut -c 1);
	#echo "${FULL_PATH_DIRECTORY} => ${SET_NUMBER}";

	DIRECTORY=$(basename ${FULL_PATH_DIRECTORY})
	# 遠端 XLOG 遊戲 Server 接收目錄不存在, 協助新增目錄.
	rsync /dev/null ${XGA_LOG_IP}::UNCLEAN/${GAME_TYPE}${COUNTRY_CODE}/${DIRECTORY}/

	# 清除卡住程序, 重新同步 ( unlock )
	killall /bin/flock
	[ -e /tmp/sync_level.lock ] && rm -rf /tmp/sync_level.lock

	# 同步各 Server 的 onelevel / twolevel 檔案到 XLOG
	#date
	#echo ${DIRECTORY}
	/bin/flock -xn /tmp/sync_level.lock rsync -avz ${FULL_PATH_DIRECTORY}/*level.log.* ${XGA_LOG_IP}::UNCLEAN/${GAME_TYPE}${COUNTRY_CODE}/${DIRECTORY}/ && rm -rf /tmp/sync_level.lock
done
