#! /bin/bash
#
# chkconfig: 2345 98 02
# description: System startup script for console splash screens
# halt: yes


test -f /proc/splash || exit 0

# Source function library.
. /etc/init.d/functions

SourceIfNotEmpty /etc/sysconfig/bootsplash

[ "$SPLASH" != no ] ||
    exit

[ "$THEME" != no ] ||
    THEME=current

LOCKFILE=/var/lock/subsys/splash
RETVAL=0

stop ()
{
    test -x /sbin/splash || exit 1
    local splashv=`cat /proc/splash` 
    local res="${splashv##*, }"
    test "$splashv" != "$res" || exit 1
    res="${res%)*}"
    splcfg="/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg"
    fgconsole=`/usr/bin/fgconsole`
    fgconsole=$((fgconsole-1))
    chvt $fgconsole
    [ -f "$splcfg" ] &&
	action "Setting up splash screen #$fgconsole ($res):" /sbin/splash -s -u "$fgconsole" "$splcfg"
    rm -f "$LOCKFILE"
}
disable()
{
  echo "0" >/proc/splash
}
status()
{
  cat /proc/splash
}
start()
{
	test -x /sbin/splash || exit 1
	local splashv=`cat /proc/splash` 
	local res="${splashv##*, }"
	test "$splashv" != "$res" || exit 1
	res="${res%)*}"
        vtsplash=`ls -1 /etc/bootsplash/themes/$THEME/config/vt*-$res.cfg 2>/dev/null`
	if [ -n "$vtsplash" ]; then
	    for splcfg in $vtsplash; do
		    test -f $splcfg || continue
		    local unit="${splcfg#/etc/bootsplash/themes/$THEME/config/vt}"
		    unit="${unit%%-*}"
		    action "Setting up splash screen #$unit ($res):" /sbin/splash -s  -u "$unit" "$splcfg"
		    echo "verbose" >/proc/splash
	    done
	else
	    splcfg="/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg"
	    [ -f "$splcfg" ] &&
		action "Setting up splash screen #0 ($res):" /sbin/splash -s "$splcfg"
	fi
	touch "$LOCKFILE"
}

case "$1" in
	start|restart|reload)
		start
		;;
	condrestart)
		;;
	stop|condstop)
		;;
	disable)
		disable
		;;
	status)
		status
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|disable|status|reload|restart}"
		RETVAL=1
esac

exit $RETVAL
