#!/bin/bash
#
# This script managed the cbramd service
#
# start cybereason av at run level 3 (networking is up), with start priority 0 (start before anything else) and stop priority 100 (stop last)
# chkconfig: 3456 1 99
# description: 
  
# Return values according to LSB for all commands but status:
# 0 - success
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - ccbram is not installed


cbram_binary="/opt/cybereason/sensor/bin/cbram"
run_flags="--daemonize"
lock_file="/var/lock/subsys/cbramd"


# Source function library.
. /etc/init.d/functions

# Allow anyone to run status
if [ "$1" = "status" ]
then
    status $cbram_binary
    RETVAL=$?
    exit $RETVAL
fi

# Check that we are root ... so non-root users stop here
test $EUID = 0  ||  exit 4

RETVAL=0

start()
{
    if [ -f "$lock_file" ]
    then
        echo "service cbramd is already running!"
        return $RETVAL
    fi

    test -x $cbram_binary || exit 5

    echo -n $"Starting $cbram_binary: "

    daemon $cbram_binary $run_flags
    RETVAL=$?
    echo
    if test $RETVAL = 0 
    then
        touch $lock_file
    fi
    return $RETVAL
}

stop()
{
    if [ ! -f "$lock_file" ]
    then
        echo "service cbramd is not running"
        return $RETVAL
    fi

    echo -n $"Stopping $cbram_binary: "
    killproc $cbram_binary
    RETVAL=$?
    echo

    if test $RETVAL = 0
    then
        rm -f $lock_file
    else
        echo "failed to stop service cbram!"
    fi

    return $RETVAL
}

restart()
{
    stop
    start
}


# See how we were called.
case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart}"
    RETVAL=3
esac

exit $RETVAL
