#!/bin/bash

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

# root path of offline logs
_gate_path="/export/client/rizhi"
_ycs=`echo "${HOSTNAME}" |awk -F\- '{print $1$2}'`
_gatenum=${HOSTNAME##*loggate}
#_num=${_ycs}${_gatenum}
_num=${_ycs}
_logfhour=`date +%Y%m%d_%H`

_dq=`date -R|awk '{print  $NF}' |cut -c 1`
_sq=`date -R|awk '{print  $NF}' |cut -c 2-3`
case ${_dq} in
    +)
    _local_timezone="${_sq#0}"
    ;;
    -)
    _local_timezone="-${_sq#0}"
    ;;
    *)
    echo "`date "+%F %T"` localtimezone get error"
    exit
    ;;
esac

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

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

_projectid=${1}
_rsyncToIp=${2}
_rsyncMode=${3}
_rsyncPort=${4}

_server_timezone=${5}
if [ "${_server_timezone}k" == "k" ];then
  _server_timezone="${_local_timezone}"
fi

echo "rsync${_rsyncMode}123s56.."  >${_bigateBin}/${_rsyncMode}_rsync.pass
chmod 600 ${_bigateBin}/${_rsyncMode}_rsync.pass
#shi cha
_hnum=$(( ${_local_timezone} - ${_server_timezone} + 1 ))

if [ $# -gt 5 ]; then
    _yesterday=${6}
else
    _yesterday=`date -d "${_hnum} hours ago" +%Y-%m-%d`
fi

if [ $# -gt 6 ]; then
    _fileHor=${7}
else
    _fileHor=`date -d "${_hnum} hours ago" +%H`
fi

_fileDay=`date -d "0 days ago ${_yesterday}" +%Y%m%d`

mkdir -p ${_bigateBin}/logs
exec &>> ${_bigateBin}/logs/${_rsyncMode}-${_yesterday}.log
echo "===`date +%Y-%m-%d-%H-%M-%S` ${0} loggatenum:${_num} localtimezone:${_local_timezone} servertimezone:${_server_timezone} ${_projectid}_${_rsyncMode}_${_yesterday}-${_fileHor}_${_rsyncToIp} start==="

_bigateRizhi=${_gate_path}/${_projectid}
if [ ! -d ${_bigateRizhi} ];then
    echo "`date "+%F %T"` not found ${_bigateRizhi}"
    exit
fi
_logtou="${_projectid}"
rsync_file=${_gate_path}/rsyncdir/${_rsyncMode}/${_yesterday}
mkdir -p ${rsync_file}/logs

pushd ${rsync_file} > /dev/null

echo "`date "+%F %T"` ${_rsyncMode}-${_yesterday}-${_fileHor} get file start"
# mv all files to ${rsync_file}
_dayList=(-2 -1 0 1 2 3 4 5 6)
for _day in ${_dayList[@]}
do
    _curDay=`date -d "${_day} days ago ${_yesterday}" +%Y-%m-%d`
    _fnum=`ls ${_bigateRizhi}/${_curDay}/${_logtou}*.log 2>/dev/null | wc -l`
    if [ ${_fnum} -gt 0 ];then
        echo "--------`date "+%F %T"` mqian ${_bigateRizhi}/${_curDay}--------"
        ls -l ${_bigateRizhi}/${_curDay}/ && echo

        /bin/rm -f ${rsync_file}/logs/${_logtou}*.log
        for _gff in `ls ${_bigateRizhi}/${_curDay}/${_logtou}*.log |grep -v "_${_logfhour}_" `
        do
            mv -f ${_gff} ${rsync_file}/logs/
        done
        echo "--------`date "+%F %T"` mhou ${_bigateRizhi}/${_curDay}--------"
        ls -l ${_bigateRizhi}/${_curDay}/ && echo
        echo "--------`date "+%F %T"` mlog ${rsync_file}/logs/ ${_curDay}--------"
        ls -l ${rsync_file}/logs/ && echo
        echo "------------------------------------------"

        [ `ls ${rsync_file}/logs/${_logtou}*.log 2>/dev/null | wc -l` -gt 0 ] || continue

        #cat logs files to onefile
        _fname=${_curDay}_${_fileDay}-${_fileHor}_BiGate${_num}_${_projectid}
        cat ${rsync_file}/logs/${_logtou}*.log |gzip -c > ${_fname}.gz && md5sum ${_fname}.gz > ${_fname}.md5 && /bin/rm -f ${rsync_file}/logs/${_logtou}*.log
        echo "--------`date "+%F %T"` ${rsync_file}/${_fname}--------"
        ls -l ${rsync_file}/${_fname}*
        echo "------------------------------------------"

    fi
done

# rsync tar files
_rsyncName=*_${_fileDay}-${_fileHor}_BiGate${_num}_${_projectid}
_fnum=`ls ${_rsyncName}.gz ${_rsyncName}.md5 2>/dev/null |wc -l`
if [ ${_fnum} -gt 1 ];then
    pwd
    #md5sum *${_fileDay}-${_fileHor}_BiGate${_num}_${_projectid}*  > BiGate${_num}_${_yesterday}_${_fileHor}_${_projectid}.md5
    cat *${_fileDay}-${_fileHor}_BiGate${_num}_${_projectid}.md5  > BiGate${_num}_${_yesterday}_${_fileHor}_${_projectid}.md5
    ls -l ${_rsyncName}.gz ${_rsyncName}.md5
    echo
    echo "`date "+%F %T"` ${_rsyncMode}-${_yesterday}-${_fileHor} rsync file start"
    # mv zip files to history
    #mkdir -p ${_gate_path}/rsyncdir/${_rsyncMode}/${_yesterday}  && mv -f ${_rsyncName}.gz ${_rsyncName}.md5 ${_gate_path}/rsyncdir/${_rsyncMode}/${_yesterday}/
    rsync -avzc ${_gate_path}/rsyncdir/${_rsyncMode}/${_yesterday}/*BiGate*_${_projectid}*  rsync@$_rsyncToIp::${_rsyncMode}/${_yesterday}/ --password-file=${_bigateBin}/${_rsyncMode}_rsync.pass --bwlimit=5120  --contimeout=180  --port=${_rsyncPort}
    if [ $? == 0 ];then
        echo "`date "+%F %T"` ${_rsyncMode}-${_yesterday}-${_fileHor} rsync file ok"
    else
        echo "`date "+%F %T"` ${_rsyncMode}-${_yesterday}-${_fileHor} rsync file error"
    fi
else
    echo "`date "+%F %T"` ${_rsyncMode}-${_yesterday}-${_fileHor} not found file"
fi

# clean cur path ${rsync_file}
#/bin/rm -rf ${rsync_file}/*

popd > /dev/null

echo "===`date +%Y-%m-%d-%H-%M-%S` ${0} loggatenum:${_num} localtimezone:${_local_timezone} servertimezone:${_server_timezone} ${_projectid}_${_rsyncMode}_${_yesterday}-${_fileHor}_${_rsyncToIp} end==="
