#!/bin/sh
#
# $Id: template.initscript,v 1.15 2004/05/21 16:02:45 asv Exp $
#
# description: Dr.Web for Sendmail is an antivirus filter 
#              for e-mails that comes through SMTP protocol
#              
# chkconfig: 2345 79 31
# processname: drweb-smf

FILTER="___T_FILTER_PATH___"

# Use these functions if standard functions cannot be found 
dw_daemon() {
    ulimit -c 0
    $*
    return $?
}

dw_killproc() {
    killall $2 $1 >/dev/null 2>&1
    return $?
}

dw_status() {
    killall -0 $1 >/dev/null 2>&1
    if [ $? -eq 0 ] ; then
        echo "$1 is running..."        
        return 0
    else
        echo "$1 is not running."
        return 1
    fi
}

if [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
    PROC_DAEMON="daemon"
    PROC_KILL="killproc"
    PROC_STATUS="status"
    MSG_START="Start Dr.Web for Sendmail: "
    MSG_RELOAD="Reload Dr.Web for Sendmail: "
    MSG_STOP="Stop Dr.Web for Sendmail: "
    MSG_WAIT="Waiting for Dr.Web for Sendmail."
else
    PROC_DAEMON="dw_daemon"
    PROC_KILL="dw_killproc"
    PROC_STATUS="dw_status"
    MSG_START=" drweb-smf(start)"
    MSG_RELOAD="drweb-smf(reload)"
    MSG_STOP=" drweb-smf(stop)"
    MSG_WAIT=" drweb-smf(wait)"
fi     

if [ ! -f "$FILTER" ] ; then
    echo "Dr.Web Sendmail Filter not found"
    return 1
fi    

RETVAL=0
FILTER_UNIX=___T_FILTER_UNIX___
FILTER_SOCKET=___T_FILTER_SOCKET___

start() {
	# Start daemons.
	echo -n $MSG_START
    [ "x$FILTER_UNIX" = "xyes" ] && rm -f $FILTER_SOCKET
    $PROC_DAEMON "$FILTER" ___T_FILTER_PARAMS___
    RETVAL=$?
	echo
	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n $MSG_STOP
    $PROC_KILL drweb-smf
	RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        if [ "x$FILTER_UNIX" = "xyes" ] ; then
            RETRY=3
            while [ -f $FILTER_SOCKET -a $RETRY -gt 0 ] ; do
            	if [ $RETRY -lt 3 ] ; then
                    echo 
            	    echo $MSG_WAIT
            	fi
                sleep 1
            	RETRY=$(($RETRY-1))
            done
            if [ -f $FILTER_SOCKET ] ; then
                RETVAL=70
            fi
        else
            # Cannot check inet socket
            # For future: use pidfile
            sleep 3
        fi
    fi
	echo
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	RETVAL=$?
	;;
  stop)
	stop
	RETVAL=$?
	;;
  restart)
	stop    
    sleep 1
	start
	RETVAL=$?
	;;
  status)
    $PROC_STATUS drweb-smf
	RETVAL=$?
	;;
  *)
	echo "Usage: $0 { start | stop | restart | status }"
	exit 1
esac

exit $RETVAL
