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

SERVER=$1
DATE=$2
SET_ID=$3
FILTER=$4
TAG=$5

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

                cd $HOME/servers${SET_ID}/Log/${DATE};
                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} | egrep \"${FILTER}\" > /tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}/${output_file}_f"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} | egrep "${FILTER}" > /tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}/${output_file}_f; done; done

                cd /tmp/s${SET_ID}_${DATE}_${TAG};
                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} | sed 's/\\x01/,/g' > ${file}; rm -rf ${tmp_file}; done
                find . -type f -size 0 -delete
                ;;

        World)
                cd $HOME/servers${SET_ID}/Log/${DATE};
                for dir in $(ls -d WorldServer*); do server="WS"; id=$(echo ${dir}|sed "s/WorldServer${SET_ID}//"); [ ! -d "/tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}${id}" ] && mkdir -p /tmp/s${SET_ID}_${DATE}_${TAG}/${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} | egrep \"${FILTER}\" > /tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}${id}/${output_file}_f"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} | egrep "${FILTER}" > /tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}${id}/${output_file}_f; done; done

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

        Zone)
                cd $HOME/servers${SET_ID}/Log/${DATE};
                for dir in $(ls -d ZoneServer*); do server="ZS"; id=$(echo ${dir}|sed "s/ZoneServer${SET_ID}//"); [ ! -d "/tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}${id}" ] && mkdir -p /tmp/s${SET_ID}_${DATE}_${TAG}/${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} | egrep \"${FILTER}\" > /tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}${id}/${output_file}_f"; xzcat $HOME/servers${SET_ID}/Log/${DATE}/${dir}/${file} | egrep "${FILTER}" > /tmp/s${SET_ID}_${DATE}_${TAG}/${server}${SET_ID}${id}/${output_file}_f; done; done

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