#!/bin/sh
splash_init=initsplash
splash_exe=/sbin/splash.sh
[ -z "$splash_proc" ] && splash_proc=/proc/splash
[ -z "$splash_cfg" ] && splash_cfg=/etc/bootsplash/
[ -f /etc/sysconfig/bootsplash ] && source /etc/sysconfig/bootsplash
[ -n $SPLASH ] && splash_rc=$SPLASH
if is_yes "$splash_rc"; then
    splash_rc="yes"
    if [ -e "$splash_proc" ]; then 
	grep -iwqs off "$splash_proc" && splash_rc=no
    else
	splash_rc=no
    fi	    
else
    splash_rc="no"
fi
[ -d $splash_cfg/themes ] || splash_rc="no"
[ -x $splash_exe ] || splash_rc="no"
export splash_rc

if is_yes "$splash_rc"; then
    function initsplash() {
	[ -f /etc/sysconfig/bootsplash ] && source /etc/sysconfig/bootsplash
	[ -n $THEME ] && splash_theme=$THEME

	if [ -n $splash_theme ];then
	    [ ! -d $splash_cfg/themes/$splash_theme ] && splash_theme=current
	
	    splash_res=`fbresolution`
	    tmpval=$LOGO_CONSOLE
	    splash_theme_file="/etc/bootsplash/themes/$splash_theme/config/bootsplash-$splash_res.cfg"
    	    if [ -f $splash_theme_file ]; then
		function box() { true; } # ignore box descriptions in the config file
		function mnganim() { true; } # ignore mnganim descriptions in the config file
		function trigger() { true; } # ignore trigger descriptions in the config file
        	. $splash_theme_file
	    fi

	    if [[ $tmpval != "theme" ]];then
		LOGO_CONSOLE=$tmpval
	    fi
	fi
	if [ -z "$1" ]; then
	    set `/sbin/runlevel`
    	    runlevel=$2
    	    previous=$1
	else
	    runlevel=$1
	    previous=N
	fi
	nbservices=0
	# 
	# for small dir, it is faster than echo /etc/rc$runlevel.d/* | wc -w
	#
	for i in /etc/rc$runlevel.d/*; do
	    a=$[splash_nbservices++]
	done
	for i in /etc/rc$runlevel.d/*.rpm*; do
	    a=$[splash_nbservices--]
	done
	a=$[splash_nbservices++]
	# this is the number of step in rc.sysinit, could be ajusted
	[[ "$previous" = "N" ]] && splash_nbservices=$(($splash_nbservices+9)) && splash_progress=8
	PROMPT=
	export splash_nbservices splash_res splash_theme progress_enable splash_progress splash_theme_file LOGO_CONSOLE PROMPT
    }

    function splash_trigger() {
	case "$runlevel" in
	    [06]) /sbin/splash -S -t "$1" "$splash_theme_file" ;;
	    *)    /sbin/splash    -t "$1" "$splash_theme_file"
	esac
    }
else
    function initsplash() { :; }
    function splash_trigger() { :; }
fi
