#!/bin/sh -e
# init script for readahead (second stage)

# Check the package is still installed
[ -x /sbin/readahead-list ] || exit 0

# Get LSB functions
. /lib/lsb/init-functions
. /etc/default/rcS


# Remember that any major changes to this script need to also be made to
# readahead.init

# Remove the desktop list to disable this readhead phase
[ -r /etc/readahead/desktop ] || exit 0

case "$1" in
    start|restart|force-reload)
	# This script is only run if /usr or /var are on a different
	# filesystem.
	if mountpoint -q /usr; then
	    dirs="$dirs /usr"
	fi
	if mountpoint -q /var; then
	    dirs="$dirs /var"
	fi

	[ -n "$dirs" ] || exit 0

	# This can take a while
	if type usplash_write >/dev/null 2>&1; then
	    usplash_write "TIMEOUT 360" || true
	fi

	# If "profile" is placed on the kernel command-line we watch the boot
	# sequence and generate new readahead lists, rather than read the lists
	if ! grep -q "profile" /proc/cmdline; then
	    log_begin_msg "Reading files needed to boot (second stage)..."
	    if /sbin/start-stop-daemon --start --quiet \
		--pidfile /var/run/readahead-list.bogus \
		--startas /sbin/readahead-list -- -s /etc/readahead/desktop; then
		log_end_msg 0
	    else
		log_end_msg $?
	    fi
	else
	    # Move the first stage's pid file so we can still kill it later
	    mv /var/run/readahead-watch.pid /var/run/readahead-watch-boot.pid

	    # Just watch the newly mounted directories
	    log_begin_msg "Preparing to profile boot sequence (second stage)..."
	    if /sbin/start-stop-daemon --start --quiet \
		--pidfile /var/run/readahead-watch.bogus \
		--startas /sbin/readahead-watch \
		-- -o /etc/readahead/desktop $dirs; then
		log_end_msg 0	
	    else
		log_end_msg $? || true
	    fi 
	fi

	if type usplash_write >/dev/null 2>&1; then
	    usplash_write "TIMEOUT 15" || true
	fi
	;;
    stop)
	;;
    *)
	echo "Usage: /etc/init.d/readahead {start|stop|restart|force-reload}"
	exit 1
	;;
esac

exit 0
