#! /bin/sh
#
# Author:	Matt Zimmerman <mdz@ubuntu.com>
#
### BEGIN INIT INFO
# Provides:          ltsp-client ltsp-client-core
# Required-Start:    ltsp-client-setup $remote_fs $network $syslog
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      
# Short-Description: Script for LTSP client initialization
# Description:
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="LTSP client"
NAME=ltsp-client
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if ltsp_chroot file is not present
test -f /etc/ltsp_chroot || exit 0

# handle localization settings
if [ -r /etc/default/locale ]; then
  . /etc/default/locale
  export LANG LANGUAGE
elif [ -r /etc/environment ]; then
  . /etc/environment
  export LANG LANGUAGE
fi

. /lib/lsb/init-functions
. /usr/share/ltsp/ltsp-init-common

case "$1" in
  start)
        log_action_begin_msg "Starting LTSP client..."

        if [ -f "/etc/ltsp/getltscfg-cluster.conf" ]; then
            # Tell the control center that we are booting and get lts.conf
            eval $(getltscfg-cluster -a -l boot) || true
        fi

        # if usplash is running, make sure to stop it now 
        # (yes "start" is the right arg to do it ... even it looks silly)
        if pidof usplash > /dev/null; then
            usplash=:
            orig_console="$(fgconsole)"
            /etc/init.d/usplash start
            # We've just shut down usplash, so don't log success as it will
            # look weird on the console.
            log_action_end_msg=:
        else
            usplash=false
            log_action_end_msg=log_end_msg
        fi

        for screen in 01 02 03 04 05 06 07 08 09 10 11 12; do
            eval num=\$SCREEN_$screen
            if [ -n "$num" ]; then
                start-stop-daemon --start -b --exec /usr/share/ltsp/screen_session -- "$screen"
            fi
        done

        start_sound || true
        configure_sound_volume || true

        log_action_end_msg 0

        if $usplash && [ "$orig_console" != serial ]; then
            # Wait a short while for the active console to change, to try to
            # avoid visible console noise from later init scripts.
            i=0
            while [ "$(fgconsole)" = "$orig_console" ]; do
                i="$(($i + 1))"
                if [ "$i" -gt 5 ]; then
                    break
                fi
                sleep 1
            done
        fi
	;;
  stop)
#	echo -n "Stopping $DESC: $NAME"
#	d_stop
#	echo "."
	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: $NAME"
	d_stop
	sleep 1
	d_start
	echo "."
	;;
  *)
	# echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
