#! /bin/sh
#
# irqbalance       Start/Stop irq balancing daemon
#
# chkconfig: 345 13 87
# description: The irqbalance daemon will distribute interrupts across  \
#              the cpus on a multiprocessor system with the purpose of \
#              spreading the load. \
# processname: irqbalance


# This is an interactive program, we need the current locale

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

# Check that we're a priviledged user
[ `id -u` = 0 ] || exit 4


prog="irqbalance"

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
	exit 1
fi

[ -f /usr/sbin/irqbalance ] || exit 5

# fetch configuration if it exists
# ONESHOT=yes says to wait for a minute, then look at the interrupt
# load and balance it once; after balancing exit and do not change
# it again.
# The default is to keep rebalancing once every 10 seconds.
ONESHOT=
[ -f /etc/sysconfig/irqbalance ] && . /etc/sysconfig/irqbalance
case "$ONESHOT" in
	y*|Y*|on) ONESHOT=--oneshot ;;
	*) ONESHOT= ;;
esac

RETVAL=0

start() {
	local CPUCOUNT=`grep ^processor /proc/cpuinfo | wc -l`
	if [ $CPUCOUNT -lt 2 ]; then
		#dont start the service for less than 2 cpus
		failure "Can't balance irqs on a uniprocessor system: "
		return 1
	fi
        echo -n $"Starting $prog: "
        daemon irqbalance $ONESHOT
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 -a -z "$ONESHOT" ] && touch /var/lock/subsys/irqbalance
	return $RETVAL
}


stop() {
        echo -n $"Stopping $prog: "
        killproc irqbalance
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/irqbalance
	return $RETVAL
}

restart() {
	stop
	start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status irqbalance
	;;
  restart|reload)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/irqbalance ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
	exit 1
esac

exit $?
