#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2015, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
# 


source `dirname $0`/zmshutil || exit 1
zmsetvars

configfile=/opt/zimbra/conf/logswatchrc
pidfile=${zimbra_log_directory}/logswatch.pid
zmrrdfetchpidfile=${zimbra_log_directory}/zmrrdfetch-server.pid
zmrrdfetchpid=""
logfile=${zimbra_log_directory}/zmlogswatch.out
pid=""
os=$(uname -s)

getpid()
{
  if [ -f ${pidfile} ]; then
    pid=$(cat ${pidfile})
  fi
  if [ -f ${zmrrdfetchpidfile} ]; then
    zmrrdfetchpid=$(cat ${zmrrdfetchpidfile})
  fi
}

checkrunning()
{
  getpid
  if [ "x$pid" = "x" ]; then
    running=0
  else
    if ps --no-headers -p $pid -o cmd 2>/dev/null |grep swatchdog >/dev/null 2>&1; then
      running=1
    else
      pid=""
      running=0
    fi
  fi
}

case "$1" in 
  start)
    if [ ! -f ${configfile} ]; then
      echo "${configfile} is missing."
      exit 0
    fi

    checkrunning
    echo -n "Starting logswatch..."
    if [ $running = 1 ]; then
      echo "logswatch is already running."
      exit 0
    fi

    /opt/zimbra/bin/zmprov -l gs `/opt/zimbra/bin/zmhostname` zimbraServiceEnabled | grep -qw logger
    if [ $? = 1 ]; then
        echo "zimbra logger service is not enabled!  failed."
        exit 1
    fi

    if [ ! -d ${zimbra_tmp_directory} ]; then
      mkdir -p ${zimbra_tmp_directory} > /dev/null 2>&1
    fi

    /opt/zimbra/common/bin/swatchdog --config-file=${configfile} \
      --use-cpan-file-tail --pid-file=${pidfile} --daemon \
      --script-dir=${zimbra_tmp_directory} \
      --tail-file /var/log/zimbra-stats.log > $logfile 2>&1
    for ((i=0; i < 30; i++)); do
      checkrunning
      if [ $running = 1 ]; then
        break
      fi
      sleep 1
    done
    if [ "x$pid" != "x" ]; then
      echo "done."
      exit 0
    else
      echo "failed."
      exit 1
    fi
  ;;
  stop)
    checkrunning
    echo -n "Stopping logswatch..."
    if [ $running = 0 ]; then
      echo "logswatch is not running."
      exit 0
    else
      for ((i = 0; i < 30; i++)); do
          if [ -z "$zmrrdfetchpid" ]; then
              break;
          fi
          if ! ps --no-headers -p $zmrrdfetchpid -o cmd 2>/dev/null |grep zmrrdfetch >/dev/null 2>&1; then
              rm -f ${zmrrdfetchpidfile}
              break;
          fi
          kill $zmrrdfetchpid 2> /dev/null
          sleep 1
      done
      if [ -n "$zmrrdfetchpid" ]; then
          if ps --no-headers -p $zmrrdfetchpid -o cmd 2>/dev/null |grep zmrrdfetch >/dev/null 2>&1; then
              kill -9 $zmrrdfetchpid
          fi
      fi
      for ((i = 0; i < 30; i++)); do
        if ! ps --no-headers -p $pid -o cmd 2>/dev/null |grep swatchdog >/dev/null 2>&1; then
          rm -rf ${pidfile}
          break
        fi
        if [ x"$os" = "xLinux" ]; then
          index=0;quit=0
          ids[$index]="$pid"
          while [ $quit -eq 0 ]; do
            ((index++))
            ids[$index]=$(ps -o pid --ppid ${ids[$index-1]} | grep -v PID | tr \\n ' ')
            if [ ! "${ids[$index]}" ]; then
              ((quit++))
            fi
          done
          for i in $(seq 0 ${#ids[@]}); do
            if [ "${ids[$i]}" ]; then
              kill -9 ${ids[$i]} 2> /dev/null
            fi
          done
        else
          kill -9 $pid
        fi 
        sleep 1
      done
    fi
    if [ -s ${pidfile} ]; then
      echo "failed."
      exit 1
    else
      echo "done."
    fi
    exit 0
  ;;
  restart|reload)
    $0 stop
    $0 start
  ;;
  status)
    echo -n "zmlogswatch is "
    checkrunning
    if [ $running = 0 ]; then
      echo "not running."
      exit 1
    else
      echo "running."
      exit 0
    fi
  ;;
  *)
    echo "$0 start|stop|restart|reload|status"
    exit 1
  ;;
esac
