#!/bin/sh
#
#	/etc/init.d/atd
#
# Starts the at daemon
#
# chkconfig: 345 40 60
# description: Runs commands scheduled by the at command at the time \
#    specified when at was run, and runs batch commands when the load \
#    average is low enough.
# processname: atd

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

EXE=/usr/sbin/atd
[ -x "$EXE" ] || exit

LOCKFILE=/var/lock/subsys/atd
RETVAL=0

start()
{
	NPROCS="`(egrep -sc ^cpu[0-9]+ /proc/stat)`"
	[ -n "$NPROCS" -a "$NPROCS" -gt 0 ] || NPROCS=1
	LOAD=
	if [ "$NPROCS" -gt 1 ]; then
		LOAD="-l $[NPROCS-1].8"
	fi

    echo -n 'Starting at daemon: '
	daemon "$EXE" $LOAD
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	echo
}

stop()
{
	echo -n 'Stopping at daemon: '
	killproc "$EXE"
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	echo
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status "$EXE"
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
