#!/bin/sh
#
# ltsp-client   Start/Stop the LTSP client.
#
# chkconfig: 2345 45 05
# description: LTSP client initialization
# config: /usr/lib/ltsp/ltsp_config
# pidfile: /var/run/ltsp-client.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# Source configuration.
SourceIfNotEmpty /etc/sysconfig/ltsp-client

LOCKFILE=/var/lock/subsys/ltsp-client
RETVAL=0

if [ -s /usr/lib/ltsp/ltsp_functions -a -f /etc/ltsp_chroot ]; then
	. /usr/lib/ltsp/ltsp_functions
	SourceIfNotEmpty /usr/lib/ltsp/ltsp_config
else
	exit 0
fi

start_sound()
{
        if get_boolean "$SOUND"; then
		AUDIO_SERVER="none"
		AUDIO_PORT="none"
		case "$SOUND_DAEMON" in
    			esd|esound)
				if [ -x /usr/bin/esd ]; then
					# load ALSA's OSS-compat module if needed
					grep -q '^snd_pcm ' /proc/modules && modprobe snd-pcm-oss
					AUDIO_PORT="16001"
					#/usr/bin/esd -nobeeps -tcp -port $AUDIO_PORT -public &
					/usr/bin/esd -nobeeps -tcp -port $AUDIO_PORT -bind 0.0.0.0 &
					AUDIO_SERVER="ESOUND"
				fi
    	    			;;
    			nas|nasd|'') # The default when no value is set
				if [ -x /usr/bin/nasd ]; then
					# load ALSA's OSS-compat module if needed
					grep -q '^snd_pcm ' /proc/modules && modprobe snd-pcm-oss
					AUDIO_PORT="8000"
					/usr/bin/nasd -aa -b
    					# Line copied from old LTSP:  Should we use it? [pere 2006-03-03]
    					#aumix-minimal -v100 -w100 -c90 -m10
					AUDIO_SERVER="NAS"
				fi
    	    			;;
    			*)
    	    			echo "Unable to start unsupported sound daemon: '$SOUND_DAEMON'" >&2
    	    		;;
    		esac
		if get_boolean "$INFO"; then
		    set_info AUDIO_SERVER $AUDIO_SERVER
		    set_info AUDIO_PORT $AUDIO_PORT
		fi
	fi
}

start_info()
{
        get_boolean "$INFO" && [ -x /usr/sbin/ltspinfod ] && /usr/sbin/ltspinfod
}

start_printer()
{
	[ -n "$PRINTER_0_DEVICE" ] && /usr/lib/ltsp/start_printer 0 &
	for printer_device in `env | egrep '^PRINTER_[[:digit:]]+_DEVICE'`; do
		pnum=`echo $printer_device | cut -d_ -f2`
		pdevice=`echo $printer_device | cut -d= -f2`
		[ -n "$pdevice" ] && /usr/lib/ltsp/start_printer $pnum &
	done
}

start()
{
	msg_starting $"LTSP client"
	touch "$LOCKFILE"
	start_info || true
	start_printer || true
        start_sound || true
	for screen in $(env | grep '^SCREEN_' | cut -d= -f1); do
            start-stop-daemon --start -b --exec /usr/lib/ltsp/screen_session -- "${screen##SCREEN_}"
        done
        RETVAL=$?
        return $RETVAL
}

stop()
{
	rm -f "$LOCKFILE"
}

restart()
{
	stop
	start
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		[ -e "$LOCKFILE" ] && stop
		;;
	condreload|condrestart)
		[ -e "$LOCKFILE" ] && restart
		;;
	status)
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
		;;
esac

exit $RETVAL
