#!/bin/bash

source /etc/profile
_gameId="21890001"
_dir=$(cd "$(dirname "$0")"; pwd)

# support date param
if [ $# -gt 0 ]; then
    _date=$1
    _hour=$2
else
    _date=`date -d "1 hours ago" +%Y-%m-%d`
    _hour=`date -d "1 hours ago" +%H`
fi

_HOST=`hostname`
ZONEID=` echo $HOSTNAME |awk -F "-" '{print $NF}' `
BI_ADDR="150.109.29.152"
BI_PORT="10001"
BI_USER="rsync"
BI_MODU="lzeuus218"
RSYNC_PASS="${_dir}/rsync.pass"
echo "rsync${BI_MODU}123s56.." >${RSYNC_PASS}
chmod 600 $RSYNC_PASS

# decide fileRoot and fileame
if [ -f "/export/game/global/auany/data/logs/bi//auany.bi.log" ];then
   ZONEID="`echo $_HOST | cut -d \- -f3`auany"
#   ZONEID="auany"
   pushd /export/game/global/auany/data/logs/bi/  > /dev/null
        _nowdate=`date  +%Y-%m-%d`
        DST="${_date}_${ZONEID}_${_gameId}.log"
        if [ "${_date}k" == "${_nowdate}k" ];then
           ln -snf auany.bi.log ${DST}
        else
           ln -snf auany.bi.log.${_date} ${DST}
        fi

        file_name=${_date}-${_hour}_${ZONEID}_${_gameId}
        [ -f ${file_name}.gz ] || (grep "^${_date} ${_hour}:" ${DST}  |gzip -c  > ${file_name}.gz && md5sum ${file_name}.gz > ${file_name}.md5 )

        /bin/rm -f ${DST}
        _dstfile="${_date}-${_hour}_${ZONEID}_${_gameId}"
        echo "`date "+%F %T"`"  &>>${_dir}/rsync_log.log
        rsync -avzc --bwlimit=5000 --port=${BI_PORT} --password-file=${RSYNC_PASS} ${_dstfile}.* ${BI_USER}@${BI_ADDR}::${BI_MODU}/${_date}/ &>>${_dir}/rsync_log.log
        _2day=`date -d "2 days ago ${_date}" +%Y-%m-%d` && /bin/rm -rf ${_2day}*.gz ${_2day}*.md5

   popd  > /dev/null
else
   if [ ! -d /export/game/rungame/log//${_date} ];then
      exit
   fi
   pushd /export/game/rungame/log//${_date}  > /dev/null
        file_name=${_date}-${_hour}_${ZONEID}_${_gameId}
        _h="${_hour#0}"
        [ -f ${file_name}.gz ] || (cat level1.log level2.log  level1.log.* level2.log.*  |grep "^${_date} ${_hour}:"  |gzip -c  > ${file_name}.gz && md5sum ${file_name}.gz > ${file_name}.md5 )
        _dstfile="${_date}-${_hour}_${ZONEID}_${_gameId}"
        echo "`date "+%F %T"`"  &>>${_dir}/rsync_log.log
        rsync -avzc --bwlimit=5000 --port=${BI_PORT} --password-file=${RSYNC_PASS} ${_dstfile}.* ${BI_USER}@${BI_ADDR}::${BI_MODU}/${_date}/ &>>${_dir}/rsync_log.log
       
   popd  > /dev/null
fi
