#!/bin/sh
unset LD_LIBRARY_PATH
#
#  Dr.Web drweb-configd init script
#
#  $Id: 9262f72e6c723f103542e6e4f1d7feb9596763b9 $
#
# PROVIDE: drweb-configd
# REQUIRE: LOGIN
# BEFORE:
# KEYWORD: drweb-configd
# Add the following line to /etc/rc.conf to enable drweb-configd:
#
#  drweb_configd_enable="YES"

if [ "$1" = faststart ]; then
  if [ -r /etc/rc.conf ]; then
    . /etc/rc.conf
    if [ "$drweb_configd_enable" != YES ]; then
      exit 0
    fi
  fi
fi

DAEMON=/usr/local/libexec/drweb.com/bin/drweb-configd
PIDFILE=/var/run/drweb-configd.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

ulimit -n 16384

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() {
    die_if_running
    if test ! -x "$DAEMON" ; then
        echo "Dr.Web drweb-configd is not installed"
        exit $EXIT_FAILURE_NOFILE
    fi

add_execaps=0

    if type execaps >/dev/null 2>&1 && cat /etc/astra_version | grep '^SE 1.5' >/dev/null 2>&1; then
              add_execaps=1
    fi

    if test $add_execaps -eq 1; then
           execaps -c 0x100 -- "$DAEMON" -d -p "$PIDFILE" >/dev/null 2>&1
    else
           "$DAEMON" -d -p "$PIDFILE" >/dev/null 2>&1
    fi
    retval=$?
        if test $retval -ne 0; then
        die_if_cant_start
    fi
    return $retval
}

stop_daemon() {
    check_if_not_running
    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
    test $retval -eq 0 && rm -f "$PIDFILE" || die_if_timeout
    return $retval
}

reload_daemon() {
    check_if_not_running
    pid=`get_pid`
    if test -n "$pid" ; then
        kill -HUP "$pid"
    fi
}

restart_daemon() {
    if check_pid ; then
        stop_daemon || die_if_timeout
    fi
    start_daemon || die_if_cant_start
}

condrestart_daemon() {
    check_if_not_running
    stop_daemon || die_if_timeout
    start_daemon || die_if_cant_start
}

daemon_status() {
    check_pid
    case "$?" in
        $STATUS_ALIVE) echo "Dr.Web drweb-configd is running" ;;
        $STATUS_NOPID) echo "Dr.Web drweb-configd is not running" ;;
        $STATUS_NOT_ALIVE|$STATUS_WRONG_PID) echo "Dr.Web drweb-configd is not running but $PIDFILE exists" ;;
    esac
}

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

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

check_if_not_running() {
    check_pid
    case "$?" in
        $STATUS_NOPID) echo "Dr.Web drweb-configd is not running"
                       exit $EXIT_SUCCESS
                       ;;
        $STATUS_NOT_ALIVE|$STATUS_WRONG_PID) die $EXIT_FAILURE_NOT_RUNNING \
            "Dr.Web drweb-configd is not running but $PIDFILE exists" ;;
    esac
}

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

die_if_cant_start() {
    die $EXIT_FAILURE_NOT_RUNNING "Failed to start Dr.Web drweb-configd"
}


case "$1" in
    stop)
        echo "Shutting down Dr.Web drweb-configd..."
        stop_daemon
    ;;
    reload)
        echo "Reloading Dr.Web drweb-configd..."
        reload_daemon
    ;;
    restart)
        echo "Restarting Dr.Web drweb-configd..."
        restart_daemon
    ;;
    condrestart)
        echo "Restarting Dr.Web drweb-configd..."
        condrestart_daemon
    ;;
    quietstart|faststart|start)
        echo "Starting Dr.Web drweb-configd..."
        start_daemon
    ;;
    status)
        daemon_status
    ;;
    *)
        echo "Usage: $0 {quietstart|faststart|start|stop|restart|condrestart|reload|status}"
        exit $EXIT_FAILURE_NOARGS
    ;;
esac

exit $EXIT_SUCCESS
