#!/bin/sh

##############################################################################
#                                                                            #
# Copyright (c) 2015 by R1Soft All rights reserved                           #
#                                                                            #
##############################################################################
#
# chkconfig: 2345 99 99
# description: R1Soft Agent Update Service
# processname: lsbaus

### BEGIN INIT INFO
# Provides:          lsbaus
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts R1Soft Linux Server Backup Agent Update Service
# Description:       Starts R1Soft Linux Server Backup Agent Update Service
# Options:           start), stop), restart), status)
### END INIT INFO
#!/bin/sh

. /lib/lsb/init-functions

NAME=lsbaus
DAEMON=/opt/r1soft/lsbaus/bin/lsbaus

if [ ! -f $DAEMON ]; then
	echo "Deamon executable does not exist"
	exit 1
fi


start() {
	pidofproc $DAEMON > /dev/null 2>&1
	if [ "$?" -eq "0" ]; then
		log_success_msg "$NAME with pid $XPID is already running"
		exit
	fi

	nohup $DAEMON > /dev/null 2>&1 &
	if [ "$?" -ne "0" ]; then
		log_failure_msg "$NAME failed to start"
		exit 1
	fi

	PID=$(pidofproc $DAEMON)
	log_success_msg "$NAME with pid $PID started"
}

stop() {
	pidofproc $DAEMON > /dev/null 2>&1
	if [ "$?" -ne "0" ]; then
		log_success_msg "$NAME is not running"
		exit 0
	fi
	killproc $DAEMON
	if [ "$?" -ne "0" ]; then
		log_failure_msg "$NAME with PID $PID failed to stop"
		exit 1
	fi
	log_success_msg "$NAME with pid $PID stopped"
}

restart() {
	pidofproc $DAEMON > /dev/null 2>&1
	if [ "$?" -eq "0" ]; then
		stop
	fi
	nohup $DAEMON > /dev/null 2>&1 &
	if [ "$?" -ne "0" ]; then
		log_failure_msg "$NAME failed to start"
		exit 1
	fi
	PID=$(pidofproc $DAEMON)
	log_success_msg "$NAME with pid $PID started"
}

status() {
	pidofproc $DAEMON > /dev/null 2>&1
	STATUS=$?
	log_success_msg "$NAME status $STATUS"
	exit $STATUS
}

help() {
	echo "usage: $0 (start|stop|restart|status|help)"
cat <<EOF

start      - start lsbaus
stop       - stop lsbaus
restart    - stop lsbaus if running then start again, 
             if lsbaus is not running it is started
status     - print status
help       - this screen

EOF
	exit 1
}

case "$1" in
  start)
		start
    		;;
  stop)
		stop
		;;
  status)
		status
		;;
  restart)
		restart
		;;
  *)
		help
		;;
esac
