#!/bin/bash
set -m                                       # Enable job control 

source ~/.gamerc

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

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

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

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

BANNER_DIR_CTIME_OLD="$(cat ~/.banner_ctime)"
BANNER_DIR_CTIME_NOW="$(stat -c %Z "$BANNER_UPLOAD_DIR/ImageFileList.txt")"

if [ "$BANNER_DIR_CTIME_OLD" == "$BANNER_DIR_CTIME_NOW" ] ; then

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

rm -f ~/.banner_ctime
"

else

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

   # Copy the banner files
   stat -c %Z "$BANNER_UPLOAD_DIR/ImageFileList.txt" > ~/.banner_ctime

   for WORLD in $BANNER_APPLY_TO ; do

	if [ "$GAME_TYPE" == "M12" -o "$GAME_TYPE" == "G1" ] ; then
	       cp -axvf $BANNER_UPLOAD_DIR/ImageFileList.txt ~/www/$GAME_TYPE-$WORLD/
	       cp -axvf $BANNER_UPLOAD_DIR/nfs/* ~/www/$GAME_TYPE-$WORLD/nfs/
        fi

   done
   
   echo "Banner files copied to game server directories" | ulogger $0
   echo "Banner files copied to game server directories" | ulogger =End=

fi

