#!/bin/sh
#
#	/etc/init.d/atd
#
# Starts the at daemon
#
# chkconfig: 345 40 60
# description: atd 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
# pidfile: /var/run/atd.pid

WITHOUT_RC_COMPAT=1

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

PIDFILE=/var/run/atd.pid
LOCKFILE=/var/lock/subsys/atd
RETVAL=0

start()
{
	local NPROCS LOAD

	LOAD=
	NPROCS=`egrep -cs ^cpu[0-9]+ /proc/stat`
	if [ "$NPROCS" -gt 1 ] 2>/dev/null; then
		LOAD="-l $((NPROCS-1)).8"
	fi

	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" -- atd $LOAD
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" atd
	RETVAL=$?
	return $RETVAL
}

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|condreload)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --pidfile "$PIDFILE" atd
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
