#!/bin/sh
#
# readahead	Prereads programs required for startup into memory
#
# chkconfig:	2345 1 99
# description:	This service causes the programs used during startup\
#		to be loaded into memory before they are needed,\
#		thus improving startup performance
#
# config:	/etc/readahead.conf
#
### BEGIN INIT INFO
# Provides: readahead_early
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Short-Description: start and stop early readahead
# Description: Start and stop early readahead
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# Check for > 384 MB
free -m | gawk '/Mem:/ {exit ($2 >= 384)?0:1}' || exit 0

VAR_SUBSYS_READAHEAD="/var/lock/subsys/readahead_early"
READAHEAD_CMD="/sbin/readahead"
RETVAL=0

start()
{
	LTYPE="early"
	READAHEAD_BASE="/var/lib/readahead"
	echo -n $"Starting background readahead ($LTYPE, "
	if [ -s "$READAHEAD_BASE/$LTYPE.sorted" ]; then
	    echo -n $"fast mode): "
	    $READAHEAD_CMD --dont-sort $READAHEAD_BASE/$LTYPE.sorted >/dev/null &
	elif [ -s "$READAHEAD_BASE/custom.$LTYPE" ]; then
	    echo -n $"customized): "
	    $READAHEAD_CMD $READAHEAD_BASE/custom.$LTYPE >/dev/null &
	else
	    echo -n $"full mode): "
	    $READAHEAD_CMD $READAHEAD_BASE/*.$LTYPE >/dev/null &
	fi
	touch $VAR_SUBSYS_READAHEAD
	echo_success
	echo
	RETVAL=$?
	return $RETVAL
}

stop()
{
	rm -f $VAR_SUBSYS_READAHEAD
	msg_stopping readahead_early
	echo_success
	echo
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

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

exit $RETVAL
