#!/bin/bash
#
# chkconfig: - 91 35
# description: Starts and stops Remote Console
#

### BEGIN INIT INFO
# Provides:          Xvnc
# Required-Start:    $network $syslog $local_fs $remote_fs
# Required-Stop:     $network $syslog $local_fs $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Xvnc: Remote Console 
# Description:	   Xvnc: Remote Console Service 
### END INIT INFO

# Redhat or SuSE?
if [ -f /etc/redhat-release ]; then
	REDHAT=1
	. /etc/init.d/functions
	RETVAL=0
elif [ -f /etc/SuSE-release ]; then
	SUSE=1
	. /etc/rc.status
	rc_reset
else
	echo "Unsupported distribution"
	exit 0
fi

if [ $REDHAT ]; then
	# Source networking configuration and check that networking is up.
	. /etc/sysconfig/network
	[ ${NETWORKING} = "no" ] && exit 0
	LOG_SUCCESS=success
	LOG_FAILURE=failure
else
	LOG_SUCCESS="logger -s"
	LOG_FAILURE="logger -s"
fi

INSTALLDIR=@@@install_path@@@
PROG=Xvnc
CURDIR=/root
VNCSERVERS="1:root"

start() {
echo -n $"Starting $PROG: "
    ulimit -S -c 0 >/dev/null 2>&1
    RETVAL=0
    display=${VNCSERVERS}
    echo -n "${display} "
    unset BASH_ENV ENV
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$INSTALLDIR/system_monitor/agent/"
    cd ~root && [ -f $INSTALLDIR/system_monitor/remote_console/.vnc/passwd ] && $INSTALLDIR/system_monitor/remote_console/vncserver :${display%%:*}
    RETVAL=$?
    [ "$RETVAL" -eq 0 ] && $LOG_SUCCESS $"Xvnc startup" || \
        $LOG_FAILURE $"vncserver start"
    echo
    [ "$RETVAL" -eq 0 ] && touch /var/lock/subsys/vncserver
    if [ $SUSE ]; then
	[ "$RETVAL" -eq 0 ]
	rc_status -v
    fi
}

stop() {
    echo -n $"Stopping $PROG: "
    display=${VNCSERVERS}
    echo -n "${display} "
    unset BASH_ENV ENV
    $INSTALLDIR/system_monitor/remote_console/vncserver -kill :${display%%:*} >/dev/null 2>&1
    RETVAL=$?
    [ "$RETVAL" -eq 0 ] && $LOG_SUCCESS $"Xvnc shutdown" || \
        $LOG_FAILURE $"vncserver shutdown"
    echo
    [ "$RETVAL" -eq 0 ] && rm -f /var/lock/subsys/vncserver
    if [ $SUSE ]; then
	[ "$RETVAL" -eq 0 ]
	rc_status -v
    fi
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	start
	;;
  condrestart)
	if [ -f /var/lock/subsys/vncserver ]; then
	    stop
	    start
	fi
	;;
  status)
	if [ $REDHAT ]; then
		status $PROG
		RETVAL=$?
	else
		checkproc $PROG
		rc_status -v
	fi
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
	;;
esac

if [ $REDHAT ]; then
	exit $RETVAL
else
	rc_exit
fi
