#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2011, 2012, 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 *****
# 

if [ x`whoami` != xzimbra ]; then
  echo Error: must be run as zimbra user
  exit 1
fi

if [ ! -x "/opt/zimbra/common/bin/freshclam" ]; then
  exit 0
fi

platform=`/opt/zimbra/libexec/get_plat_tag.sh`

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

fpidfile=${zimbra_log_directory}/freshclam.pid

rewriteconfig() {
  /opt/zimbra/libexec/configrewrite antivirus > /dev/null 2>&1
}

getpid() {
  if [ -f $fpidfile ]; then
    fpid=`cat $fpidfile`
  fi
}

checkrunning() {
  getpid
  # freshclam
  if [ "x$fpid" = "x" ]; then
    frunning=0
  else
    if ps --no-headers -p $fpid -o cmd 2>/dev/null |grep freshclam >/dev/null 2>&1; then
      frunning=1
    else
      rm $fpidfile
      fpid=""
      frunning=0
    fi
  fi
}

pskillall() {
  ps_cmd="/bin/ps ax -o pid,ppid,command"
  killsig="$1"
  pname=`echo "$2" | sed -e 's%/%\\\/%g'`
  plist=`${ps_cmd} | awk '{ if ( $3 ~ /'${pname}'/ ) { print $1 " " $2 } }' | sort -nr -k2 -k1 | awk '{ print $1 }'`
  for p in ${plist}
  do
    kill ${killsig} ${p}
  done
}


#
# Main
#
case "$1" in
  'start')

    if [ x$2 = "x" ]; then
      rewriteconfig
    fi

    checkrunning
    echo -n "Starting freshclam..."
    if [ $frunning = 1 ]; then
      echo "freshclam is already running.";
    else
      /opt/zimbra/common/bin/freshclam \
        --config-file=/opt/zimbra/conf/freshclam.conf --quiet --daemon --checks=12 \
          >> ${zimbra_log_directory}/freshclam.log 2>&1 &
      echo "done."
    fi
    exit 0
   ;;

  'kill')
    if [ -f /opt/zimbra/log/freshclam.pid ]; then
      cpid=`cat /opt/zimbra/log/freshclam.pid`
      kill -9 "$cpid" 2> /dev/null
    fi
    pskillall -9 /opt/zimbra/common/bin/freshclam
    exit 0
   ;;

  'stop')
    checkrunning
    echo -n "Stopping freshclam..."
    if [ $frunning = 0 ]; then
      echo "freshclam is not running.";
    else 
      if [ x"$fpid" != "x" ]; then  
        kill -9 "$fpid" 2> /dev/null
        if [ $? = 0 ]; then
          echo "done."
        else 
          echo "failed."
        fi
      fi
    fi

    exit 0
  ;;
  
  'restart'|'reload')
    $0 stop
    $0 start $2
  ;;
  
  'status')
    checkrunning
    echo -n "freshclam is "
    if [ $frunning = 1 ]; then
      echo "running."
      exit 0
    else
      echo "not runnning."
      exit 1
    fi
  ;;
  
  *)
    echo "Usage: $0 start|stop|kill|restart|status"
    exit 1
  ;;
esac
