#!/bin/bash
source ~/.gamerc

XGA_LOG_IP="104.199.226.218"

SCRIPT_NAME=$(basename "$0")
LOCK_FILE="/tmp/${SCRIPT_NAME}.lock"

exec 200>"${LOCK_FILE}"
flock -n 200 || {
    echo "${SCRIPT_NAME} is running. Exiting."
    exit 1
}

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

	# 同步各 Server 的 onelevel / twolevel 檔案到 XLOG
	rsync -avz ${FULL_PATH_DIRECTORY}/*level.log.* ${XGA_LOG_IP}::UNCLEAN/${GAME_TYPE}${COUNTRY_CODE}/${DIRECTORY}/
done
