#!/bin/sh
### BEGIN INIT INFO
# Provides:       bootsplash
# Required-Start: $remote_fs
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:
# Description:    Boot Splash screen setup
### END INIT INFO

# based on initscript from gentoo linux
# made some changes to fit normal sysvinit like used by debian
# everything in here is GPL ;)
# tcs@double-action.org

SCRIPTNAME='/etc/init.d/bootsplash'

# Only do this if the kernel has support
[ -r /proc/splash ] || exit 0
# Only do this if 'splash' exists
SPLASHBIN=$(which splash) || exit 0
# Only do this if 'fbresolution' exists
FBRES=$(which fbresolution) || exit 0

# source our config
[ -r /etc/default/bootsplash ] && . /etc/default/bootsplash

# source rcS configuration variables
[ -r /etc/default/rcS ] && . /etc/default/rcS

. /lib/lsb/init-functions

# default settings
THEME="${THEME:-current}"
BOOTSPLASH_TTYS="${BOOTSPLASH_TTYS:-$(seq 0 5)}"

get_bootsplash_theme () {
	# try to get bootsplash theme from kernel command line
	CMDLINE=$(cat /proc/cmdline)

	( echo $CMDLINE | grep -qe 'theme=.*' ) || return 1
	
	for param in $CMDLINE; do
		if [ "${param%=*}" = "theme" ]; then
			THEME="${param#theme=}"
			return 0
		fi
	done
	
	return 1
}

set_bootsplash_image () {

    TTY="$1"
    
    [ -z "$TTY" ] && return 1

    # get console resolution
    RESOLUTION=$($FBRES)
    
    # support for a different config per virtual terminal
    if [ -r /etc/bootsplash/themes/${THEME}/config/vtsplash-${TTY}-${RESOLUTION}.cfg ]
    then
	$SPLASHBIN -s -u $TTY -n /etc/bootsplash/themes/${THEME}/config/vtsplash-${TTY}-${RESOLUTION}.cfg
	return 0
    elif [ -r  /etc/bootsplash/themes/${THEME}/config/bootsplash-${RESOLUTION}.cfg ]
    then
	$SPLASHBIN -s -u $TTY -n /etc/bootsplash/themes/${THEME}/config/bootsplash-${RESOLUTION}.cfg
	return 0
    fi

    return 1
}

case "$1" in
    start)
	log_action_begin_msg "Setting Console frame buffer images"
	get_bootsplash_theme
	log_action_cont_msg "using theme '$THEME'"

	# switch to a usable image on all consoles
        for TTY in $(echo "${BOOTSPLASH_TTYS}" | sed -e 's# #\n#g')
	do
	    set_bootsplash_image $TTY
        done
		
	log_action_end_msg 0
    ;;
    
    stop)
        # Stop doesn't really stop, it actually changes the image
	# on vt1 back to the bootsplash image.

        CHVT_EXISTS='yes'
	CHVT=$(which chvt) || CHVT_EXISTS='no'

        log_action_begin_msg "Setting Console frame buffer images"
	get_bootsplash_theme
        log_action_cont_msg "using theme '$THEME'"

	$SPLASHBIN -s -u 0 /etc/bootsplash/themes/${THEME}/config/bootsplash-$(/sbin/fbresolution).cfg

        # ensure "silent" image is displayed
        echo "silent" > /proc/splash

        # switch to vt1 (must be &'d otherwise script hangs)
        [ "$CHVT_EXISTS" = 'yes' ] && ${CHVT} 1 &

        # print a nice message
        # /sbin/fbtruetype ........

        log_action_end_msg 0
    ;;

    restart|force-reload)
	:
    ;;

    *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-restart}" >&2
        exit 3
    ;;
esac

:
