#!/bin/bash
#
# chkconfig: - 91 35
# description: Starts and stops vncserver. \
#	       used to provide remote X administration services.

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

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

LOCKFILE=/var/lock/subsys/vncserver

SourceIfNotEmpty /etc/sysconfig/vncservers

prog=$"VNC server"

start()
{
    echo -n $"Starting $prog: "
    ulimit -S -c 0 >/dev/null 2>&1
    RETVAL=0
    for display in ${VNCSERVERS}
    do
        echo -n "${display} "
        initlog $INITLOG_ARGS -c \
            "su - ${display##*:} -c \"cd ~${display##*:} && [ -f .vnc/passwd ] && vncserver :${display%%:*}\""
        RETVAL=$?
        [ "$RETVAL" -ne 0 ] && break
    done
    [ "$RETVAL" -eq 0 ] && success $"vncserver startup" || \
        failure $"vncserver start"
    echo
    [ "$RETVAL" -eq 0 ] && touch "$LOCKFILE"
}

stop()
{
    echo -n $"Shutting down $prog: "
    for display in ${VNCSERVERS}
    do
        echo -n "${display} "
	unset BASH_ENV ENV
        initlog $INITLOG_ARGS -c \
	    "su - ${display##*:} -c \"vncserver -kill :${display%%:*} >/dev/null 2>&1\""
    done
    RETVAL=$?
    [ "$RETVAL" -eq 0 ] && success $"vncserver shutdown" || \
        failure $"vncserver shutdown"
    echo
    [ "$RETVAL" -eq 0 ] && rm -f "$LOCKFILE"
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status vncserver
		RETVAL=$?
		;;
	restart|reload)
		restart
		;;
	condstart)
		if [ ! -e "$LOCKFILE" ]; then
			start
		fi
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|status|reload|restart|condstart|condstop|condrestart}"
		RETVAL=1
esac

exit $RETVAL
