#!/bin/bash
# description: start or stop the eig_performance daemon
# chkconfig: 2345 20 10

# source function library
. /etc/rc.d/init.d/functions

PIDFILE='/var/run/eig_performance.start'

if [[ -f $PIDFILE ]]; then
	PID=$(cat $PIDFILE)
	if [[ -d /proc/$PID ]]; then
		RUNNING=1
	else 
		RUNNING=0
	fi
else
	RUNNING=0
fi

case "$1" in
'start')
	if [[ $RUNNING -eq 1 ]]; then
		echo "eig_performance already running"
		exit 1
	fi
	echo "Starting eig_performance.pl"
	if [[ ! -d /sys/fs/cgroup/cpu/USERS ]]; then
		echo "cannot find /sys/fs/cgroup/USERS, is this kernel patched?"
		exit 1
	fi
	touch /var/log/eig_performance.log
	nohup /opt/eig_linux/bin/eig_performance.pl >/dev/null 2>&1 &
	RETVAL=$?
	;;
'stop')
	if [[ $RUNNING -eq 0 ]]; then
		echo "eig_performance is not running"
		exit 1
	fi
	echo "Stopping eig_performance.pl"
	kill -SIGABRT $PID
	RETVAL=$?
	;;
'restart')
	if [[ $RUNNING -eq 0 ]]; then
		echo "eig_performance is not running"
	else
		echo "Stopping eig_performance.pl"
		kill -SIGABRT $PID
	fi
	echo "Starting eig_performance.pl"
	nohup /opt/eig_linux/bin/eig_performance.pl >/dev/null 2>&1 &
	RETVAL=$?
	;;
'status')
        status eig_performance.pl
        ;;
*)
       echo "Usage: $0 { start | stop | restart | status }"
       RETVAL=1
       ;;
esac
exit $RETVAL

