#!/bin/sh
#
#  Dr.Web drwebd init script
#
#  $Id: 4f28f79ede3baa352db7e7ab479ae6bf294ace4e $
#
# chkconfig: 235 20 80
# description: drwebd is a Dr.Web Daemon
# processname: drwebd
# config: /etc/drweb/drweb32.ini
# pidfile: /var/drweb/run/drwebd.pid
### BEGIN INIT INFO
# Provides: drwebd
# Required-Start: $local_fs $network
# Required-Stop: $null
# Should-Start: $null
# Should-Stop: $null
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Start-Before: keriomailserver
# Description: drwebd is a Dr.Web Daemon
### END INIT INFO

DAEMON=/opt/drweb/drwebd
PIDFILE=/var/drweb/run/drwebd.pid
TIMEOUT=300

PATH=/usr/xpg4/bin:/bin:/usr/bin:/usr/ucb:/sbin:/usr/sbin:${PATH}

EXIT_SUCCESS=0
EXIT_FAILURE_NOFILE=1
EXIT_FAILURE_RUNNING=2
EXIT_FAILURE_NOT_RUNNING=3
EXIT_FAILURE_TIMEOUT=4
EXIT_FAILURE_NOARGS=5
EXIT_FAILURE_NOT_ROOT=6

STATUS_NOPID=1
STATUS_ALIVE=0
STATUS_NOT_ALIVE=2
STATUS_WRONG_PID=3

if test -n "$1" -a ! "$1" = "status" ; then
    case "`id`" in
        uid=0*)
        ;;
        *)
            echo "$0 $1 must be executed with root privileges"
            exit $EXIT_FAILURE_NOT_ROOT
        ;;
    esac
fi

check_run() {
    runfiles="/etc/drweb/drwebd.enable /etc/default/drwebd /etc/sysconfig/drwebd /etc/drweb/daemons.run"
    enabled=""
    found=""
    for runfile in $runfiles ; do
        if test -f "$runfile" ; then
            found=1
            . "$runfile"
            if test "$RUN_DRWEBD" = "1" -o "$ENABLE" = "1" ; then
                enabled=1
                break
            else
                echo "Dr.Web drwebd is disabled according to $runfile"
            fi
        fi
    done
    if test -z "$found" ; then
        echo "Didn't found a enable file for Dr.Web drwebd. See documentation to solve this problem"
        exit $EXIT_FAILURE_NOFILE
    fi
    test -z "$enabled" && exit $EXIT_SUCCESS
}

get_pid() {
    head -1 "$PIDFILE" 2>/dev/null
}

check_pid() {
    if test -r "$PIDFILE" ; then
        pid=`get_pid`
        if test -n "$pid" ; then
            if kill -0 "$pid" 2>/dev/null || ps -p "$pid" >/dev/null 2>&1 ; then
                return $STATUS_ALIVE
            else
                return $STATUS_NOT_ALIVE
            fi
        else
            return $STATUS_WRONG_PID
        fi
    else
        return $STATUS_NOPID
    fi
}

start_daemon() {
    if test ! -x "$DAEMON" ; then
        echo "Dr.Web drwebd is not installed"
        exit $EXIT_FAILURE_NOFILE
    fi

    
    
    "$DAEMON" "$@"
    return $?
}

stop_daemon() {
    pid=`get_pid`
    if test -n "$pid" ; then
        kill "$pid"
    fi
    seconds=0
    retval=0
    while check_pid ; do
        sleep 1
        printf "."
        seconds=`expr $seconds + 1`
        if test "$seconds" -gt "$TIMEOUT" ; then
            retval=1
            break
        fi
    done
    test "$seconds" -gt "0" && echo
    return $retval
}

die() {
    echo "$2" && exit $1
}

die_if_running() {
    check_pid && die $EXIT_FAILURE_RUNNING "Dr.Web drwebd is already running"
}

die_if_not_running() {
    check_pid
    case "$?" in
        $STATUS_NOPID) die $EXIT_FAILURE_NOT_RUNNING "Dr.Web drwebd is not running" ;;
        $STATUS_NOT_ALIVE|$STATUS_WRONG_PID) die $EXIT_FAILURE_NOT_RUNNING \
            "Dr.Web drwebd is not running but $PIDFILE exists" ;;
    esac
}

die_if_timeout() {
    die $EXIT_FAILURE_RUNNING "Dr.Web drwebd seems is still running"
}

case "$1" in
    stop)
        die_if_not_running
        echo "Shutting down Dr.Web drwebd..."
        stop_daemon || die_if_timeout
    ;;
    reload)
        die_if_not_running
        echo "Reloading Dr.Web drwebd..."
        pid=`get_pid`
        if test -n "$pid" ; then
            kill -HUP "$pid"
        fi
    ;;
    restart)
        echo "Restarting Dr.Web drwebd..."
        if check_pid ; then
            stop_daemon || die_if_timeout
        fi
        start_daemon
    ;;
    condrestart)
        die_if_not_running
        echo "Restarting Dr.Web drwebd..."
        stop_daemon || die_if_timeout
        start_daemon
    ;;
    start)
        die_if_running
        echo "Starting Dr.Web drwebd..."
        start_daemon
    ;;
    status)
        check_pid
        case "$?" in
            $STATUS_ALIVE) echo "Dr.Web drwebd is running" ;;
            $STATUS_NOPID) echo "Dr.Web drwebd is not running" ;;
            $STATUS_NOT_ALIVE|$STATUS_WRONG_PID) echo "Dr.Web drwebd is not running but $PIDFILE exists" ;;
        esac
    ;;
    *)
        echo "Usage: $0 {start|stop|restart|condrestart|reload|status}"
        exit $EXIT_FAILURE_NOARGS
    ;;
esac

exit $EXIT_SUCCESS
