#!/bin/sh
#
# atop		This shell script takes care of initializing atop
#
# chkconfig: - 85 15
# description:	Atop is a system and process activity monitor
#

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Check existance of binaries 
[ -f /usr/bin/atop ] || exit 0

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

PIDFILE=/var/run/atop.pid
RETVAL=0

# See how we were called.
case "$1" in
  start)
	# Check if atop runs already
	#
	if [ -e $PIDFILE ] && ps -p `cat $PIDFILE` | grep 'atop$' > /dev/null
	then
		:
	else
		# Start atop
		/etc/atop/atop.daily
	fi
	;;

  stop|condstop)
	# Check if atop runs
	#
	if [ -e $PIDFILE ] && ps -p `cat $PIDFILE` | grep 'atop$' > /dev/null
	then
		kill -USR1 `cat $PIDFILE` 	# take final sample
		sleep 3
		kill -TERM `cat $PIDFILE`
		rm $PIDFILE
		sleep 1
	fi
	;;

  status)
	status --pidfile "$PIDFILE" --expect-user root -- atop
        RETVAL=$?
	;;

  reload|condreload)
	/etc/atop/atop.daily
	;;

  restart|condrestart)
	/etc/atop/atop.daily
	;;

  *)
	echo "Usage: $0 [start|stop|status|reload|restart|condstop|condreload|condrestart]"
	exit 1
esac

exit $RETVAL
