#!/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_messaged_lock ];then
  exit 0
else
 touch /tmp/ecron_messaged_lock
fi

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

## global.conf load
startup () {
  . ${ROOT}/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`

 for RHOST in `cat ${ROOT}/map.conf`
 do

 SERVER_SET=`echo "${RHOST}" | awk '{print $1}'`
 SERVER=`echo ${SERVER_SET} | awk  -F":" '{print $1}'`
 SSH_PORT=`echo ${SERVER_SET} | awk  -F":" '{print $2}'`

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

  ##rsync from server to console
  if [ -f ${ROOT}/servers/${SERVER}/server.conf ];then
    . ${ROOT}/servers/${SERVER}/server.conf

    for MAIL in `ls -t ${ROOT}/servers/${SERVER}/que/mail`
    do
       JOBNET_MAIL_ADDR=`grep MAIL_ADDR: ${ROOT}/servers/${SERVER}/que/mail/${MAIL} | sed -e s/MAIL_ADDR://g`
       SUBJECT=`grep SUBJECT: ${ROOT}/servers/${SERVER}/que/mail/${MAIL} | sed -e s/SUBJECT://g`
       CONTENTS=`cat ${ROOT}/servers/${SERVER}/que/mail/${MAIL} | grep -v MAIL_ADDR: | grep -v SUBJECT: `
       echo "${CONTENTS}" | mailx -s "${SUBJECT}" ${JOBNET_MAIL_ADDR}

       if [ "${LOCAL_CHECK_1}" = "" ];then
         ssh -o "ConnectTimeout 5" -p ${SSH_PORT} ${SERVER} rm ${USER_ROOT}/que/mail/${MAIL}
       else
          rm ${USER_ROOT}/que/mail/${MAIL}
       fi

       rm -f ${ROOT}/servers/${SERVER}/que/mail/${MAIL}

    done

   else

      echo "`date +%Y/%m/%d` `date +%H:%M:%S` ecron_messaged: ${SERVER} server.conf not found" >> ${ERROR_QUE}

      if [ "${MAIL_ADDR}" ];then
        echo "message ${MESSAGE} ${SERVER}  server.conf not found "|mailx -s "WARN ${UNAME} ecron_messaged" ${MAIL_ADDR}
      fi

   fi

   for REMOTE_MESSAGE in `ls ${ROOT}/servers/${SERVER}/que/message 2> /dev/null`
   do
      case ${REMOTE_MESSAGE} in

      *::*)
        REMOTE_SERVER=`echo ${REMOTE_MESSAGE} | awk -F"::" '{print $1}'`
        MESSAGE=`echo ${REMOTE_MESSAGE} | awk -F"::" '{print $2}'`
        IP_ADDR=`dig ${REMOTE_SERVER} | grep -A 1 "ANSWER SECTION" | grep -v "ANSWER SECTION" | awk '{print $5}'`
        LOCAL_CHECK_2=`${IFCONFIG} -a | grep ${IP_ADDR}`

        if [ -f ${ROOT}/servers/${REMOTE_SERVER}/server.conf ];then
          . ${ROOT}/servers/${REMOTE_SERVER}/server.conf

          if [ "${LOCAL_CHECK_2}" = "" ];then
            ssh -o "ConnectTimeout 5" -p ${SSH_PORT} ${REMOTE_SERVER} touch ${USER_ROOT}/que/message/${MESSAGE}
          else
            touch ${USER_ROOT}/que/message/${MESSAGE}
          fi

          if [ $? != 0  ];then
             echo "`date +%Y/%m/%d` `date +%H:%M:%S` ecron_messaged ${MESSAGE} copy from ${SERVER} to ${REMOTE_SERVER} failed" >> ${ERROR_QUE}

             if [ "${MAIL_ADDR}" ];then
               echo "message ${MESSAGE} copy from ${SERVER} to ${REMOTE_SERVER} failed"|mailx -s "WARN ${UNAME} ecron_messaged" ${MAIL_ADDR}
             fi

          fi

          if [ "${LOCAL_CHECK_1}" = "" ];then
            ssh -o "ConnectTimeout 5" -p ${SSH_PORT} ${SERVER} rm ${USER_ROOT}/que/message/${REMOTE_MESSAGE}
            rm ${ROOT}/servers/${SERVER}/que/message/${REMOTE_MESSAGE}
          else
            rm ${USER_ROOT}/que/message/${REMOTE_MESSAGE}
            rm ${ROOT}/servers/${SERVER}/que/message/${REMOTE_MESSAGE}
          fi

        else

          echo "`date +%Y/%m/%d` `date +%H:%M:%S` ecron_messaged: ${REMOTE_SERVER} server.conf not found" >> ${ERROR_QUE}

          if [ "${MAIL_ADDR}" ];then
            echo "message ${MESSAGE} copy from ${SERVER} to ${REMOTE_SERVER} failed because server.conf not found "|mailx -s "WARN ${UNAME} ecron_messaged" ${MAIL_ADDR}
          fi

        fi

     esac

   done

 done

 sleep 60

done
