#!/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

UTL_SCHEDULE=${ROOT}/utilities/.schedule
DAEMON_ENT=${ROOT}/utilities/ecron_daemon_check.sh
CONSOLE_LOG_ENT=${ROOT}/utilities/log_delete.sh
CONSOLE_QUE_ENT=${ROOT}/utilities/que_delete.sh


FLAG=$1


## daemon check
if [ "${FLAG}" = "-pair" ];then
  echo "check cluster pair"
else
  MON_PID=`ps -ef | grep ecron_monitord | grep -v grep | awk '{print $2}'`
  if [ "${MON_PID}" = "" ];then
    if [ -f /tmp/ecron_monitord_lock ];then
      rm /tmp/ecron_monitord_lock 
    fi
    ${ROOT}/sbin/ecron_monitord &
    sleep 1
  fi

  MON_PID=`ps -ef | grep ecron_monitord | grep -v grep | awk '{print $2}'`
  if [ "${MON_PID}" = "" ];then
    echo "can't start ecron_monitord !!"
    exit 1
  fi

  MIR_PID=`ps -ef | grep ecron_mirrord | grep -v grep | awk '{print $2}'`
  if [ "${MIR_PID}" = "" ];then
    if [ -f /tmp/ecron_mirrord_lock ];then
      rm /tmp/ecron_mirrord_lock
    fi
    ${ROOT}/sbin/ecron_mirrord &
    sleep 1
  fi

  MIR_PID=`ps -ef | grep ecron_mirrord | grep -v grep | awk '{print $2}'`
  if [ "${MIR_PID}" = "" ];then
    echo "can't start ecron_mirrord !!"
    exit 1
  fi

  MES_PID=`ps -ef | grep ecron_messaged | grep -v grep | awk '{print $2}'`
  if [ "${MES_PID}" = "" ];then
    if [ -f /tmp/ecron_messaged_lock ];then
      rm /tmp/ecron_messaged_lock
    fi
    ${ROOT}/sbin/ecron_messaged &
    sleep 1
  fi

  MES_PID=`ps -ef | grep ecron_messaged | grep -v grep | awk '{print $2}'`
  if [ "${MES_PID}" = "" ];then
    echo "can't start ecron_messaged !!"
    exit 1
  fi

fi

## create cron entry

crontab -l > ${ROOT}/bin/.crontab

DAEMON_UTILITY_CHECK=`grep "${DAEMON_ENT}" ${ROOT}/bin/.crontab`

if [ "${DAEMON_UTILITY_CHECK}" = "" ];then
  echo "`grep DAEMON_CHECK ${UTL_SCHEDULE}| awk -F [:] '{print $2}'` ${DAEMON_ENT} > /dev/null 2>&1" >> ${ROOT}/bin/.crontab
  crontab ${ROOT}/bin/.crontab

  if [ $? != 0 ];then
    echo "your schedule entry is not accurate."
    echo "please check schedule and retry"
    echo "${UTL_SCHEDULE}"
  fi

fi

CONSOLE_LOG_CHECK=`grep "${CONSOLE_LOG_ENT}" ${ROOT}/bin/.crontab`

if [ "${CONSOLE_LOG_CHECK}" = "" ];then
  echo "`grep CONAOLE_LOG_SCHEDULE ${UTL_SCHEDULE}| awk -F [:] '{print $2}'` ${CONSOLE_LOG_ENT} > /dev/null 2>&1" >> ${ROOT}/bin/.crontab
  crontab ${ROOT}/bin/.crontab

  if [ $? != 0 ];then
    echo "your schedule entry is not accurate."
    echo "please check schedule and retry"
    echo "${UTL_SCHEDULE}"
  fi

fi

CONSOLE_QUE_CHECK=`grep "${CONSOLE_QUE_ENT}" ${ROOT}/bin/.crontab`

if [ "${CONSOLE_QUE_CHECK}" = "" ];then
  echo "`grep CONAOLE_QUE_SCHEDULE ${UTL_SCHEDULE}| awk -F [:] '{print $2}'` ${CONSOLE_QUE_ENT} > /dev/null 2>&1" >> ${ROOT}/bin/.crontab
  crontab ${ROOT}/bin/.crontab

  if [ $? != 0 ];then
    echo "your schedule entry is not accurate."
    echo "please check schedule and retry"
    echo "${UTL_SCHEDULE}"
  fi

fi

if [ -f  ${ROOT}/bin/.crontab ];then 
  rm  ${ROOT}/bin/.crontab
fi

exit 0
