#!/bin/bash
#
# Usage: live_backup_log
#
source ~/.gamerc

remote_log_old_directory="$WORKING_DIRECTORY/servers*/Log/"
remote_log_cur_directory="$WORKING_DIRECTORY/servers*/"
local_log_php_directory="/mnt/disks/fn_live_logsearch/Log/Old"
local_log_old_directory="/mnt/disks/fn_live_backup/Log/Old"
local_log_cur_directory="/mnt/disks/fn_live_backup/Log/Cur"
local_log_cur_wz_directory="/mnt/disks/fn_live_backup/Log/Cur/WZ"
local_log_cur_ms_directory="/mnt/disks/fn_live_backup/Log/Cur/MS"
local_rsync_log_old="/tmp/rsync_live_old_log"
local_rsync_log_cur="/tmp/rsync_live_cur_log"
local_rsync_log_wz_old="/tmp/rsync_live_old_wz_log"
local_rsync_log_wz_cur="/tmp/rsync_live_cur_wz_log"
local_rsync_log_ms_old="/tmp/rsync_live_old_ms_log"
local_rsync_log_ms_cur="/tmp/rsync_live_cur_ms_log"

if [ ! -d "${local_log_old_directory}" ]; then
        mkdir -p ${local_log_old_directory}
        chmod 777 ${local_log_old_directory}
fi

if [ ! -d "${local_log_cur_wz_directory}" ]; then
        mkdir -p ${local_log_cur_wz_directory}
        chmod 777 ${local_log_cur_wz_directory}
fi

if [ ! -d "${local_log_cur_ms_directory}" ]; then
        mkdir -p ${local_log_cur_ms_directory}
        chmod 777 ${local_log_cur_ms_directory}
fi

if [ -f "${local_rsync_log_old}" ]; then
        rm -rf ${local_rsync_log_old}
fi
cd ${local_log_old_directory}
for machine in $(cat /etc/hosts | grep -i wz | awk -F" " '{print $2}')
do
        if [ -f "${local_rsync_log_wz_old}" ]; then
                rm -rf ${local_rsync_log_wz_old}
        fi
        echo "======"
        echo "RSYNC Machine for WZ Old Log from Live: ${machine}"
        echo ""

	set_id=$(echo ${machine}|sed 's/WS//')
	echo "rsync -m -avnz --ignore-existing --recursive --include=\"*/ZoneServer${set_id}*/Item${set_id}*.xz\" --include=\"*/ZoneServer${set_id}*/Message*.xz\" --exclude=\"*/**/\" -e ssh ${machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_wz_old}"
	rsync -m -avnz --ignore-existing --recursive --include="*/ZoneServer${set_id}*/Item${set_id}*.xz" --include="*/ZoneServer${set_id}*/Message*.xz" --exclude="*/*/*" -e ssh ${machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_wz_old}
        rsync -m -avz  --ignore-existing --recursive --include="*/ZoneServer${set_id}*/Item${set_id}*.xz" --include="*/ZoneServer${set_id}*/Message*.xz" --exclude="*/*/*" -e ssh ${machine}:${remote_log_old_directory}/ .

        # WZ Old: Include Message, Item
        for file in $(cat ${local_rsync_log_wz_old} | egrep "ZoneServer${set_id}0[1-8]/" | egrep 'Message|Item')
        do
        	wz_new_file=$(echo ${file} | sed 's/xz/output/')
        	if [ ! -f "${local_log_php_directory}/${wz_new_file}" ]; then
        	        dir_name=$(dirname ${local_log_php_directory}/${wz_new_file})
        	        if [ ! -d "${dir_name}" ]; then
        	                mkdir -p ${dir_name}
        	        fi
			echo "${local_log_php_directory}/${wz_new_file}"
        	        xcat ${file} > ${local_log_php_directory}/${wz_new_file}
        	fi
        done
        echo "======"
done

if [ -f "${local_rsync_log_old}" ]; then
        rm -rf ${local_rsync_log_old}
fi
cd ${local_log_old_directory}
for machine in $(cat /etc/hosts | grep -i ms | awk -F" " '{print $2}')
do
        if [ -f "${local_rsync_log_ms_old}" ]; then
                rm -rf ${local_rsync_log_ms_old}
        fi
        echo "======"
        echo "RSYNC Machine for MS Old Log from Live: ${machine}"
        echo ""

	set_id=$(echo ${machine}|sed 's/GAMEDB//;s/MS//')
	echo "rsync -m -avnz --ignore-existing --recursive --include=\"*/MissionServer${set_id}*/Mail*.xz\" --include=\"*/MissionServer${set_id}*/twolevel*.xz\" --exclude=\"*/*/*\" -e ssh ${machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_ms_old}"
	rsync -m -avnz --ignore-existing --recursive --include="*/MissionServer${set_id}*/Mail*.xz" --include="*/MissionServer${set_id}*/twolevel*.xz" --exclude="*/*/*" -e ssh ${machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_ms_old}
	rsync -m -avz --ignore-existing --recursive --include="*/MissionServer${set_id}*/Mail*.xz" --include="*/MissionServer${set_id}*/twolevel*.xz" --exclude="*/*/*" -e ssh ${machine}:${remote_log_old_directory}/ .

        # MS Old: Include twolevel, Mail
        for file in $(cat ${local_rsync_log_ms_old} | egrep "MissionServer${set_id}/" | egrep 'twolevel|Mail')
        do
                ms_new_file=$(echo ${file} | sed 's/xz/output/')
                if [[ ${file} == *"twolevel"* ]]; then
                        if [ ! -f "${local_log_php_directory}/${ms_new_file}" ]; then
                                dir_name=$(dirname ${local_log_php_directory}/${ms_new_file})
                                if [ ! -d "${dir_name}" ]; then
                                        mkdir -p ${dir_name}
                                fi
				echo "${local_log_php_directory}/${ms_new_file}"
                                xcat ${file} | sed 's/\x01/,/g' > ${local_log_php_directory}/${ms_new_file}
                        fi
                elif [[ ${file} == *"Mail"* ]]; then
                        if [ ! -f "${local_log_php_directory}/${ms_new_file}" ]; then
                                dir_name=$(dirname ${local_log_php_directory}/${ms_new_file})
                                if [ ! -d "${dir_name}" ]; then
                                        mkdir -p ${dir_name}
                                fi
				echo "${local_log_php_directory}/${ms_new_file}"
                                xcat ${file} > ${local_log_php_directory}/${ms_new_file}
                        fi
                fi
        done
        echo "======"
done

###############################################################################################
# WZ: Current Log from LIVE
if [ -f "${local_rsync_log_cur}" ]; then
        rm -rf ${local_rsync_log_cur}
fi
if [ -d "${local_log_cur_wz_directory}" ]; then
        rm -rf ${local_log_cur_wz_directory}
        mkdir -p ${local_log_cur_wz_directory}; chmod 777 ${local_log_cur_wz_directory}
fi
cd ${local_log_cur_wz_directory}
for machine in $(cat /etc/hosts | grep -i wz | awk -F" " '{print $2}')
do
	if [ -f "${local_rsync_log_wz_cur}" ]; then
		rm -rf ${local_rsync_log_wz_cur}
	fi
        echo "======"
        echo "RSYNC Machine for WZ Current Log from Live: ${machine}"
        echo ""

	set_id=$(echo ${machine}|sed 's/WS//')
        echo "rsync -m -avnz --include=\"/ZoneServer${set_id}*/Item${set_id}*log*\" --include=\"/ZoneServer${set_id}*/Message*log*\" --exclude=\"/*/*\" --exclude=\"/Data\" --exclude=\"/bin\" --exclude=\"/config.ini\" --exclude=\"/db\" --exclude=\"/setup.ini\" --ignore-existing --recursive -e ssh ${machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_wz_cur}"
        rsync -m -avnz --include="/ZoneServer${set_id}*/Item${set_id}*log*" --include="/ZoneServer${set_id}*/Message*log*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_wz_cur}
        rsync -m -avz --include="/ZoneServer${set_id}*/Item${set_id}*log*" --include="/ZoneServer${set_id}*/Message*log*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${machine}:${remote_log_cur_directory}/ .
        echo "======"
done

# MS: Current Log from LIVE
if [ -f "${local_rsync_log_cur}" ]; then
        rm -rf ${local_rsync_log_cur}
fi
if [ -d "${local_log_cur_ms_directory}" ]; then
        rm -rf ${local_log_cur_ms_directory}
        mkdir -p ${local_log_cur_ms_directory}; chmod 777 ${local_log_cur_ms_directory}
fi
cd ${local_log_cur_ms_directory}
for machine in $(cat /etc/hosts | grep -i ms | awk -F" " '{print $2}')
do
	if [ -f "${local_rsync_log_ms_cur}" ]; then
		rm -rf ${local_rsync_log_ms_cur}
	fi
        echo "======"
        echo "RSYNC Machine for MS Current Log from Live: ${machine}"
        echo ""

	set_id=$(echo ${machine}|sed 's/GAMEDB//;s/MS//')
        echo "rsync -m -avnz --include=\"/MissionServer${set_id}/Mail*\" --include=\"/MissionServer${set_id}/twolevel*\" --exclude=\"/*/*\" --exclude=\"/Data\" --exclude=\"/bin\" --exclude=\"/config.ini\" --exclude=\"/db\" --exclude=\"/setup.ini\" --ignore-existing --recursive -e ssh ${machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_ms_cur}"
        rsync -m -avnz --include="/MissionServer${set_id}/Mail*" --include="/MissionServer${set_id}/twolevel*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_ms_cur}
        rsync -m -avz --include="/MissionServer${set_id}/Mail*" --include="/MissionServer${set_id}/twolevel*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${machine}:${remote_log_cur_directory}/ .

        # MS Cur: Include twolevel, Mail
	for file in $(cat ${local_rsync_log_ms_cur} | egrep 'twolevel')
	do
		ms_new_file=$(echo ${file} | sed 's/$/\.output/')
		if [[ ${file} == *"twolevel"* ]]; then
			xcat ${file} | sed 's/\x01/,/g' > ${ms_new_file}
		fi
	done
        echo "======"
done

chmod -R 777 ${local_log_old_directory}
chmod -R 777 ${local_log_cur_directory}

echo ""
echo "RSYNC WZ/GAMEDB Log Done!" 
