#!/bin/sh
#
#  Dr.Web (R) init script
#  $Revision: 1.10.14.7 $
#

# chkconfig: 2345 20 80
# description: drwebd is a Dr.Web (R) daemon
# processname: drwebd
# config: /etc/drweb/drweb32.ini
# pidfile: /var/drweb/run/drwebd.pid
BIN="drwebd"
PROGRAM="/opt/drweb/$BIN"
PIDFILE="/var/drweb/run/drwebd.pid"

RUNFILE=/etc/drweb/daemons.run
if test -f $RUNFILE ; then
    . $RUNFILE
    if test "$RUN_DRWEBD" != "1" ; then
        echo "DrWeb Daemon is disabled according to $RUNFILE"
	exit 0
    fi
fi

check_pidfile()
{
    pid=`head -1 $PIDFILE` || { echo "cannot read PID file"; exit 1; }
    [ "$pid" != "" ] && [ "$pid" -lt 100000 ] || { echo "invalid PID file for $PIDFILE"; exit 1; }
}


case "$1" in
    stop)
	echo "Shutting down Dr. Web daemon..."
    if [ -f $PIDFILE ] ; then
	check_pidfile;
    	kill `head -1 $PIDFILE`    
    fi
    ;;
    reload)
	echo "Reloading Dr. Web daemon..."
    if [ -f $PIDFILE ] ; then
	check_pidfile;
    	kill -HUP `head -1 $PIDFILE`    
    fi
    ;;
    restart)
	echo "Restarting Dr. Web daemon..."
    if [ -f $PIDFILE ] ; then
	check_pidfile;
    	kill `head -1 $PIDFILE`    
    fi
	sleep 3
	$PROGRAM
    ;;
    start)
	echo "Starting Dr. Web daemon..."
	$PROGRAM
    ;;
    status)
        if test -f "$PIDFILE"
        then
            echo "Dr.Web daemon started"
            exit 0
        else
            echo "Dr.Web daemon not started"
            exit 1
        fi
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|reload|status}"
    ;;
esac
 