#!/bin/bash
source ~/.gamerc

### 參數設定區 ###
[ "$3" == "" ] && exec echo "Usage: $0 <LOG_DATE [YYYYMMDD]/today> <GAMEDB/WZ> <LOG_NAME>"

SERVER_OLD_RESERVED_DAYS="14"
TODATE=$(date +"%Y%m%d")
YESTERDATE=$(date +%Y%m%d -d "-1 days")

[ "$1" == "today" ] && LOG_DATE=$(date +"%Y%m%d") || LOG_DATE=$1
[ "${LOG_DATE}" == "${TODATE}" -o "${LOG_DATE}" == "${YESTERDATE}" ] && CURRENT=1 || CURRENT=0
SERVER_NAME=$2
LOG_NAME=$3
RYESTERDATE=$(date -d "${LOG_DATE} -1day" +%Y%m%d)

RSYNC_LOG_PATH="$HOME/WebTool_Server_LOG"

### 刪除舊 LOG ###
ssh CTRL "[ ! -d ${RSYNC_LOG_PATH} ] && mkdir -p ${RSYNC_LOG_PATH}"
echo "ssh CTRL \"cd ${RSYNC_LOG_PATH}; find . -name \"*log*\" -type f -mtime +${SERVER_OLD_RESERVED_DAYS} -exec rm {} \;\""

### 同步新 LOG ###
for ip in $(cat /etc/hosts | sed -n '/\# PLEASE/,/# DO/p' | egrep -v "GAMEDB23" | egrep "${SERVER_NAME}" | awk -F" " '{print $1}'); do
	echo ${ip} | colorize blue black
	print_servername ${ip} | colorize yellow black
	if [ "${SERVER_NAME}" == "GAMEDB" ]; then
		set=$(print_servername ${ip} | awk -F"_" '{print $1}' | sed "s/${SERVER_NAME}//")
		num=$(print_servername ${ip} | awk -F"_" '{print $1}' | sed "s/${SERVER_NAME}//")
		keyword="MissionServer"
	elif [ "${SERVER_NAME}" == "WZ" ]; then
		set=$(print_servername ${ip} | awk -F"_" '{print $1}' | sed "s/${SERVER_NAME}//;s/..$//")
		num=$(print_servername ${ip} | awk -F"_" '{print $1}' | sed "s/${SERVER_NAME}//")
		keyword="WorldServer ZoneServer"
	fi
	echo ${set} | colorize red green
	echo ${num} | colorize orange green

	rsync /dev/null CTRL:$RSYNC_LOG_PATH/servers${set}/
	rsync /dev/null CTRL:$RSYNC_LOG_PATH/servers${set}/Log/
	rsync /dev/null CTRL:$RSYNC_LOG_PATH/servers${set}/Log/${LOG_DATE}/
	rsync /dev/null CTRL:$RSYNC_LOG_PATH/servers${set}/Log/${RYESTERDATE}/

	for server in ${keyword}; do
		if [ "${server}" == "MissionServer" ]; then
			if [ "${CURRENT}" == 1 ]; then
				echo "ssh CTRL \"cd $RSYNC_LOG_PATH/servers${set}/${server}${set}/; rm -rf *log*\""
				ssh CTRL "cd $RSYNC_LOG_PATH/servers${set}/${server}${set}/; rm -rf *log*"

				#echo "ssh ${SERVER_NAME}${num} \"cd $HOME/servers${set}/; rsync -avz --delete --progress ${server}${set}/${LOG_NAME}* CTRL:$RSYNC_LOG_PATH/servers${set}/${server}${set}/\""
				#ssh ${SERVER_NAME}${num} "cd $HOME/servers${set}/; rsync -avz --delete --progress ${server}${set}/${LOG_NAME}* CTRL:$RSYNC_LOG_PATH/servers${set}/${server}${set}/"
				echo "ssh ${SERVER_NAME}${num} \"cd $HOME/servers${set}/; rsync -avz --delete --progress ${server}${set}/{Exchange0,ExchangeBooking0,Node0}* CTRL:$RSYNC_LOG_PATH/servers${set}/${server}${set}/\""
				ssh ${SERVER_NAME}${num} "cd $HOME/servers${set}/; rsync -avz --delete --progress ${server}${set}/{Exchange0,ExchangeBooking0,Node0}* CTRL:$RSYNC_LOG_PATH/servers${set}/${server}${set}/"
			fi

			echo "ssh ${SERVER_NAME}${num} \"cd $HOME/servers${set}/Log/; rsync -avz ${LOG_DATE}/${server}${set}/${LOG_NAME}* CTRL:$RSYNC_LOG_PATH/servers${set}/Log/${LOG_DATE}/${server}${set}/\""
			ssh ${SERVER_NAME}${num} "cd $HOME/servers${set}/Log/; rsync -avz ${LOG_DATE}/${server}${set}/${LOG_NAME}* CTRL:$RSYNC_LOG_PATH/servers${set}/Log/${LOG_DATE}/${server}${set}/"

			echo "ssh ${SERVER_NAME}${num} \"cd $HOME/servers${set}/Log/; rsync -avz ${RYESTERDATE}/${server}${set}/${LOG_NAME}* CTRL:$RSYNC_LOG_PATH/servers${set}/Log/${RYESTERDATE}/${server}${set}/\""
			ssh ${SERVER_NAME}${num} "cd $HOME/servers${set}/Log/; rsync -avz ${RYESTERDATE}/${server}${set}/${LOG_NAME}* CTRL:$RSYNC_LOG_PATH/servers${set}/Log/${RYESTERDATE}/${server}${set}/"
		fi
	done
done
