#!/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
	OTHER=1
	RETVAL=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
elif [ $SUSE ]; then
	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=10
    echo -n "${display} "
    HOME=/root
    export HOME
    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=$?
    
    if [ $SUSE ] || [ $REDHAT ]; then
	    [ "$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
	fi
}

stop() {
    echo -n $"Stopping $PROG: "
    display=10
    echo -n "${display} "
    HOME=/root
    export HOME
    $INSTALLDIR/system_monitor/remote_console/vncserver -kill :10 >/dev/null 2>&1 
	    
    RETVAL=$?
    
    if [ $SUSE ] || [ $REDHAT ]; then
	    [ "$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
	fi
}

# Perform called operation.
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=$?
	elif [ $SUSE ]; then
		checkproc $PROG
		rc_status -v
	else
		tpid=`ps -ef | grep "$INSTALLDIR/system_monitor/remote_console" | awk '{print $2}'`	#get pid of smad, step 1
		tpid=`echo $tpid | awk '{print $1}'`	#get pid of smad, step 2
		ps $tpid | grep "$INSTALLDIR/system_monitor/remote_console" > /dev/null 2> /dev/null	#make sure the pid is not another process
		if [ "$?" == "0" ]; then #this means the pid is in fact smad, so report running status
			echo -e "$0\033[1;32m            [running]\033[0m"
			RETVAL=0
		else
			echo -e "$0\033[1;31m            [stopped]\033[0m"
			RETVAL=1
		fi
	fi
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
	;;
esac

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