#!/bin/sh
# ---------------------------------------------- #

CRON_ROOT=/var/spool/cron/root
CRON_ROOT_INST=/home/smxd/installer/tmp/var/spool/cron/root
CRON_SMXD=/var/spool/cron/smxd

##### Function ###################################

##### Main routine ###############################

# 初期処理
. ${INST_HOME}/function.sh
initialize

# root権限チェック
check_root

# 開始出力
CLEAR
msg_info  "${SECUREMATRIX} の CRON 設定変更を行います。" \
          "しばらくお待ちください。"
#echo ""
echo_sp "${SECUREMATRIX} CRON 設定変更中."
write_log "CRON 設定変更開始"

# CRON BACKUP
# /var/spool/cron/root
$tar $U_OPT $BACKUP_PKG_FILE $CRON_ROOT $CRON_ROOT_INST > /dev/null 2>&1
if [ $? -ne 0 ] ; then
  arize_err "dmy" "CRON(root) ファイルのバックアップに失敗しました。"
  exit 1
fi

# /var/spool/cron/smxd
if [ -f "$CRON_SMXD" ] ; then
  $tar $U_OPT $BACKUP_PKG_FILE $CRON_SMXD > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    arize_err "dmy" "CRON(smxd) ファイルのバックアップに失敗しました。"
    exit 1
  fi
fi

# /var/spool/cron/smxd : smx_guardian.sh
if [ -f "$CRON_SMXD" ] ; then
  CLEAR_FILE
  sgTime1="2"
  sgTime2="7"
  sgTimeFor1or3="2,7,12,17,22,27,32,37,42,47,52,57"

  cat /var/spool/cron/smxd | grep "smx_guardian.sh" | grep ^'0 ' > /dev/null 2>&1
  if [ $? -eq 0 ] ; then
    # '0 'で始まるのは 1時間/2時間/.../24時間の設定の場合
    # この場合は分の設定値を、4(auth1) 又は 3(auth2) に書き換える
    cat /var/spool/cron/smxd | grep -v "smx_guardian.sh" > ${TMP_FILE}
    cat /var/spool/cron/smxd | grep 'smx_guardian.sh' | sed -e s/^"0 "/"${sgTime1} "/g >> ${TMP_FILE}
  else
    # 0 で始まらないのは # でコメントアウトか、1分/3分/.../30分の設定の場合
    # この場合は分の設定値を、既存値に応じて書き換える
    # 1分(*) と 3分(0,3,6...) の場合は 5分に書き換える
    cat /var/spool/cron/smxd | grep "smx_guardian.sh" | grep '\* \* \* \* \* ' > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
      # 1分の場合
      cat /var/spool/cron/smxd | grep -v "smx_guardian.sh" > ${TMP_FILE}
      cat /var/spool/cron/smxd | grep 'smx_guardian.sh' | sed -e s/"\* \* \* \* \* "/"${sgTimeFor1or3} \* \* \* \* "/g >> ${TMP_FILE}
    else
      cat /var/spool/cron/smxd | grep "smx_guardian.sh" | grep '0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 \* \* \* \* ' > /dev/null 2>&1
      if [ $? -eq 0 ] ; then
        # 3分の場合
        cat /var/spool/cron/smxd | grep -v "smx_guardian.sh" > ${TMP_FILE}
        cat /var/spool/cron/smxd | grep 'smx_guardian.sh' | sed -e s/"0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 \* \* \* \* "/"${sgTimeFor1or3} \* \* \* \* "/g >> ${TMP_FILE}
      else
        # 5分,10分,20分,30分の場合
        cat /var/spool/cron/smxd | grep -v "smx_guardian.sh" > ${TMP_FILE}
        cat /var/spool/cron/smxd | grep 'smx_guardian.sh' | sed -e s/"0,"/"${sgTime1},"/g \
                                                                -e s/"5,"/"${sgTime2},"/g \
                                                                -e s/"0 "/"${sgTime1} "/g \
                                                                -e s/"5 "/"${sgTime2} "/g >> ${TMP_FILE}
      fi
    fi
  fi

  cat ${TMP_FILE} > /var/spool/cron/smxd
fi

## JAVA 監視ツール対応
$cat /var/spool/cron/root | $grep monitoring_smx_java.sh > /dev/null 2>&1
if [ $? -ne 0 ] ; then
 $echo "20 * * * * /opt/cse/smx/common/bin/monitor/monitoring_smx_java.sh" >> /var/spool/cron/root
fi
$cat /home/smxd/installer/tmp/var/spool/cron/root | $grep monitoring_smx_java.sh > /dev/null 2>&1
if [ $? -ne 0 ] ; then
 $echo "20 * * * * /opt/cse/smx/common/bin/monitor/monitoring_smx_java.sh" >> /home/smxd/installer/tmp/var/spool/cron/root
fi

echo "完了"
echo ""
msg_dialog  "${SECUREMATRIX} の CRON 設定変更に成功しました。" \
            " " \
            "処理を継続します。"
  write_log "${SECUREMATRIX} のCRON 設定変更に成功しました。"
  WAIT_ENTER

# 後始末
CLEAR_FILE
