# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler SplashOptions
AddConfigHelp "Bootsplash <boolean>" "Showing script progress on an bootsplash enabled kernel. You will almost certainly need to enable LeaveXBeforeSuspend as well, else your screen might end up garbled."
AddConfigHelp "BootsplashConfig <configfile>" "Bootsplash config file (default is /etc/bootsplash/default/config/bootsplash-1024x768.cfg)"


# default bootsplash theme config file
SPLASH_CONFIG_FILE="/etc/bootsplash/default/config/bootsplash-1024x768.cfg"

SplashProgress() 
{
    if [ x"$USE_BOOTSPLASH" = "x1" ] ; then
	CURRENT_PROGRESS=$((65535*${SPLASH_PROGRESS}/100));
	echo "show $CURRENT_PROGRESS" > /proc/splash
	SPLASH_PROGRESS=$(($SPLASH_PROGRESS+10));
    fi
    return 0
}

SplashStart() {
    [ x"$USE_BOOTSPLASH" != "x1" ] && return 0

    # check if /proc/splash and splash exists 
    if [ ! -f /proc/splash ]; then
	USE_BOOTSPLASH=0
	vecho 1 "/proc/splash not found. Bootsplash disabled."
	return 0
    fi
    if ! which splash > /dev/null 2>&1 ; then
	USE_BOOTSPLASH=0
	vecho 1 "'splash' utility not found. Bootsplash disabled."
	return 0
    fi

    # configfile exists ?
    if [ ! -f "$SPLASH_CONFIG_FILE" ]; then
	USE_BOOTSPLASH=0
	vecho 1 "config file not found. Bootsplash disabled."
	return 0
    fi

    # Detect current splash state so we know how to restore it
    if grep -q 'off$' /proc/splash ; then
	SPLASH_RESTORE_CMD="0"
    else
	# We assume that their usual splash screen isn't silent.
	SPLASH_RESTORE_CMD="verbose"
    fi

    # go
    splash -s -u 0 "$SPLASH_CONFIG_FILE"
    echo "silent" > /proc/splash 

    # increments the bar to 10 and kicks it all off.
    SPLASH_PROGRESS=0
    SplashProgress
    
    return 0
}

SplashResume() {
    SplashStart
    # XXX add text output
}

SplashSuspend() {
    SplashStart
    # XXX add text output
}

SplashStop() {
    if [ x"$USE_BOOTSPLASH" = "x1" ] ; then
	echo $SPLASH_RESTORE_CMD > /proc/splash
    fi
    return 0
}

SplashOptions() {
    case $1 in
	bootsplash)
	    BoolIsOn "$1" "$2" && USE_BOOTSPLASH=1 || return 0
	    # don't return. still stuff to do
	    ;;
	bootsplashconfig)
	    SPLASH_CONFIG_FILE="$2"
	    return 0
	    ;;
	*)
	    return 1
    esac

    if [ -z "$BOOTSPLASH_HOOKED" ] ; then
	# in call order
	AddSuspendHook 12 SplashSuspend
	AddSuspendHook 20 SplashProgress
	AddSuspendHook 30 SplashProgress
	AddSuspendHook 40 SplashProgress
	AddSuspendHook 50 SplashProgress
	AddSuspendHook 60 SplashProgress
	AddSuspendHook 70 SplashProgress
	AddSuspendHook 80 SplashProgress
	AddSuspendHook 90 SplashProgress
	AddSuspendHook 97 SplashProgress
	AddResumeHook 97 SplashResume
	AddResumeHook 90 SplashProgress
	AddResumeHook 80 SplashProgress
	AddResumeHook 70 SplashProgress
	AddResumeHook 60 SplashProgress
	AddResumeHook 50 SplashProgress
	AddResumeHook 40 SplashProgress
	AddResumeHook 30 SplashProgress
	AddResumeHook 20 SplashProgress
	AddResumeHook 12 SplashStop
	BOOTSPLASH_HOOKED=1
    fi

    return 0
}

# $Id: bootsplash 347 2004-07-29 20:18:29Z dagobah $
