#!/bin/bash
#
# splash - script to paint progress bar during 
# system startup/shutdown. This script is solely run
# by the init scripts (shell function splash_update)
#
# (w) 2002-2003 Stefan Reinauer, <stephan@suse.de>
# It's licensed under GPL, of course.
#
# modified by Michael Aichler, <micha at aichler dot net>
# 
# this script expects the following environment variables:
#	pb_init = the current status of the progress bar
#	pb_count = number of currently executed start/stop script
#	pb_scripts = number of scripts to be executed for runlevel change
#	pb_rate = the progress bar rate of the current boot process
#	RUNLEVEL = runlevel to be reached
#
# execute splash binary utility because we are a wrapper
#
if [ "$1" == "-s" -o "$1" == "-u" -o "$1" == "-n" -o "$1" == "-f" ]; then
	exec /sbin/splash.bin $*
fi

function box() { true; }

#
# assertions
#
test -r /proc/splash || exit 1
test -z "`cat /proc/splash 2>/dev/null | grep on`" && exit 1
test -z "$pb_count" -a -z "$pb_scripts" && DBZ="yes"
test "$RUNLEVEL" == "6" -o "$RUNLEVEL" == "0" && SHUTDOWN="yes"
#
# Source bootsplash config file
#
test -f /etc/conf.d/bootsplash.conf && . /etc/conf.d/bootsplash.conf
test -z "$BOOTSPLASH_THEME" && BOOTSPLASH_THEME="default"
test -z "${BOOTSPLASH_TTYS}" && BOOTSPLASH_TTYS="0"
#
# Get theme name from kernel command line
#
for param in `grep "theme=" /proc/cmdline`; do
	if [ "${param%=*}" == "theme" ]; then
		BOOTSPLASH_THEME="${param#*=}"
	fi
done
#
# Source theme config file
#
if [ ! -x /sbin/fbresolution ]; then
  echo "$0: '/sbin/fbresolution' does not exist on your system... aborting !"
else
  res="$(/sbin/fbresolution)"
fi

config="/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${res}.cfg"

if [ ! -f "$config" ]; then
  echo "$0: Can't find config file '$config'... aborting !"
  exit 1 
else
  source "$config"
fi

#
# void splash_start (const char* config_path)
#
splash_start() {
	/sbin/splash -s -u 0 "$1"
	echo "silent" > /proc/splash 
	/usr/bin/chvt 1  
}
#
# void splash_stop (const char* config_path)
#
splash_stop() {
	/sbin/bootanim kill 
	/sbin/splash -s -u 0 -n "$1" 
	exit 0
}
#
# void print_text (void)
#
print_text() {
	text="Booting the system... Press F2 for verbose mode"
	[ -n "${PROGRESS_BOOT_MESSAGE}" ] && text="${PROGRESS_BOOT_MESSAGE}"

	if [ "${SHUTDOWN}" == "yes" ]; then
		text="Shutting down the system... Press F2 for verbose mode"
		[ -n "${PROGRESS_SHUTDOWN_MESSAGE}" ] && text="${PROGRESS_SHUTDOWN_MESSAGE}"

		if [ "${RUNLEVEL}" == "6" ]; then
			text="Rebooting the system... Press F2 for verbose mode"
			[ -n "${PROGRESS_REBOOT_MESSAGE}" ] && text="${PROGRESS_REBOOT_MESSAGE}"
		fi
	fi

	if [ -n "$text_x" -a -n "$text_y" -a -n "$text_color" -a -n "$text_size" ]
	then
		options="-x $text_x -y $text_y -t $text_color -s $text_size"
		if [ -n "${BOOTSPLASH_FONT}" -a -f "/etc/bootsplash/${BOOTSPLASH_FONT}" ]
		then
			options="$options -f \"/etc/bootsplash/${BOOTSPLASH_FONT}\""
		fi
			
		eval "/sbin/fbtruetype.static $options \"$text\""
	fi
}
# 
# void paint_progress (void)
#
paint_progress() {
	if [ "$DBZ" != "yes" -a -n "$progress_enable" ]; then
		progress=$(($pb_init + ($pb_count + 1) * ($pb_rate - $pb_init) / $pb_scripts))
		echo "show $progress" > /proc/splash
	fi
}


#
# Start silent splash on system shutdown/reboot
#
if [ "$*" == "start" ]; then
	if [ -n "`grep \"splash=silent\" /proc/cmdline 2>/dev/null`" ]; then
		splash_start "$config"
		paint_progress
		print_text
	fi
#
# Stop on errors
#
elif [ "$*" == "stop" ]; then
	if [ "${BOOTSPLASH_STOP_ON_ERROR}" == "yes" ]; then
		splash_stop "$config"
	fi
#
# Print the currently used theme
#
elif [ "$*" == "theme" ]; then
	echo "$BOOTSPLASH_THEME"
#
# Print the currently used config file
#
elif [ "$*" == "config" ]; then
	echo "$config"
#
# Show boot/shutdown/reboot message
#
elif [ "$*" == "text" ]; then
	print_text
#
# Set verbose splash and fade out animations
# if booting has finished
#
elif [ "$*" == "master" ]; then
	echo "show $pb_rate" > /proc/splash	
	/sbin/bootanim stop && sleep 1
	for tty in ${BOOTSPLASH_TTYS}; do
		/sbin/splash -s -u $tty -n $config
	done
else
	#
	# Do not display anything if F2 was pressed. 
	#
	if [ -z "`grep \"silent\" /proc/splash 2> /dev/null`" ]; then
		if [ -n "`/bin/ps ax | grep fbmngplay | grep -v grep`" ]; then
			splash_stop
		fi
		exit 0
	fi
	#
	# Start animations here, if there's any config
	# (because of a hard coded path in splash.c)
	#
	if [ -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/animations.cfg ]; then
		while read line; do
			if [ "${line%:*}" == "$*" ]; then
				eval "${line#*:}"
			fi
		done < /etc/bootsplash/${BOOTSPLASH_THEME}/config/animations.cfg
	fi
	#
	# Print text string and paint progress bar
	#
	paint_progress
fi

exit 0
