#! /bin/sh
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#               Clamav version by Magnus Ekdahl <magnus@debian.org>
#               Heavily reworked by Stephen Gran <sgran@debian.org>
#
### BEGIN INIT INFO
# Provides:          clamav-daemon
# Required-Start:    $syslog
# Should-Start:      
# Required-Stop:
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 6
# Short-Description: ClamAV daemon
# Description:       Clam AntiVirus userspace daemon
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/clamd
NAME="clamd"
DESC="ClamAV daemon"
CLAMAVCONF=/etc/clamav/clamd.conf
SUPERVISOR=/usr/bin/daemon
SUPERVISORNAME=daemon
SUPERVISORPIDFILE="/var/run/clamav/daemon-clamd.pid"
SUPERVISORARGS="--name=$NAME --respawn $DAEMON -F $SUPERVISORPIDFILE"

[ -x "$DAEMON" ] || exit 0
[ -r /etc/default/clamav-daemon ] && . /etc/default/clamav-daemon
. /lib/lsb/init-functions

if [ ! -f "$CLAMAVCONF" ]; then
  log_failure_msg "There is no configuration file for Clamav."
  log_failure_msg "Please either dpkg-reconfigure $DESC, or copy the example from"
  log_failure_msg "/usr/share/doc/clamav-base/examples/ to $CLAMAVCONF and run"
  log_failure_msg "'/etc/init.d/clamav-daemon start'"
  exit 1;
fi

if grep -q "^Example" $CLAMAVCONF; then
  log_failure_msg "Clamav is not configured."
  log_failure_msg "Please edit $CLAMAVCONF and run  '/etc/init.d/clamav-daemon start'"
  exit 0
fi

if egrep -qi "^Foreground[[:space:]]*(yes|true|1)" $CLAMAVCONF; then
  if [ ! -x "$SUPERVISOR" ] ; then
     log_failure_msg "Foreground specified, but $SUPERVISORNAME not found"
     exit 0
  else
     RUN_SUPERVISED=1
  fi
fi

THEPIDFILE="`grep ^PidFile $CLAMAVCONF | awk '{print $2}'`"
[ -n "$THEPIDFILE" ] || THEPIDFILE='/var/run/clamav/clamd.pid'

if [ -z "$RUN_SUPERVISED" ]; then
  if [ -f "$THEPIDFILE" ]; then
    PID=`pidofproc -p $THEPIDFILE $DAEMON`
    RUNNING=$?
  else
    PID=`pidofproc $DAEMON`
    RUNNING=$?
  fi
else
  [ -e "$SUPERVISORPIDFILE" ] && PID=`cat $SUPERVISORPIDFILE`
fi

[ "$PID" = '1' ] && unset PID

case "$1" in
  start)
  OPTIND=1
  if [ -z "$RUN_SUPERVISED" ] ; then
    log_daemon_msg "Starting $DESC" "$NAME "
    start_daemon -p $THEPIDFILE $DAEMON
    ret=$?
  else 
    log_daemon_msg "Starting $DESC" "$NAME (supervised) "
    $SUPERVISOR $SUPERVISORARGS
    ret=$?
  fi
  log_end_msg $ret
  ;;
  stop)
  log_daemon_msg "Stopping $DESC" "$NAME"
  OPTIND=1
  if [ -n "$PID" ]; then
    kill -15 -"$PID"
    ret=$?
    sleep 1
    if kill -0 "$PID" 2>/dev/null; then
      ret=$?
      log_progress_msg "Waiting . "
      cnt=0
      while kill -0 "$PID" 2>/dev/null; do
        ret=$?
        cnt=`expr "$cnt" + 1`
        if [ "$cnt" -gt 15 ]; then
          kill -9 -"$PID"
          break
        fi
        sleep 2
        log_progress_msg ". "
      done
    fi
  else
    if [ -z "$RUN_SUPERVISED" ] ; then
      killproc -p $THEPIDFILE
      ret=$?
    else
      killproc -p $SUPERVISORPIDFILE
      ret=$?
    fi
  fi
  if [ -n "$ret" ]; then
    log_end_msg $ret
  else
    log_end_msg $?
  fi
  ;;
  status)
  case "$RUNNING" in
    0) log_success_msg "$NAME is running."
    ;;
    1) log_warning_msg "$NAME is not running, but pidfile $THEPIDIFILE exists."
    ;;
    3) log_failure_msg "$NAME is not running."
    ;;
    *) log_failure_msg "$NAME is unknown."
    ;;
  esac
  ;;
  restart|force-reload)
  $0 stop
  $0 start
  ;;
  reload-database)
  OPTIND=1
  log_daemon_msg "Reloading database for $DESC" "$NAME"
  if [ "$RUNNING" = 0 ] && [ -n "$PID" ]; then
    kill -USR2 $PID
  fi
  log_end_msg $?
  ;;
  reload-log)
  OPTIND=1
  log_daemon_msg "Reloading log file for $DESC" "$NAME"
  if [ "$RUNNING" = 0 ] && [ -n "$PID" ]; then
    kill -HUP $PID
  fi
  log_end_msg $?
  ;;
  *)
  log_failure_msg "Usage: $0 {start|stop|restart|force-reload|reload-log|reload-database|status}" >&2
  exit 1
  ;;
esac

exit 0
