#!/bin/bash
#===============================================================================
#
#          FILE: get_rankaward_list
#
#         USAGE: get_rankaward_list [YYYYmm]
#
#   DESCRIPTION: Get RankAward list and save the list.
#
#  REQUIREMENTS: sendscript
#
#         NOTES: 
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Tue Sep 25 11:10:38 KST 2012
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control
source ~/.gamerc

echo
cd $WORKING_DIRECTORY

[ "$1" == "" ] && DAY="$(date +%Y%m%d)" || DAY="$1"

date -d $DAY +%s 2> /dev/null | grep -q "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$" \
|| exec echo "Error: Invalid month. Accept YYYYMMDD only. eg. 20120905"

ulogger_echo_no_nl "Saving the RankAward list ... " | colorize yellow black

if [ -d $WORKING_DIRECTORY/RankAward/$DAY/ ] ;then
	ulogger_echo "Already saved the list for today's maintenance." | colorize cyan
	wc -l $PWD/RankAward/$DAY/* | grep GAMEDB
	wc -l $PWD/RankAward/$DAY/* | grep GAMEDB | ulogger $0
	exit
fi

sendscript allgamedb -s RankAward/$DAY <<< 'xgrep -h RankAward ~/servers*/Log/'"$DAY"'/Mission*/Test* ~/servers*/Mission*/Test* ~/servers*/Log/'"$DAY"'/Mission*/Colosseum* ~/servers*/Mission*/Colosseum* 2>&1|sed -n "/RankAward Start/,/RankAward End/p"'

cd RankAward/$DAY
for file in *-output ; do
	set_number="$(get_set $(awk -F"[_-]" '{print $1}' <<< "$file"))"
	mv $file GAMEDB$set_number 2> /dev/null
done
cd ../..

ulogger_echo "Result:" | colorize green

wc -l $PWD/RankAward/$DAY/* | grep GAMEDB
wc -l $PWD/RankAward/$DAY/* | grep GAMEDB | ulogger $0

