#!/bin/sh
set -e
### BEGIN INIT INFO
# Provides:          later-readahead
# Required-Start:    $remote_fs
# Required-Stop:
# Should-Start:      mountkernfs
# X-Start-Before:
# Default-Start:     S
# Default-Stop:
# Short-Description: init script for readahead-fedora
### END INIT INFO

READAHEAD_CMD="/sbin/readahead"
READAHEAD_COLLECT_CMD="/sbin/readahead-collector"

[ -x $READAHEAD_CMD ] || exit 0

. /lib/lsb/init-functions

RUN_IN_BACKGROUND=
IO_PRIORITY=
MAX_FILE_SIZE=
READAHEAD_BASE=/etc/readahead.d
[ ! -f /etc/default/readahead-fedora ] || . /etc/default/readahead-fedora

[ "${READAHEAD:-yes}" = yes ] || exit 0

# Exit if the collector is already running
pidof readahead-collector > /dev/null && exit

LTYPE=later

if [ "$MAX_FILE_SIZE" ]; then
    MAX_FILE_SIZE="-m $MAX_FILE_SIZE"
fi

NICE_COLLECTOR="-N ${NICE_COLLECTOR:-5}"

get_lists() {
    if [ -f "$READAHEAD_BASE/$LTYPE.sorted" ]; then
	printf -- "--dont-sort %s" "$READAHEAD_BASE/$LTYPE.sorted"
    elif [ -s "$READAHEAD_BASE/custom.$LTYPE" ]; then
	printf "%s" "$READAHEAD_BASE/custom.$LTYPE"
    else
	ls "$READAHEAD_BASE"/*."$LTYPE" 2>/dev/null || echo /dev/null
    fi
}

SHOULD_COLLECT=false
if test -f /.readahead_collect || grep -qw profile /proc/cmdline; then
    SHOULD_COLLECT=true
fi

case "$1" in
    start|restart|force-reload)
	[ -f /run/readahead ] || {
	    [ "$LTYPE" = early ] || {
		log_warning_msg "Early readahead flag not found"
	    }
	    echo "
# This file was automatically generated by $0
# The following lines should indicate whether the /usr and /var file systems
# were available during the early boot process. Note that these flags do not
# directly modify the behaviour of the init scripts; they are used by the
# script that sorts the files lists
MOUNTED_USR=$([ -d /usr/bin ] && echo true || echo false)
MOUNTED_VAR=$([ -d /var/lib ] && echo true || echo false)
		" > /run/readahead
	}

	if [ "$RUN_IN_BACKGROUND" = yes ] && ! $SHOULD_COLLECT; then
	    log_begin_msg "Reading $LTYPE boot files (in background)..."
	    if /sbin/start-stop-daemon --background --start --quiet \
		-I ${IO_PRIORITY:-idle} --exec $READAHEAD_CMD \
		-- $MAX_FILE_SIZE $(get_lists) > /dev/null; then
		log_end_msg 0
	    else
		log_end_msg $?
	    fi
	else
	    log_begin_msg "Reading $LTYPE boot files..."
	    if /sbin/start-stop-daemon --start --quiet \
		-I ${IO_PRIORITY:-real-time} --exec $READAHEAD_CMD \
		-- $MAX_FILE_SIZE $(get_lists) > /dev/null; then
		log_end_msg 0
	    else
		log_end_msg $?
	    fi
	fi

	if $SHOULD_COLLECT; then
	    if [ -d /proc/1 ] && [ ! -e /proc/1/loginuid ]; then
		log_failure_msg "Profiling on a kernel without audit subsystem..."
		exit 1
	    fi
	    log_begin_msg "Preparing to profile boot sequence..."
	    if /sbin/start-stop-daemon --start --quiet $NICE_COLLECTOR \
		--exec $READAHEAD_COLLECT_CMD; then
		log_end_msg 0
	    else
		log_end_msg $?
	    fi
	    if [ -f /etc/init.d/auditd ]; then
		log_warning_msg "Auditd installed, read readahead's README"
	    fi
	fi
	;;
	stop)
	;;
	*)
	    echo "Usage: $0 {start|stop|restart|force-reload}"
	    exit 1
	;;
esac
