#!/bin/bash
[ "$3" == "" ] && exec echo "Usage: $0 <Mission/World/Zone> <DATE (YYYYMMDD)> <SET_ID> <SKIP_SERVER_LOG_NAME>"

SERVER=$1
DATE=$2
SET_ID=$3
SKIP_SERVER_LOG_NAME=$4

case ${SERVER} in
        Mission)
                mkdir /tmp/s${SET_ID}_${DATE}; cd /tmp/s${SET_ID}_${DATE}; for server in MS; do for set in ${SET_ID}; do mkdir -p /tmp/s${SET_ID}_${DATE}/${server}${set}; done; done

                cd $HOME/servers${SET_ID}/Log/${DATE};
		if [ ! -z "${SKIP_SERVER_LOG_NAME}" ]; then
			for dir in $(ls -d MissionServer${SET_ID}); do server="MS"; cd $HOME/servers${SET_ID}/Log/${DATE}/${dir}; for file in $(ls *xz); do checked=1; for keyword in ${SKIP_SERVER_LOG_NAME}; do if [[ ${file} == *"${keyword}"* ]]; then checked=0; break; fi; done; if [ ${checked} -eq "1" ]; then output_file=$(echo ${file}|sed 's/.xz//'); echo "xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}/${output_file}"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}/${output_file}; fi; done; done
		else
	                for dir in $(ls -d MissionServer${SET_ID}); do server="MS"; cd $HOME/servers${SET_ID}/Log/${DATE}/${dir}; for file in $(ls *xz); do output_file=$(echo ${file}|sed 's/.xz//'); echo "xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}/${output_file}"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}/${output_file}; done; done
		fi

                cd /tmp/s${SET_ID}_${DATE};
                for file in $(ls MS${SET_ID}/*level*.log*); do echo ${file}; tmp_file=$(echo ${file}|sed 's/$/_tmp/'); mv ${file} ${tmp_file}; cat ${tmp_file} | twolevel_comma > ${file}; rm -rf ${tmp_file}; done
                find . -type f -size 0 -delete
                ;;

        World)
                cd $HOME/servers${SET_ID}/Log/${DATE};
		if [ ! -z "${SKIP_SERVER_LOG_NAME}" ]; then
			for dir in $(ls -d WorldServer*); do server="WS"; id=$(echo ${dir}|sed "s/WorldServer${SET_ID}//"); [ ! -d "/tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}" ] && mkdir -p /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}; cd $HOME/servers${SET_ID}/Log/${DATE}/${dir}; for file in $(ls *xz); do checked=1; for keyword in ${SKIP_SERVER_LOG_NAME}; do if [[ ${file} == *"${keyword}"* ]]; then checked=0; break; fi; done; if [ ${checked} -eq "1" ]; then output_file=$(echo ${file}|sed 's/.xz//'); echo "xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}; fi; done; done
		else
                	for dir in $(ls -d WorldServer*); do server="WS"; id=$(echo ${dir}|sed "s/WorldServer${SET_ID}//"); [ ! -d "/tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}" ] && mkdir -p /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}; cd $HOME/servers${SET_ID}/Log/${DATE}/${dir}; for file in $(ls *xz); do output_file=$(echo ${file}|sed 's/.xz//'); echo "xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}; done; done
		fi

                cd /tmp/s${SET_ID}_${DATE};
                find . -type f -size 0 -delete
                ;;

        Zone)
                cd $HOME/servers${SET_ID}/Log/${DATE};
		if [ ! -z "${SKIP_SERVER_LOG_NAME}" ]; then
			for dir in $(ls -d ZoneServer*); do server="ZS"; id=$(echo ${dir}|sed "s/ZoneServer${SET_ID}//"); [ ! -d "/tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}" ] && mkdir -p /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}; cd $HOME/servers${SET_ID}/Log/${DATE}/${dir}; for file in $(ls *xz); do checked=1; for keyword in ${SKIP_SERVER_LOG_NAME}; do if [[ ${file} == *"${keyword}"* ]]; then checked=0; break; fi; done; if [ ${checked} -eq "1" ]; then output_file=$(echo ${file}|sed 's/.xz//'); echo "xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}; fi; done; done
		else
                	for dir in $(ls -d ZoneServer*); do server="ZS"; id=$(echo ${dir}|sed "s/ZoneServer${SET_ID}//"); [ ! -d "/tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}" ] && mkdir -p /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}; cd $HOME/servers${SET_ID}/Log/${DATE}/${dir}; for file in $(ls *xz); do output_file=$(echo ${file}|sed 's/.xz//'); echo "xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} > /tmp/s${SET_ID}_${DATE}/${server}${SET_ID}${id}/${output_file}; done; done
		fi

                cd /tmp/s${SET_ID}_${DATE};
                find . -type f -size 0 -delete
                ;;
        *)
                exec echo "Wrong Server Name Input!"
esac
