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

BASE=/usr/local/reveantivirus/bin
NAME=revescheduleD
BNAME=reveschedule
DAEMON=${BASE}/reveschedule
SCRIPTNAME=/etc/init.d/$NAME
pidFPath=/usr/local/reveantivirus/conf/.scheduler.pid
# Cheeck for root user
start()
{
	isProc=`pidof $BNAME`
	isProcVal=`expr $isProc + 1`
	if [ $isProcVal -gt 1 ];
	then
		echo "service is already running"
	else
		sleep 1
		start-stop-daemon --exec $DAEMON --start
		sleep 1
		echo $BNAME started
	fi
}
stop()
{
	if [ -f "$pidFPath" ];
	then
	rm $pidFPath
	start-stop-daemon --exec $DAEMON --stop > /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()
{
	isProc=`pidof $BNAME`
	isProcVal=`expr $isProc + 1`
	if [ $isProcVal -gt 1 ];
	then
		echo "$NAME service is running"
	else
		echo "$NAME service is stopped"
	fi

}
check_process()
{
    # Check if the process is already running. Ignore grep line.
    result=`pidof $BNAME`
}
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
	fi
fi																	

if [ ! -f $DAEMON ]; then
exit 0
fi
# Setting XAUTHORITY
																		[ -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
# 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:/lib/reveAM"
 export DISPLAY=:0
 export XAUTHORITY=$GUICONFIG

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