#!/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/livedb_backup/Log/Old"
local_log_old_directory="/mnt/disks/m7livebackup/Log/Old"
local_log_cur_directory="/mnt/disks/m7livebackup/Log/Cur"
local_log_cur_wz_directory="/mnt/disks/m7livebackup/Log/Cur/WZ"
local_log_cur_db_directory="/mnt/disks/m7livebackup/Log/Cur/DB"
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_db_old="/tmp/rsync_live_old_db_log"
local_rsync_log_db_cur="/tmp/rsync_live_cur_db_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_db_directory}" ]; then
        mkdir -p ${local_log_cur_db_directory}
	chmod 777 ${local_log_cur_db_directory}
fi

cd ${local_log_old_directory}
for wz_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: ${wz_machine}"
        echo ""
        #rsync -m -avnz --include="/*/*/Message*" --include="/*/*/Item1*" --include="/*/*/Debug*" --exclude="/*/*/*" --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_wz_old}
        #rsync -m -avz  --include="/*/*/Message*" --include="/*/*/Item1*" --include="/*/*/Debug*" --exclude="/*/*/*" --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_old_directory}/ .
        rsync -m -avnz --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_wz_old}
        rsync -m -avz  --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_old_directory}/ .

	# Include Message, Item, ItemMallEnchant, Debug, Stamina
	for file in $(cat ${local_rsync_log_wz_old} | egrep 'Message|Item|ItemMallEnchant|Debug|Stamina')
	do
		#wz_new_file=$(echo ${file} | sed 's/xz/output/')
		#xcat ${file} > ${local_log_php_directory}/${wz_new_file}

		if [[ ${file} == *"Debug"* ]]; then
			dbg_drop_only_file=$(echo ${file} | sed 's/\.xz/_item_drop\.output/')
			if [ ! -f "${local_log_php_directory}/${dbg_drop_only_file}" ]; then
				dir_name=$(dirname ${local_log_php_directory}/${dbg_drop_only_file})
				if [ ! -d "${dir_name}" ]; then
					mkdir -p ${dir_name}
				fi
				xcat ${file} | egrep 'drop' > ${local_log_php_directory}/${dbg_drop_only_file}
			fi
		else
			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
				xcat ${file} > ${local_log_php_directory}/${wz_new_file}
			fi
		fi
	done
done

if [ -f "${local_rsync_log_wz_cur}" ]; then
	rm -rf ${local_rsync_log_wz_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 wz_machine in $(cat /etc/hosts | grep -i wz | awk -F" " '{print $2}')
do
	echo "======"
        echo "RSYNC Machine for WZ Current Log from Live: ${wz_machine}"
        echo ""
	
        #rsync -m -avnz --include="/ZoneServer*/Message*" --include="/ZoneServer*/Item1*" --include="/ZoneServer*/Debug*"  --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_wz_cur}
        #rsync -m -avz --include="/ZoneServer*/Message*" --include="/ZoneServer*/Item1*" --include="/ZoneServer*/Debug*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_cur_directory}/ .
        rsync -m -avnz --include="/*Server*/*log*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_wz_cur}
        rsync -m -avz --include="/*Server*/*log*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${wz_machine}:${remote_log_cur_directory}/ .

	for file in $(cat ${local_rsync_log_wz_cur} | egrep 'Debug')
	do
		dbg_drop_only_file=$(echo ${file} | sed 's/$/\_item_drop.output/')
		xcat ${file} | egrep 'drop' > ${dbg_drop_only_file}
	done
	echo "======"
done

###############################################################################################

cd ${local_log_old_directory}
for db_machine in $(cat /etc/hosts | grep -i gamedb | awk -F" " '{print $2}')
do
	if [ -f "${local_rsync_log_db_old}" ]; then
		rm -rf ${local_rsync_log_db_old}
	fi
	echo "======"
	echo "RSYNC Machine for GAMEDB Old Log from Live: ${db_machine}"
	echo ""
        #rsync -m -avnz --include="/*/*/twolevel*" --include="/*/*/AccountMail*" --exclude="/*/*/*" --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_db_old}
        #rsync -m -avz  --include="/*/*/twolevel*" --include="/*/*/AccountMail*" --exclude="/*/*/*" --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_old_directory}/ .
        rsync -m -avnz --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_db_old}
        rsync -m -avz  --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_old_directory}/ .

        for file in $(cat ${local_rsync_log_db_old} | egrep 'twolevel|AccountMail')
        do
		db_new_file=$(echo ${file} | sed 's/xz/output/')
		if [[ ${file} == *"twolevel"* ]]; then
			if [ ! -f "${local_log_php_directory}/${db_new_file}" ]; then
				dir_name=$(dirname ${local_log_php_directory}/${db_new_file})
				if [ ! -d "${dir_name}" ]; then
					mkdir -p ${dir_name}
				fi
				xcat ${file} | sed 's/\x01/,/g' > ${local_log_php_directory}/${db_new_file}
			fi
		elif [[ ${file} == *"AccountMail"* ]]; then
			if [ ! -f "${local_log_php_directory}/${db_new_file}" ]; then
				dir_name=$(dirname ${local_log_php_directory}/${db_new_file})
				if [ ! -d "${dir_name}" ]; then
					mkdir -p ${dir_name}
				fi
				xcat ${file} > ${local_log_php_directory}/${db_new_file}
			fi
		fi
        done
	echo "======"
done

if [ -f "${local_rsync_log_db_cur}" ]; then
        rm -rf ${local_rsync_log_db_cur}
fi
if [ -d "${local_log_cur_db_directory}" ]; then
        rm -rf ${local_log_cur_db_directory}
        mkdir -p ${local_log_cur_db_directory}; chmod 777 ${local_log_cur_db_directory}
fi
cd ${local_log_cur_db_directory}
for db_machine in $(cat /etc/hosts | grep -i gamedb | awk -F" " '{print $2}')
do
        echo "======"
        echo "RSYNC Machine for GAMEDB Current Log from Live: ${db_machine}"
        echo ""

        #rsync -m -avnz --include="/MissionServer*/twolevel*" --include="/MissionServer*/AccountMail*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_db_cur}
        #rsync -m -avz --include="/MissionServer*/twolevel*" --include="/MissionServer*/AccountMail*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_cur_directory}/ .
        rsync -m -avnz --include="/*Server*/*log*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_cur_directory}/ . >> ${local_rsync_log_db_cur}
        rsync -m -avz --include="/*Server*/*log*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${db_machine}:${remote_log_cur_directory}/ .

        for file in $(cat ${local_rsync_log_db_cur} | egrep 'twolevel|AccountMail')
        do
		db_new_file=$(echo ${file} | sed 's/$/\.output/')
		if [[ ${file} == *"twolevel"* ]]; then
			xcat ${file} | sed 's/\x01/,/g' > ${db_new_file}
		fi
        done
        echo "======"
done 

chmod 777 ${local_log_old_directory}
chmod 777 ${local_log_cur_directory}

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