#!/bin/sh
### BEGIN INIT INFO
# Provides:          clamav-milter
# Required-Start:    $syslog
# Should-Start:      clamav-daemon
# Required-Stop:
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 6
# Short-Description: ClamAV virus milter
# Description:       Clam AntiVirus milter interface
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/clamav-milter
NAME=clamav-milter
DESC="Sendmail milter plugin for ClamAV"
CLAMAVMILTERCONF=/etc/clamav/clamav-milter.conf
DEFAULT=/etc/default/clamav-milter
SUPERVISOR=/usr/bin/daemon
SUPERVISORNAME=daemon
SUPERVISORPIDFILE="/var/run/clamav/daemon-clamav-milter.pid"
SUPERVISORARGS="--name=$NAME --respawn $DAEMON -F $SUPERVISORPIDFILE"

[ -x "$DAEMON" ] || exit 0
. /lib/lsb/init-functions

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

#COMMON-FUNCTIONS#

slurp_config "$CLAMAVMILTERCONF"

[ ! -r $DEFAULT ] || . $DEFAULT
[ -n "$PidFile" ] || PidFile=/var/run/clamav/clamav-milter.pid
[ -n "$MilterSocket" ] || MilterSocket=local:/var/run/clamav/clamav-milter.ctl

if [ -n "$Example" ]; then
  log_failure_msg "$DESC is not configured."
  log_failure_msg "Please edit $CLAMAVMILTERCONF and run '/etc/init.d/clamav-milter start'"
  exit 0
fi

if is_true "$Foreground"; then
  if [ ! -x "$SUPERVISOR" ] ; then
     log_failure_msg "Foreground specified, but $SUPERVISORNAME not found"
     exit 0
  else
     RUN_SUPERVISED=1
     make_dir $(dirname "$SUPERVISORPIDFILE")
     chown $User $(dirname "$SUPERVISORPIDFILE")
  fi
fi

case "$MilterSocket" in
  /*)
  SOCKET_PATH="$MilterSocket"
  ;;
  *)
  # If the socket is type inet: we don't care - we can't rm -f that later :)
  SOCKET_PATH=`echo $MilterSocket | sed -e s/(unix|local)\://`
  ;;
esac

[ -n "$User" ] || User=clamav

THEPIDFILE=$PidFile
[ -n "$THEPIDFILE" ] || THEPIDFILE='/var/run/clamav/clamav-milter.pid'
make_dir $(dirname "$THEPIDFILE")
chown $User $(dirname "$THEPIDFILE")

if [ -f "$THEPIDFILE" ]; then
  CLAMMILTERPID=`pidofproc -p $THEPIDFILE $DAEMON`
  RUNNING=$?
else
  CLAMMILTERPID=`pidofproc $DAEMON`
  RUNNING=$?
fi

if [ -z "$RUN_SUPERVISED" ]; then
  PID="$CLAMMILTERPID"
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 "
    su "$User" -p -s /bin/sh -c ". /lib/lsb/init-functions && start_daemon -p $THEPIDFILE $DAEMON"
    ret=$?
  else 
    log_daemon_msg "Starting $DESC" "$NAME (supervised) "
    $SUPERVISOR $SUPERVISORARGS
    ret=$?
  fi
  if [ $ret = 0 ] && [ "$USE_POSTFIX" = 'yes' ] && [ "${SOCKET_PATH#inet}" = "${SOCKET_PATH}" ]; then
    cnt=0
    until [ -e "$SOCKET_PATH" ] ; do
      cnt=`expr "$cnt" + 1`
      if [ "$cnt" -gt 15 ]; then
        break
      fi
      sleep 2
    done
    if [ -e "$SOCKET_PATH" ]; then
      chmod g+w $SOCKET_PATH
      chgrp postfix $SOCKET_PATH
    else
      log_warning_msg "Socket not created.  Investigate"
    fi
  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)
  status_of_proc "$DAEMON" "$NAME"
  exit $?
  ;;
  restart|force-reload)
  $0 stop
  $0 start
  ;;
  *)
  log_failure_msg "Usage: $0 {start|stop|restart|force-reload|status}" >&2
  exit 1
  ;;
esac

exit 0
