#!/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_my ] || touch ~/.localize_ctime_my

LOCALIZE_DIR_CTIME_OLD="$(cat ~/.localize_ctime_my)"
if [ "$GAME_TYPE" == "M2" ]; then
        LOCALIZE_DIR_CTIME_NOW="$(stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR/translatefilelist.txt")"
elif [ "$GAME_TYPE" == "M12" ]; then
        LOCALIZE_DIR_CTIME_NOW="$(stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR_MY/GameDataTranslateFileList_my.txt")"
else
        LOCALIZE_DIR_CTIME_NOW="$(stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR/GameDataTranslateFileList.txt")"
fi

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_my
"

else

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

   # Copy the translation files
   if [ "$GAME_TYPE" == "M2" ]; then
        stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR/translatefilelist.txt" > ~/.localize_ctime
   elif [ "$GAME_TYPE" == "M12" ]; then
        stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR_MY/GameDataTranslateFileList_my.txt" > ~/.localize_ctime_my
   else
        stat -c %Z "$TRANSLATED_INI_UPLOAD_DIR/GameDataTranslateFileList.txt" > ~/.localize_ctime
   fi
   for WORLD in $TRANSLATED_INI_APPLY_TO ; do

        if [ "$GAME_TYPE" == "DJ" ] ; then
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/*.bz2 ~/www/$GAME_TYPE-$WORLD/2*/data/Translate/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/*.txt ~/www/$GAME_TYPE-$WORLD/2*/data/Translate/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/*.txt ~/www/$GAME_TYPE-$WORLD/2*/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/*.ini $WORKING_DIRECTORY/common-$WORLD/Data/Translate/
	elif [ "$GAME_TYPE" == "GD" ] ; then
	       cp -axvf $TRANSLATED_INI_UPLOAD_DIR/data/db/T_*.ini $WORKING_DIRECTORY/common-$WORLD/Data/db/
	       cp -axvf $TRANSLATED_INI_UPLOAD_DIR/_GameDataTranslateFileList.txt ~/www/$GAME_TYPE-$WORLD/2*/
	       cp -axvf $TRANSLATED_INI_UPLOAD_DIR/data/db/_T_*.ini ~/www/$GAME_TYPE-$WORLD/2*/data/db/
        elif [ "$GAME_TYPE" == "M2" ] ; then
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/translatefilelist.txt ~/www/$GAME_TYPE-$WORLD/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/data/ini/t_*.ini.cat ~/www/$GAME_TYPE-$WORLD/data/ini/
        elif [ "$GAME_TYPE" == "LA" ] ; then
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/T_*.ini $WORKING_DIRECTORY/common-$WORLD/Data/db/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/GameDataTranslateFileList.txt ~/www/$GAME_TYPE-$WORLD/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/nfs/* ~/www/$GAME_TYPE-$WORLD/nfs/
        elif [ "$GAME_TYPE" == "AK" ] ; then
	       # TEST
	       cp -axvf $TRANSLATED_INI_UPLOAD_DIR/t_*.ini $WORKING_DIRECTORY/common-$WORLD/Data/db/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/GameDataTranslateFileList.txt ~/www/$GAME_TYPE-$WORLD/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/nfs/* ~/www/$GAME_TYPE-$WORLD/nfs/
	       # DATA
	       cp -axvf $TRANSLATED_INI_UPLOAD_DIR/t_*.ini $WORKING_DIRECTORY/common-DATA/Data/db/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/GameDataTranslateFileList.txt ~/www/$GAME_TYPE-DATA/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/nfs/* ~/www/$GAME_TYPE-DATA/nfs/
	elif [ "$GAME_TYPE" == "M12"  -o "$GAME_TYPE" == "G1" -o "$GAME_TYPE" == "G2" ] ; then
	       cp -axvf $TRANSLATED_INI_UPLOAD_DIR_MY/GameDataTranslateFileList_my.txt ~/www/$GAME_TYPE-$WORLD/
	       cp -axvf $TRANSLATED_INI_UPLOAD_DIR_MY/nfs/* ~/www/$GAME_TYPE-$WORLD/nfs/
        else
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/T_*.ini $WORKING_DIRECTORY/common-$WORLD/Data/db/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/_GameDataTranslateFileList.txt ~/www/$GAME_TYPE-$WORLD/2*/
               cp -axvf $TRANSLATED_INI_UPLOAD_DIR/_T_*.ini ~/www/$GAME_TYPE-$WORLD/2*/data/db/
        fi

   done
   
   echo "Translation files copied to game server directories" | ulogger $0

   #if [ "$GAME_TYPE" != "M2" ] && [ "$GAME_TYPE" != "AK" ] ; then
   if [ "$GAME_TYPE" != "M2" ] && [ "$GAME_TYPE" != "M12" ] && [ "$GAME_TYPE" != "G1" ] && [ "$GAME_TYPE" != "G2" ] ; then
        TestCtrl stop
        yes|TestCtrl item2db
        TestCtrl start
   fi

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

