#!/sbin/runscript
# Copyright 1999-2005 Gentoo Foundation 
# Distributed under the terms of the GNU General Public License v2

# run after local so that the boot time splash image
# is visible for as long as possible
depend() {
	need local
}

start() {
	source /etc/conf.d/splash
	[[ -z "${SPLASH_THEME}" ]] && SPLASH_THEME="default"                                                                             
	if [[ -z "${SPLASH_TTYS}" ]]; then
		if [ -n "${RC_TTY_NUMBER}" ]; then	
			SPLASH_TTYS=$(seq 1 "${RC_TTY_NUMBER}")
		else
			SPLASH_TTYS="1 2 3 4 5 6"
		fi
	fi

	# only do this if the kernel supports fbsplash
	if [[ -e /dev/fbsplash ]]; then
		ebegin "Setting framebuffer console images"
		splash_setup
		
		for TTY in ${SPLASH_TTYS} ; do
			theme="${SPLASH_THEME}"

			[[ ${TTY} == "1" && -z "$(/sbin/splash_util -c getstate --tty=${TTY}| grep 'off')" ]] && continue
			[[ ${TTY} == "0" ]] && continue

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

			if [[ -x /usr/bin/openvt ]]; then
				/usr/bin/openvt -c ${TTY} echo -n "" 2>/dev/null
			fi

			/sbin/splash_util --tty="${TTY}" -m v -t "${theme}" -c setcfg 2>/dev/null 
			/sbin/splash_util --tty="${TTY}" -m v -t "${theme}" -c setpic 2>/dev/null
			/sbin/splash_util --tty="${TTY}" -c on 2>/dev/null
		done

		eend $? "Failed to set framebuffer console images"
	fi
}
