#!/bin/sh
### BEGIN INIT INFO
# Provides:          splashy
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 6
# Short-Description: A script to calculate the progress 
#                    percentage for init scripts
# Description:       This calculates the progress percentage 
#                    for the scripts in /etc/rcS.d and 
#                    /etc/rc$CURRENT_RUNLEVEL.d.
### END INIT INFO

# Author: Tim Dijkstra <newsuser@famdijkstra.org>
#
# If called in the rc[3456].d runlevels with the start target
# this will try to stop a running splashy process.
# When called in the rc[06].d runlevels with the stop target
# it will calculate the progress percentage which will be used
# by the calls to splashy_update in the log_end_msg functions.
#
# It's really simple. We just count them and put them
# in alpha-numeric order. Their percentage is then just
# int( their number on the list * ( 100 / total number on list) )
#
# Of course not all packages use log_end_msg yet, but that
# doesn't matter. The packages that do, will trigger the update
# anyway. This may result in big jumps in the percentage.
# The more scripts start using it, the more granular it will become.
#
# I think it is best to run this script as the first script
# on halt and reboot. Then we always have an up to date list.
# We can't do it on boot, because then '/' is not writable.
# We could also integrate this is in splashy_update, but I think
# it faster this way.
#
# Luis Mondesi <lemsx1@gmail.com> 
# This script also needs to detect if Splashy is running and if not
# start it. It's assumed that this will only be run while halt/reboot
# and at RUNLEVEL S.
# This script is the first one to run during these stages
#

# Note that we run very early at boot and /usr is not even mounted
# we include /usr for our "stop" target (namely "awk" and "wc")
PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=splashy
DESC="Boot splash manager"

if [ -r /etc/default/splashy ] ; then
            . /etc/default/splashy
fi

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# we cannot use sed -e here because when we have hanging symlinks
# grep -l will return an error
#set -e

# Bug #400598
if [ -z "$RUNLEVEL" ]; then
    # we need only the current level
    RUNLEVEL=`runlevel | sed 's/^. //'`
fi

case "$1" in
    # Sounds a bit weird, but called as start will stop splashy;)
    start)
        if [ "x$RUNLEVEL" = "xN S" -o "x$RUNLEVEL" = "xS" ]; then
            log_daemon_msg "Starting $DESC" $NAME
            pidof splashy > /dev/null || /sbin/splashy boot
            log_end_msg $?
        else
            log_daemon_msg "Stopping $DESC" $NAME
            /sbin/splashy_update exit
            log_end_msg $?
            # wait until splashy exits before changing tty's
            while `pidof splashy > /dev/null`; do
                sleep 0.2
            done
            # do some magic with the TTYs
            if test -z "$CHVT_TTY"; then
		CHVT_TTY=1
            fi
            # detect X, if not, go to CHVT_TTY
            X11_RUNNING=0
            pidof X > /dev/null && X11_RUNNING=1
            if [ $X11_RUNNING -eq 1 ]; then
                splashy_chvt 7
            else
                splashy_chvt $CHVT_TTY
            fi
        fi
    ;;
    stop)
    # note that these steps are done with a fully mounted /usr
        log_daemon_msg "(Re)generating splash steps for "

        RLVL=`sed -n 's/id:\([2345]\):initdefault:/\1/ p' /etc/inittab`
        TMP=`mktemp`
        DIR=/lib/splashy

        if [ ! -d "$DIR" ]; then
            log_action_end_msg 1
            exit 1
        fi

        # While booting rcS will also be executed
        #ls /etc/rcS.d/S* > $TMP
        # we only care about the scripts that actually call log_end_msg
        # if not we end up never completing our progressbar!
        grep -l log_end_msg /etc/rcS.d/S* > $TMP 2> /dev/null

        for I in ${RLVL:=2} 0 6; do
            #ls /etc/rc$I.d/[KS]* >> $TMP
            # in debian rc.local runs log_end_msg conditionally. we simply skip that
            grep -l log_end_msg /etc/rc$I.d/[KS]* 2> /dev/null | grep -v rc.local >> $TMP 
            
            NR=`wc -l $TMP`
            awk '{print $1, int(FNR/'${NR%%[[:space:]]*}'*100)}' $TMP > "$DIR/$I-progress"
            # Truncate $TMP file
            echo -n > $TMP
            log_action_cont_msg "rc$I.d"
        done

        # In the first stage of booting RUNLEVEL will be S
        ln -sf "$DIR/$RLVL-progress" "$DIR/S-progress"
        rm -f $TMP

        log_action_end_msg 0

        log_daemon_msg "Starting $DESC" $NAME
        pidof splashy > /dev/null || /sbin/splashy shutdown
        log_end_msg $?

    ;;
    # We only do somthing usefull in the stop target.
    restart|force-reload)
        $0 stop
        ;;

    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

