# This file is sourced by /etc/init.d/functions
# it redefines the exit function to
# call splashy_update.
#
# KNOWN BUGS
# Splashy and console-screen.sh don't work together.

# int exit (int exitstatus)
splashy_on_exit () {
    ##############################################################
    # Start splashy code 

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

    STEPS_DIR=/lib/init/rw/splashy
    SPL_UPD=/usr/sbin/splashy_update
    # load some default variables
    [ -r "/etc/sysconfig/splashy" ] && . "/etc/sysconfig/splashy"

    [ ! -d $STEPS_DIR ] && mkdir -p $STEPS_DIR

    SPL_PRG=$STEPS_DIR/$RUNLEVEL-progress

    [ -x $SPL_UPD ] || return $1;
    [ -f $SPL_PRG ] || return $1; 

    # It makes no sense for us to send this step if splashy is not running
    # Although then splashy_update would just return
    pidof splashy > /dev/null || return $1; 

    # Get progress percentage of this script
    # was calculated by update-progress-steps
    PER=`sed -n 's+'${0}' ++ p' $SPL_PRG`

    # This can not happen ...
    [ -n "$PER" ] || return $1;

    # in Sid 2006-10-08 05:57 EDT the scripts after S99rc.local
    # do not call lsb* functions. So we don't know when the boot process 
    # is done
    [ "${0##*/}" = "S99rc.local" ] && PER=100

    # Update progress bar 
    $SPL_UPD "progress $PER" &>/dev/null || true

    if [ "x$PER" != "x100" ]; then
        # Set $PER to 100% if X is started or about to be started
        #
        # Actually we should have already been stopped by log_daemon_msg
        case ${0} in 
            ?dm) PER=100;;
        esac
    fi

    if [ "x$PER" != "x100" ]; then
        pidof X > /dev/null && PER=100
    fi

    if [ "x$PER" != "x100" ]; then
        pidof Xgl > /dev/null && PER=100
    fi

    if [ "x$PER" != "x100" ]; then
        xsconsole=`ps aux | grep '/usr/bin/python /usr/lib/xsconsole/XSConsole.py' | wc -l`
        if [ "$xsconsole" -a "$xsconsole" -gt 1 ]; then
            PER=100
        fi
    fi

    if [ "x$PER" != "x100" ]; then
        pidof xswm > /dev/null && PER=100
    fi

    # Write to log (for testing)
    if [ "x$DEBUG" != "x0" ]; then
	cat /proc/loadavg >> $STEPS_DIR/splashy.log 2>&1
	echo "print ${0##*/} $PER" >> $STEPS_DIR/splashy.log
    fi

    # If we're at 100% stop splashy nicely
    [ "$PER" = "100" ] && stop_splashy
    return $1
}

stop_splashy () { 
    # 
    # when we exit Splashy there are a few things that need to be done:
    # - set progressbar to 100%
    # - Splashy always runs on tty8, if the user wants us to switch TTYs, 
    #   we will do it with splashy_chvt
    # - if Splashy stopped console-screen.sh and keyboard.sh, this is the time to re-run those
    #   console-screen.sh sets the fonts for the console and it cannot be done while in 
    #   graphics mode
    # - umount our STEPS_DIR
    STEPS_DIR=/lib/init/rw/splashy
    SPL_UPD=/usr/sbin/splashy_update
    # load some default variables
    [ -r "/etc/sysconfig/splashy" ] && . "/etc/sysconfig/splashy"

    # it makes no sense to do this if splashy is not running
    #  - if running:
    #    * set progress to 100%
    #    * wait a bit for it to actually show
    #    * send exit
    if test "`pidof splashy`"
    then
        $SPL_UPD "progress 100" &> /dev/null
        sleep 3.5
        $SPL_UPD "exit" &> /dev/null
        sleep 1
    fi

    # wait until splashy exits before changing tty's
    # this is because of the fade-out effect mostly
    # so 200 ms should do it
    pidof splashy > /dev/null && sleep 0.2

    [ ! -d $STEPS_DIR ] && mkdir -p $STEPS_DIR

    # Write to log (for testing)
    if [ "x$DEBUG" != "x0" ]; then
	echo "passed 'exit' was call" >> $STEPS_DIR/splashy.log
        pidof splashy > /dev/null && echo 'Splashy is still running!' >> $STEPS_DIR/splashy.log
	cat /proc/loadavg >> $STEPS_DIR/splashy.log 2>&1
    fi

    # if splashy does not exit. we kill -9 it...
    while test "`pidof splashy`"
    do
        if [ "x$DEBUG" != "x0" ]; then
	    echo "Splashy didn't die!" >> $STEPS_DIR/splashy.log
	fi

        kill -15 `pidof splashy`
	sleep 0.2

	#echo "calling killall -9 splashy" >> $STEPS_DIR/splashy.log
	killall -9 splashy > /dev/null 2>&1
    done

    # clear tty8 of console messages
    # (see splashy_video.c:splashy_start_splash().DirectFBSetOption("vt-num","8"))
    if [ "$(fgconsole 2>/dev/null)" = "8" ]; then
        clear >/dev/tty8 || true
    fi
 
    # we need to re-run keymap.sh and console-screen.sh only if Splashy scripts stopped it
    # see /etc/kbd/conf.d/splashy and /etc/console-tools/config.d/splashy
    if [ -x "/etc/init.d/keymap.sh" -a -f "$STEPS_DIR/splashy-stopped-keymap" ]; then
        if [ "x$DEBUG" != "x0" ]; then
            cat /proc/loadavg >> $STEPS_DIR/splashy.log 2>&1
            echo "calling keymap.sh" >> $STEPS_DIR/splashy.log
        fi
        /etc/init.d/keymap.sh start || true
        rm -f $STEPS_DIR/splashy-stopped-keymap
        # clear tty8 of console messages
        # (see splashy_video.c:splashy_start_splash().DirectFBSetOption("vt-num","8"))
        if [ "$(fgconsole 2>/dev/null)" = "8" ]; then
            clear >/dev/tty8 || true
        fi
    fi

    # console-screen.sh still stops Splashy at this point. Do we still need to run this?? - Luis
    if [ -x "/etc/init.d/console-screen.sh" -a -f "$STEPS_DIR/splashy-stopped-console-screen" ]; then
        if [ "x$DEBUG" != "x0" ]; then
            cat /proc/loadavg >> $STEPS_DIR/splashy.log 2>&1
            echo "calling console-screen.sh" >> $STEPS_DIR/splashy.log
        fi
        /etc/init.d/console-screen.sh start || true
        # whether it worked or not, we do not care
        rm -f $STEPS_DIR/splashy-stopped-console-screen
        # clear tty8 of console messages
        # (see splashy_video.c:splashy_start_splash().DirectFBSetOption("vt-num","8"))
        if [ "$(fgconsole 2>/dev/null)" = "8" ]; then
            clear >/dev/tty8 || true
        fi
    fi

    # Do some magic with the TTYs
    if [ -z "$CHVT_TTY" ] && [ "$(fgconsole 2>/dev/null)" != "$CHVT_TTY" ]; then 
        splashy_chvt $CHVT_TTY &>/dev/null || true
    fi

}

