#! /bin/sh
### BEGIN INIT INFO
# Provides:          bootlogd
# Required-Start:    mountvirtfs
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 1 2 3 4 5 6
# Short-Description: Start bootlogd to log boot messages.
# Description:       One of the first scripts to be executed. Starts or stops
#                    the bootlogd log program. If this script is called as
#                    "stop-bootlogd", it will stop the daemon instead of
#                    starting it even when called with the "start" argument.
### END INIT INFO
#
# Version:	@(#)bootlogd  2.85-12  21-Mar-2004  miquels@cistron.nl
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/sbin/bootlogd
NAME=bootlogd
DESC="Bootlog daemon"

test -f $DAEMON || exit 0

[ -r /etc/default/bootlogd ] && . /etc/default/bootlogd

## set -e # not needed

case "$BOOTLOGD_ENABLE" in
	[Nn]*)
		exit 0
		;;
esac

. /lib/lsb/init-functions

STOPPER=
ACTION="$1"
case "$0" in
	*stop-bootlog*)
		STOPPER=Y
		if [ "$ACTION" = start ]
		then
			ACTION=stop
		fi
		;;
esac

case "$ACTION" in
	start)
		log_daemon_msg "Starting $DESC" "$NAME"
		if [ -d /proc/1/. ]
		then
			umask 027
			start-stop-daemon --start --quiet --exec $DAEMON -- \
				-r
			ES=$?
		else
			$DAEMON -r
			ES=$?
		fi
		log_end_msg $ES
		;;
	stop)
		log_daemon_msg "Stopping $DESC" "$NAME"
		start-stop-daemon --stop --quiet --exec $DAEMON
		ES=$?

		if [ "$STOPPER" ] && [ -f /var/log/boot ] && \
		   [ -f /var/log/boot~ ]
		then
			cd /var/log
			chgrp adm boot
			savelog -p -c 5 boot > /dev/null 2>&1
			mv boot.0 boot
			mv boot~ boot.0
		fi

		log_end_msg $ES
		;;
	 restart|force-reload)
	 	sh $0 stop
		sleep 1
	 	sh $0 start
		;;
	*)
		N=${0##*/}
		N=${N#[SK]??}
		echo "Usage: $N {start|stop|restart|force-reload}" >&2
		exit 3
		;;
esac

exit 0
