#!/bin/bash
#===============================================================================
#
#          FILE: localize
#
#         USAGE: localize
#
#   DESCRIPTION: Sync translated ini file to the server and client patch.
#
#       OPTIONS: N/A
#
#  REQUIREMENTS: TestCtrl
#
#         NOTES: 
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#                vincent.tu (Vincent Tu), xvincent.tu@x-legend.com.tw
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Tue May 31 23:45:02 EDT 2011
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
set -m                                       # Enable job control 

source ~/.gamerc

ps -C $TEST_CONTROL_SCRIPT && exec echo "The script localize already running."

#[ "$(ps -C localize | wc -l)" -gt 1 ] && "The script localize already running."

[ "$HOST_NAME" == "TEST" -o "$HOST_NAME" == "DATA" ] || exit

[ -f ~/.localize_ctime_de ] || touch ~/.localize_ctime_de

LOCALIZE_DIR_CTIME_OLD="$(cat ~/.localize_ctime_de)"
LOCALIZE_DIR_CTIME_NOW="$(stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR_DE/GameDataTranslateFileList_de.txt")"

if [ "$LOCALIZE_DIR_CTIME_OLD" == "$LOCALIZE_DIR_CTIME_NOW" ] ; then

   exec echo "No new translated ini uploaded
force localize to run? please run this command and run localize again:

rm -f ~/.localize_ctime_de
"

else

   # ulog
   UPLOADER="$(sudo tail -1 /var/log/xferlog|awk '{print $7}')"
   echo "Detectd new translation files upload from $UPLOADER" | ulogger $0 Begin

   # Backup current translation files
   ~/bin/ibackup backup translate_de

   # Copy the translation files
   stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR_DE/GameDataTranslateFileList_de.txt" > ~/.localize_ctime_de
   for WORLD in $TRANSLATED_INI_APPLY_TO ; do
	cp -axvf $TRANSLATED_INI_UPLOAD_DIR_DE/GameDataTranslateFileList_de.txt ~/www/$GAME_TYPE-$WORLD/
	cp -axvf $TRANSLATED_INI_UPLOAD_DIR_DE/nfs/* ~/www/$GAME_TYPE-$WORLD/nfs/
	#~/bin/fn_localize/generate_localize_md5 LOCALIZE DE
	#~/bin/fn_localize/generate_localize_md5 CLIENT DE
   done
   
   echo "Translation files copied to game server directories" | ulogger $0

   echo "TEST server restarted by localize" | ulogger $0 =End=
fi

