#!/bin/bash
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#
# splash - a script to handle rc_scripts events and execute appropriate
#          fbsplash actions; loosely based on bootsplash /sbin/splash
#
# Author: Michal Januszewski <spock@gentoo.org>
#

# Make sure we're running in a sane environment
[[ ! -x /sbin/splash_util.static ]] && exit 1

[[ -z ${SPLASH_BOOT_MESSAGE} ]] 	&& SPLASH_BOOT_MESSAGE="Booting the system (\$progress%)... Press F2 for verbose mode"
[[ -z ${SPLASH_SHUTDOWN_MESSAGE} ]] && SPLASH_SHUTDOWN_MESSAGE="Shutting down the system (\$progress%)... Press F2 for verbose mode"
[[ -z ${SPLASH_REBOOT_MESSAGE} ]] 	&& SPLASH_REBOOT_MESSAGE="Rebooting the system (\$progress%)... Press F2 for verbose mode"

[[ RC_GOT_FUNCTIONS != "yes" ]] && . /sbin/functions.sh
[[ -f /etc/conf.d/splash ]] 	&& . /etc/conf.d/splash
[[ -f /etc/conf.d/rc ]] 		&& . /etc/conf.d/rc
[[ ${RUNLEVEL} == "6" || ${RUNLEVEL} == "0" ]] && SHUTDOWN="yes"

SPLUTIL="/sbin/splash_util.static"

splash_setup 'force'

get_boot_message() {
	local text

	if [[ ${RUNLEVEL} == "6" ]]; then
		text=${SPLASH_REBOOT_MESSAGE}
	elif [[ ${RUNLEVEL} == "0" ]]; then
		text=${SPLASH_SHUTDOWN_MESSAGE}
	else
		text=${SPLASH_BOOT_MESSAGE}
	fi	

	echo ${text}
}

update_progress() {
	local srv=$1
		
	splash_load_vars	
	[[ -n "${spl_execed}" && -z "${spl_execed//* $srv */}" ]] && return
	[[ -z "${spl_scripts}" ]] && return
	spl_execed="${spl_execed} ${srv} "
	spl_count=$((${spl_count} + 1))
		
	# spl_init and spl_rate are deprecated and will probably be removed soon
	if [ "${spl_scripts}" -gt 0 ]; then
		progress=$(($spl_init + ($spl_count) * ($spl_rate - $spl_init) / $spl_scripts))
	else
		progress=0
	fi

	splash_comm_send "progress ${progress}"
	splash_save_vars
	
	if [[ "$(splash_get_mode)" == "silent" ]]; then
		local t=$(get_boot_message)
		splash_comm_send "paint"

		if [[ -z "${t//*\$progress*/}" && -e ${spl_cachedir}/message ]]; then
			source ${spl_cachedir}/message
			if [[ -n "${text_x}" && -n "${text_y}" && -n "${text_size}" ]]; then
				splash_comm_send "paint rect ${text_x} ${text_y} ${xres} $(($text_y+5*$text_size))"
			fi
		fi
	fi
}

# Start the splash daemon
if [ "$1" == "start" ]; then
	# Prepare the communications FIFO
	rm -f ${spl_fifo} 2>/dev/null
		
	if [[ ${SPLASH_MODE_REQ} == "verbose" ]]; then
		${SPLUTIL} -c on 2>/dev/null
		exit 0
	elif [[ ${SPLASH_MODE_REQ} != "silent" ]]; then
		exit 0
	fi
		
	# Display a warning if the system is not configured to display init messages
	# to tty1. This can cause a lot of problems if it's not handled correctly, so
	# we don't allow silent splash to run on incorrectly configured systems.
	if [[ ${SPLASH_MODE_REQ} == "silent" ]]; then
		if [[ -z "`grep -E '(^| )CONSOLE=/dev/tty1( |$)' /proc/cmdline`" ]]; then
			clear
			ewarn "You don't appear to have a correct CONSOLE= setting on your kernel"
			ewarn "command line. Silent splash will not be enabled. Please add"
			ewarn "CONSOLE=/dev/tty1 to your kernel command line to avoid this message."
			if [[ -n "`grep 'CONSOLE=/dev/tty1' /proc/cmdline`" ]]; then
				ewarn "Note that CONSOLE=/dev/tty1 is a general parameter and not a splash= setting."
			fi
			exit 1
		fi

		mount --bind / ${spl_tmpdir}
		if [[ ! -c "${spl_tmpdir}/dev/tty1" ]]; then
			umount ${spl_tmpdir}
			clear
			ewarn "The filesystem mounted on / doesn't contain the /dev/tty1 device"
			ewarn "which is required for the silent splash to function properly."
			ewarn "Silent splash will not be enabled. Please create the appropriate"
			ewarn "device file to avoid this message."
			exit 1
		fi
		umount ${spl_tmpdir}
	fi

	# In the unlikely case that there's a splash daemon running -- kill it.
	killall -9 ${SPLUTIL} 2>/dev/null
	
	# Prepare the communications FIFO
	mkfifo ${spl_fifo}	

	options=""
	[[ ${SPLASH_KDMODE} == "GRAPHICS" ]] && options="--kdgraphics"
		
	# Start the splash daemon
	${SPLUTIL} -d --theme="${SPLASH_THEME}" "${options}"

	# Get the PID of the daemon and save it
	pid=$(ps -o pid,ppid -C splash_util.static 2>/dev/null | grep '.* 1$' | awk '{ print $1 }')
	echo "${pid}" > ${spl_pidfile} 

	# Set the silent TTY and boot message
	splash_comm_send "set tty silent ${SPLASH_TTY}"
	splash_comm_send "set message $(get_boot_message)"
	
	if [[ ${SPLASH_MODE_REQ} == "silent" ]] ; then
		splash_comm_send "set mode silent"
		splash_comm_send "repaint"
		${SPLUTIL} -c on 2>/dev/null
	fi

	# Save some useful variables
	res=$(/lib/splash/bin/fbres)
	yres=${res#*x}
	xres=${res%x*}

	cat /etc/splash/${SPLASH_THEME}/${res}.cfg | grep "^text_[a-z]\+=" > ${spl_cachedir}/message
	echo -e "yres=${yres}\nxres=${xres}" >> ${spl_cachedir}/message
elif [ "$1" == "silent" ]; then
	splash_comm_send "set mode silent"
	${SPLUTIL} -c on 2>/dev/null
	update_progress
elif [ "$1" == "verbose" ]; then
	if [[ "$(splash_get_mode)" == "silent" ]]; then
		if [[ -x /usr/bin/chvt ]]; then
			/usr/bin/chvt 1
		else
			splash_comm_send "set mode verbose"
		fi
	fi
else
	update_progress "$1"
fi

exit 0

# vim:ts=4
