#!/bin/sh
#
# preload	Starts the preload daemon
#
# chkconfig:	345 05 95
# description:	Adaptive readahead daemon
# processname: preload
#
# $Id$

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

# Get service config - may override defaults
[ -f /etc/sysconfig/preload ] && . /etc/sysconfig/preload

start() {
	MIN_MEMORY=${MIN_MEMORY:-256}
	# Check for > MIN_MEMORY MB
	free -m | awk '/Mem:/ {exit ($2 >= ('"$MIN_MEMORY"'))?0:1}' || exit 0

	# Check for ionice and use idle scheduling if available
	RUNAS=""
	IONICE="/usr/bin/ionice"
	if [ -n "$IONICE_OPTS" ]; then
		if [ -x "$IONICE" ]; then
			RUNAS="$IONICE $IONICE_OPTS"
		else
			nls "ionice not found, running with normal IO priority" >&2
		fi
	fi

	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/preload ]; then
		msg_starting preload
		daemon $RUNAS /usr/sbin/preload $PRELOAD_OPTS --statefile /var/lib/misc/preload.state
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/preload
	else
		msg_already_running preload
	fi
}

stop() {
	if [ -f /var/lock/subsys/preload ]; then
		# Stop daemons.
		msg_stopping preload
		killproc /usr/sbin/preload
		rm -f /var/lock/subsys/preload
	else
		msg_not_running preload
	fi
}

reload() {
	if [ -f /var/lock/subsys/preload ]; then
		msg_reloading preload
		killproc preload -HUP
		RETVAL=$?
	else
		msg_not_running preload
		RETVAL=7
	fi
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  reload|force-reload)
	reload
	;;
  status)
	status preload
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL

# This must be last line !
# vi:syntax=sh:tw=78:ts=4:sw=4
