#!/bin/sh
#
# splash	splashed console
#
# chkconfig:	345 98 98
#
# description:	Activate splashed consoles
#
# $Id: splashutils.init,v 1.13 2008/04/26 19:39:19 glen Exp $


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

# Set defaults
RES=$(/usr/bin/fbres)

# Get service config - may override defaults
[ -f /etc/sysconfig/splash ] && . /etc/sysconfig/splash

set_theme() {
	local vc=$1
	local theme=$2

	out=$(fbcondecor_ctl --vc=$vc -t "$theme" -c setcfg 2>&1)
	rc=$?
	[ $rc = 0 ] || return $rc
	[ "$out" ] && return 1

	if [ $vc = 0 ]; then
		fbcondecor_ctl --vc=$vc -t "$theme" -c setpic
	fi

	fbcondecor_ctl --vc=$vc -c on
	return $?
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/splash ]; then
		msg_already_running splash
		return
	fi

	# if fbcondecor not initialized, exit
	if [ ! -e /dev/fbcondecor ] || [ ! -e /proc/sys/kernel/fbcondecor ]; then
		return
	fi

	[ "$SPLASH_THEME" ] || SPLASH_THEME="default"

	# no theme or no resolution for this theme, exit too
	if [ ! -f /etc/splash/$SPLASH_THEME/$RES.cfg ]; then
		return
	fi

	if [ -z "$SPLASH_TTYS" -a -d /sys/class/vc ]; then
		SPLASH_TTYS=$(ls -dv /sys/class/vc/vcsa* | awk '{printf("%d\n", substr($0, length("/sys/class/vc/vcsa") + 1))}')
	fi
	if [ -z "$SPLASH_TTYS" ]; then
		SPLASH_TTYS=$(awk -F: '/^[0-9]*:/{print $1}' /etc/inittab)
	fi

	show "Setting framebuffer console images"; echo
	for TTY in $SPLASH_TTYS; do
		theme=$SPLASH_THEME

		if [ -n "$SPLASH_TTY_MAP" ]; then
			for i in $SPLASH_TTY_MAP; do
				if [ "${i%:*}" = "$TTY" ]; then
					theme="${i#*:}"
				fi
			done
		fi

		show " console %d: theme: '%s'" $TTY "$theme"
		set_theme $TTY "$theme"
		if [ $? = 0 ]; then
			ok
		else
			fail
		fi
	done
	touch /var/lock/subsys/splash
}

stop() {
	if [ -f /var/lock/subsys/splash ]; then
		rm -f /var/lock/subsys/splash
	else
		msg_not_running splash
	fi
}

# See how we were called.
case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	stop
	start
	;;
*)
	msg_usage "$0 {start|stop|restart}"
	exit 3
esac

exit $RETVAL
