#!/bin/bash

source /etc/profile
_dir=$(cd "$(dirname "$0")"; pwd)
_shname=`basename $0`

# 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}' `
_gameId="23200000"
BI_ADDR="10.20.33.15"
BI_PORT="10001"
BI_USER="rsync"
BI_MODU="tfx"
RSYNC_PASS="${_dir}/${_shname%.sh}.pass"
echo "rsync${BI_MODU}123s56.." >${RSYNC_PASS}
chmod 600 $RSYNC_PASS

_glogdir="/export/server/im_home/zlogd/logs/${ZONEID}"
_aulogdir="/export/auany/data/logs/bi"

if [ -f "${_aulogdir}/auany.bi.log" ];then
   #ZONEID="`echo $_HOST | cut -d \- -f2`auany"
   pushd ${_aulogdir}  > /dev/null

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

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

   popd  > /dev/null
else
   #ZONEID=`cat  /export/config_xml/*_game.xml |grep  '<common' |grep 'seasonid="' |grep 'zoneid="' |grep 'zone_subject="'  |awk -F'zoneid="' '{print $2}' |awk -F\" '{print $1}' |sort |head -n 1 `
   if [ "${ZONEID}k" == "k" ];then
      echo "zoneid not found"
      exit
   fi
   if [ ! -d ${_glogdir}/${_date} ];then
      exit
   fi
   pushd ${_glogdir}/${_date}  > /dev/null
        file_name=${_date}-${_hour}_${ZONEID}_${_gameId}
        _day=`echo "${_date}" |sed 's/-//g'`
        [ -f ${file_name}.gz ] || (cat level1.log* level2.log*  |grep -a "^${_date} ${_hour}:"  |gzip -c  > ${file_name}.gz && md5sum ${file_name}.gz > ${file_name}.md5 )
        _dstfile="${_date}-${_hour}_${ZONEID}_${_gameId}"
        rsync -avz --bwlimit=5000 --port=${BI_PORT} --password-file=${RSYNC_PASS} ${_dstfile}.* ${BI_USER}@${BI_ADDR}::${BI_MODU}/${_date}/ &>>${_dir}/${_shname%.sh}.log
       
   popd  > /dev/null
fi
