#!/bin/bash

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

_gate_path="/export/client/rizhi"
_rsyncToIp="10.20.33.16"
_rsyncMode="clogfile"
_rsyncPort="10001"

if [ $# -lt 1 ]; then
    echo "Usage: ${0} gameid"
    exit 1 
fi

me=`whoami`
if [ ${me} != root ];then
    echo "ERROR: su root please !!!"
    exit 1
fi

_projectid=${1}

echo "rsync${_rsyncMode}123s56.."  >${_bigateBin}/${_rsyncMode}_rsync.pass
chmod 600 ${_bigateBin}/${_rsyncMode}_rsync.pass

_opshour=`date +%Y%m%d_%H`
_oopshour=`date -d "5 minutes ago" +%Y%m%d_%H`
_opsdir="/export/client/rizhi/ops/${_projectid}"

_yesterday=`date +%F`

mkdir -p ${_bigateBin}/logs
exec &>> ${_bigateBin}/logs/${_rsyncMode}_${_projectid}_${_yesterday}.log

_bigateRizhi=${_gate_path}/${_projectid}
if [ ! -d ${_bigateRizhi} ];then
    echo "`date "+%F %T"` not found ${_bigateRizhi}"
    exit
fi

echo "================`date "+%F %T"` rsync ${_gate_path}/${_projectid} to ${_rsyncMode} start================"

_dayList=(-3 -2 -1 0 1 2 3 4 5 6 7)
for _day in ${_dayList[@]}
do
    _curDay=`date -d "${_day} days ago ${_yesterday}" +%Y-%m-%d`
    _fnum=`ls ${_bigateRizhi}/${_curDay}/*.log 2>/dev/null | wc -l`
    if [ ${_fnum} -gt 0 ];then
        ls -l ${_bigateRizhi}/${_curDay}/ && echo
        rsync -avzc ${_bigateRizhi}/${_curDay}  rsync@$_rsyncToIp::${_rsyncMode}/${_projectid}/ --password-file=${_bigateBin}/${_rsyncMode}_rsync.pass --bwlimit=5120  --contimeout=180  --port=${_rsyncPort}
        if [ $? == 0 ];then
            echo "`date "+%F %T"` rsync ${_projectid}-${_curDay} file ok"
        else
            echo "`date "+%F %T"` rsync ${_projectid}-${_curDay} file error"
        fi
    else
        echo "`date "+%F %T"` ${_projectid}-${_curDay} not found file"
    fi
done

if [ "${_opshour}k" == "${_oopshour}k" ];then
  opsf="${_projectid}_ClientLog_${_opshour}.log"
else
  opsf="${_projectid}_ClientLog_${_opshour}.log  ${_projectid}_ClientLog_${_oopshour}.log"
fi

for f in `echo "${opsf}"`
do
  if [ -f ${_opsdir}/${f} ];then
    ls -l ${_opsdir}/${f}
    rsync -avzc ${_opsdir}/${f}  rsync@$_rsyncToIp::${_rsyncMode}/${_projectid}/ops/${HOSTNAME}_${f} --password-file=${_bigateBin}/${_rsyncMode}_rsync.pass --bwlimit=5120  --contimeout=180  --port=${_rsyncPort}
    if [ $? == 0 ];then
      echo "`date "+%F %T"` rsync ${_projectid}-ops-${f} file ok"
    else
      echo "`date "+%F %T"` rsync ${_projectid}-ops-${f} file error"
    fi
  fi
done

echo "================`date "+%F %T"` rsync ${_gate_path}/${_projectid} to ${_rsyncMode} end================"
