#!/bin/sh
### BEGIN INIT INFO
# Provides:          splashy
# 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>, 
#         Luis Mondesi <lemsx1@gmail.com> 
#         Luca Capello <luca@pca.it>
#
# If called in the rc[06].d runlevels with the stop target it will start 
# splashy in 'shutdown' mode. In the rcS.d runlevel it will try 
# to start splashy if it didn't start yet from initramfs.
#
# When it decides to start splashy it will first calculate the 
# progress percentage which will be used by the calls to splashy_update 
# in the log_end_msg functions.
# This is 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.
#
#
# 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.
#

#
# Heavily modified by Citrix Systems.
#

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
NAME="splashy"
DESC="Boot splash manager"
STEPS_DIR="/lib/init/rw/$NAME"
DEBUG=0

# source function library
. /etc/rc.d/init.d/functions

[ -r "/etc/sysconfig/$NAME" ] && . "/etc/sysconfig/$NAME"

set -e


check_to_enable () {
    ENABLE=0
    SINGLE=false
    SPLASH=false
    FBMODESET=false

    for x in $(cat /proc/cmdline); do
        case $x in
        single)
            SINGLE=true
        ;;
        splash)
            SPLASH=true
        ;;
        nosplash)
            SPLASH=false
        ;;
        vga=*|video=*)
            FBMODESET=true
        ;;
        esac
    done
    [ "$SPLASH" = "true" -a "$FBMODESET" = "true" ] && ENABLE=1
    [ "$SINGLE" = "true" ] && ENABLE=0

    if [ "$ENABLE" = "0" ]; then
        exit 0
    fi
}

calculate_steps () {
    [ -f /etc/inittab ] &&  RLVL=`sed -n 's/id:\([2345]\):initdefault:/\1/ p' /etc/inittab`

    [ ! -d $STEPS_DIR ] && mkdir -p $STEPS_DIR
    #
    # Mount a tmpfs on $STEPS_DIR
    #
    # on Ubuntu our Steps dir gets umounted if using /dev/shm/splashy
    # we will always use /lib/init/rw/splashy and mount a tmpfs there
    if ! touch $STEPS_DIR/starting &>/dev/null
    then
        mount -n -t tmpfs none $STEPS_DIR
        touch $STEPS_DIR/starting
    fi

    if ! test -c $STEPS_DIR/fb0
    then
        mknod $STEPS_DIR/fb0 c 29 0 &>/dev/null
    fi
    export FRAMEBUFFER=$STEPS_DIR/fb0

    TMP=`mktemp -p $STEPS_DIR`

    for ILVL in ${RLVL:=2} 0 6; do
        for i in /etc/rc$ILVL.d/K*
        do
            subsys=${i#/etc/rc$ILVL.d/K??}
            [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] ||  continue
            echo $i >> $TMP
        done
        grep -l exit /etc/rc$ILVL.d/S* 2> /dev/null |\
        grep -v rc.local |\
        grep -v killall |\
        grep -v reboot |\
        grep -v halt |\
        grep -v splashy >> $TMP  || /bin/true
                    
        NR=`sed -n -e '$=' $TMP`
        I=1
        for SCR in `cat $TMP`; do
            echo "$SCR $(($I*100/$NR))"
            I=$(($I+1))
        done > "$STEPS_DIR/$ILVL-progress"

        # Truncate $TMP file
        echo -n > $TMP
    done

    rm -f $TMP
}

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

case "$1" in
    start)
        if test "`pidof $NAME`"
        then
            stop_splashy
        elif ! test -f $STEPS_DIR/starting
	then
            check_to_enable

            calculate_steps

            if [ "$DEBUG" = "1" ]; then
                pidof $NAME > /dev/null || \
                echo "$0: Splashy not running?" >> $STEPS_DIR/splashy.log
            fi
            /usr/sbin/$NAME boot &> /dev/null
        fi
        touch /var/lock/subsys/splashy &> /dev/null
    ;;
    stop)
        if test "`pidof $NAME`" 
        then
            rm -f /var/lock/subsys/splashy
            rm -f $STEPS_DIR/starting
            stop_splashy
        else
            check_to_enable
            calculate_steps
            /usr/sbin/$NAME shutdown &> /dev/null
        fi
    ;;

    restart|force-reload)
        calculate_steps $RUNLEVEL
        ;;

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

exit 0

