#! /bin/sh
### BEGIN INIT INFO
# Provides:          revedeviceD
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: revedevice
# Description:       Start Stop revedevice
### END INIT INFO
# Check root user.

BASE=/usr/local/reveantivirus/bin
NAME=revedeviceD
BNAME=revedevice
DAEMON=${BASE}/revedevice
SCRIPTNAME=/etc/init.d/$NAME
pidFPath=/usr/local/reveantivirus/conf/.device.pid
start()
{
	isProc=`pidof $BNAME`
	isProcVal=`expr $isProc + 1`
       if [ $isProcVal -gt 1 ];
       then
		echo "$NAME Service is already running"
       else
	       sleep 1
	       startproc $DAEMON
	       sleep 1
	       echo "$BNAME started"
	fi
			
}
stop()
{
	if [ -f "$pidFPath" ];
	then
	rm $pidFPath
	killproc $DAEMON  > /dev/null
	sleep 2
	fi
	isProc=`pidof $BNAME`
	isProcVal=`expr $isProc + 1`
	if [ $isProcVal -gt 1 ];
	then
	kill -9 $isProc > /dev/null
	sleep 3
	fi

}
restart()
{
	stop
	sleep 5
	start
}
status()
{
	checkproc $DAEMON
}
if [ $(id -u) -ne 0 ]
then
        if [ "$1" != "start" -a "$1" != "stop" -a "$1" != "status" ];
        then
                echo "Usage: {start|stop|restart|status}" >&2
                exit 0
        fi

        if [ "$1" != "status" ]
        then
                echo "You are not root user. Please login as root user.";
                exit 0
        fi
fi
if [ ! -f $DAEMON ]; then
	exit 0
fi
# Setting HOME
[ -f /etc/default/revedefault ] && . /etc/default/revedefault

if [ -z "$GUICONFIG" ] ;  then
  echo "Gui Config is not set, please set it in /etc/default/revedefault" >&2
  exit 1
fi


check_process()
{
    # Check if the process is already running. Ignore grep line.
    result=`ps aux | grep "$BNAME" | grep -v grep | wc -l`
}
# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

PATH="/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/reveantivirus/bin"
 export DISPLAY=:0
 export XAUTHORITY=$GUICONFIG

case "$1" in
start)
     # Check if already runnning; 
     start
     ;;
stop) 
    stop
    ;;
restart)
    restart
    ;;
status)
    status
     ;;
*)
   echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
   exit 1
esac
:
