#!/bin/sh

# Copyright (c) 2012 ken.naruo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

## global conf load
LANG=C;export LANG
SCRIPT_DIR=`dirname $0`
 . ${SCRIPT_DIR}/../global.conf

## process check
if [ -f /tmp/ecron_mirrord_lock ];then
  exit 0
else
  touch /tmp/ecron_mirrord_lock
fi


IFCONFIG=/usr/sbin/ifconfig
LINUX=`uname -a | grep Linux`
if [ "${LINUX}" ];then
  IFCONFIG=/sbin/ifconfig
fi

## global.conf load
startup () {
 . ${SCRIPT_DIR}/../global.conf
}
trap startup HUP
startup


while true
do
 . ${SCRIPT_DIR}/../global.conf
 UNAME=`uname -n`
 ERROR_QUE=${ROOT}/que/daemon_error/${UNAME}_`date +%Y%m%d`

 while read RHOST
 do

 SERVER=`echo ${RHOST} | awk  -F":" '{print $1}'`
 SSH_PORT=`echo ${RHOST} | awk  -F":" '{print $2}'`
 SERVER_STATUS_QUE=${ROOT}/que/server_status/${SERVER}_mirror

 IP_ADDR=`dig ${SERVER} | grep -A 1 "ANSWER SECTION" | grep -v "ANSWER SECTION" | awk '{print $5}'`
 LOCAL_CHECK=`${IFCONFIG} -a | grep ${IP_ADDR}`

  ##rsync from execution server to console server
  . ${ROOT}/servers/${SERVER}/server.conf
  SERVER_SYNC_CHECK=""

  if [ "${LOCAL_CHECK}" = "" ];then

    if [ ! -f ${ROOT}/que/daemon_error/${SERVER}_monitor_error ];then

      rsync -e "ssh -p ${SSH_PORT}" -alv --delete ${SERVER}:${USER_ROOT}/que/ ${ROOT}/servers/${SERVER}/que  > /dev/null 2>&1
      SERVER_SYNC_CHECK=$?

    fi

  else

    rsync  -alv --delete ${USER_ROOT}/que/ ${ROOT}/servers/${SERVER}/que  > /dev/null 2>&1
    SERVER_SYNC_CHECK=$?

  fi

  if [ "${SERVER_SYNC_CHECK}" != 0 ];then

     echo "`date +%Y/%m/%d` `date +%H:%M:%S` ${SERVER} que mirror failed" >  ${SERVER_STATUS_QUE}

     if [ ! -f ${ROOT}/que/daemon_error/${SERVER}_mirror_error ];then

       touch ${ROOT}/que/daemon_error/${SERVER}_mirror_error
       echo "`date +%Y/%m/%d` `date +%H:%M:%S` ecron_mirrord: ${SERVER} que mirror failed" >> ${ERROR_QUE}

       if [ "${MAIL_ADDR}" ];then
         echo "ERROR ecron_mirrord ${SERVER} que mirror failed"| mailx -s "WARN ${UNAME} ecron_mirrord" ${MAIL_ADDR}
       fi

     fi

  else

     echo "`date +%Y/%m/%d` `date +%H:%M:%S` ${SERVER} que mirrored " > ${SERVER_STATUS_QUE}

     if [ -f ${ROOT}/que/daemon_error/${SERVER}_mirror_error ];then
       rm  ${ROOT}/que/daemon_error/${SERVER}_mirror_error
     fi

  fi


 done < ${ROOT}/map.conf

 sleep 60

done
