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

AddConfigHandler FBSplashOptions
AddConfigHelp "FBSplash <boolean>" "Showing script progress using fbsplash. No kernel patches are required, but you will need the fbsplash splashutils package installed (distinct from the bootsplash splashutils package). This will automatically enable SwitchToTextMode too."
AddConfigHelp "FBSplashTheme <themename>" "FBSplash theme name (default is taken from /proc/cmdline)"


# default fbsplash theme config file
FBSPLASH_THEME=""

FBSPLASH_FIFO="/lib/splash/cache/.splash"

TimedFBSplashCtl() {
    # Write to the splash daemon fifo, but don't let it take more than a
    # second (implying that there is no longer a splash daemon there).
    (
    (
    /bin/echo -ne "$@" > $FBSPLASH_FIFO &
    echo_pid=$!
    sleep 1
    kill -9 $echo_pid > /dev/null 2>&1
    ) < /dev/null > /dev/null 2>&1 &
    )
}

FBSplashProgress() {
    if [ x"$USE_FBSPLASH" = "x1" ] ; then
	FBSPLASH_PROGRESS=$(($FBSPLASH_PROGRESS+6553));
	TimedFBSplashCtl "progress $(($FBSPLASH_PROGRESS))\nrepaint\n"
    fi
    return 0
}

FBSplashBegin() {
    [ x"$USE_FBSPLASH" != "x1" ] && return 0

    [ -z "$FBSPLASH_THEME" ] && FBSPLASH_THEME="$(grep splash= /proc/cmdline > /dev/null && cat /proc/cmdline | sed 's/.*splash=/splash=/' | sed 's/ .*//' | sed 's/.*theme://' | sed 's/,.*//' || echo default)"

    # check if fbsplashd exists
    if ! command -v fbsplashd > /dev/null 2>&1 ; then
	USE_FBSPLASH=0
	vecho 1 "'fbsplashd' utility not found. fbsplash disabled."
	return 0
    fi

    # configfile exists
    if [ ! -d "/etc/splash/$FBSPLASH_THEME/" ]; then
	USE_FBSPLASH=0
	vecho 1 "config file not found. fbsplash disabled."
	return 0
    fi

    if ! test -p $FBSPLASH_FIFO ; then
	USE_FBSPLASH=0
	vecho 0 "Could not talk to splash daemon. fbsplash disabled."
	return 0
    fi

    # Start up the splash daemon.
    if ! fbsplashd --theme $FBSPLASH_THEME --mesg "Suspending..." --effects "fadein" ; then
	USE_FBSPLASH=0
	vecho 0 "Splash daemon failed to start. fbsplash disabled."
	return 0
    fi
    TimedFBSplashCtl "set mode silent\nprogress 0\nset message $@\nrepaint\n"

    # Set console loglevel to KERN_CRIT whilst suspending
    SAVED_FBSPLASH_PRINTK_LEVEL=`awk '{print $1}' < /proc/sys/kernel/printk`
    echo 2 > /proc/sys/kernel/printk

    # increments the bar to 10 and kicks it all off.
    FBSPLASH_PROGRESS=0
    FBSplashProgress

    return 0
}

FBSplashStartResume() {
    TimedFBSplashCtl "set tty silent 2\nset mode silent\nset theme $FBSPLASH_THEME\nrepaint\n"
    TimedFBSplashCtl "set message Reloading drivers and applications ...\nrepaint\n"
    FBSPLASH_PROGRESS=0
    FBSplashProgress
}

FBSplashStartSuspend() {
    FBSplashBegin "Preparing to suspend ..."
}

FBSplashOff() {
    TimedFBSplashCtl "set effects fadeout\n"
    TimedFBSplashCtl "exit\n"

    while killall -q -0 fbsplashd; do
    	sleep 1
    done

    [ -n "$SAVED_FBSPLASH_PRINTK_LEVEL" ] && \
	echo $SAVED_FBSPLASH_PRINTK_LEVEL > /proc/sys/kernel/printk

    return 0
}

FBSplashOptions() {
    case $1 in
	fbsplash)
	    BoolIsOn "$1" "$2" && USE_FBSPLASH=1 || return 0
	    # don't return. still stuff to do
	    ;;
	fbsplashtheme)
	    FBSPLASH_THEME="$2"
	    return 0
	    ;;
	*)
	    return 1
    esac

    if [ -z "$FBSPLASH_HOOKED" ] ; then
	# in call order
	AddSuspendHook 12 FBSplashStartSuspend
	AddSuspendHook 20 FBSplashProgress
	AddSuspendHook 30 FBSplashProgress
	AddSuspendHook 40 FBSplashProgress
	AddSuspendHook 50 FBSplashProgress
	AddSuspendHook 60 FBSplashProgress
	AddSuspendHook 70 FBSplashProgress
	AddSuspendHook 80 FBSplashProgress
	AddSuspendHook 90 FBSplashProgress
	AddSuspendHook 94 FBSplashProgress
	AddResumeHook 95 FBSplashStartResume
	AddResumeHook 90 FBSplashProgress
	AddResumeHook 80 FBSplashProgress
	AddResumeHook 70 FBSplashProgress
	AddResumeHook 60 FBSplashProgress
	AddResumeHook 50 FBSplashProgress
	AddResumeHook 40 FBSplashProgress
	AddResumeHook 30 FBSplashProgress
	AddResumeHook 20 FBSplashProgress
	AddResumeHook 15 FBSplashProgress # get it to 100% :)
	AddResumeHook 12 FBSplashOff
	FBSPLASH_HOOKED=1

	# Enable SwitchToTextMode too.
	XHacksOptions switchtotextmode 1
    fi

    return 0
}

# $Id$
