#!/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

cd ${local_log_old_directory}
for machine in $(cat /etc/hosts | grep -i wz | awk -F" " '{print $NF}')
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 ""
        rsync -m -avnz --ignore-existing --recursive -e ssh ${machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_wz_old}
        rsync -m -avz  --ignore-existing --recursive -e ssh ${machine}:${remote_log_old_directory}/ .

        # WZ Old: Include Message, Item, ItemMallEnchant, Debug, Stamina
        for file in $(cat ${local_rsync_log_wz_old} | egrep 'WorldServer|ZoneServer' | 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
        echo "======"
done

cd ${local_log_old_directory}
for machine in $(cat /etc/hosts | grep -i ms | awk -F" " '{print $NF}')
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 ""
        rsync -m -avnz --ignore-existing --recursive -e ssh ${machine}:${remote_log_old_directory}/ . >> ${local_rsync_log_ms_old}
        # MS Old: Include twolevel, AccountMail
        for file in $(cat ${local_rsync_log_ms_old} | egrep 'MissionServer' | egrep 'twolevel|AccountMail')
        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
                                xcat ${file} | sed 's/\x01/,/g' > ${local_log_php_directory}/${ms_new_file}
                        fi
                elif [[ ${file} == *"AccountMail"* ]]; 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
                                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 $NF}')
do
        echo "======"
        echo "RSYNC Machine for WZ Current Log from Live: ${machine}"
        echo ""

        rsync -m -avnz --include="/*Server*/*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="/*Server*/*log*" --exclude="/*/*" --exclude="/Data" --exclude="/bin" --exclude="/config.ini" --exclude="/db" --exclude="/setup.ini" --ignore-existing --recursive -e ssh ${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

# MS: Current Log from LIVE
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 $NF}')
do
        echo "======"
        echo "RSYNC Machine for MS Current Log from Live: ${machine}"
        echo ""

        rsync -m -avnz --include="/*Server*/*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_ms_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 ${machine}:${remote_log_cur_directory}/ .

        for file in $(cat ${local_rsync_log_ms_cur} | egrep 'twolevel|AccountMail')
        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 777 ${local_log_old_directory}
chmod 777 ${local_log_cur_directory}

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