#!/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=${VNCSERVERS}
    echo -n "${display} "
    unset BASH_ENV ENV
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$INSTALLDIR/system_monitor/agent/"
    $INSTALLDIR/system_monitor/remote_console/Xtightvnc :10 -desktop X -httpd $INSTALLDIR/system_monitor/remote_console/class -httpport 5800 -auth /root/.Xauthority -geometry 1024x768 -depth 16 -rfbwait 120000 -rfbauth $INSTALLDIR/system_monitor/remote_console/.vnc/passwd -rfbport 5910 -fp /usr/X11R6/lib/X11/fonts/Type1/,/usr/X11R6/lib/X11/fonts/Speedo/,/usr/X11R6/lib/X11/fonts/misc/,/usr/X11R6/lib/X11/fonts/75dpi/,/usr/X11R6/lib/X11/fonts/100dpi/,/usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb & echo $! >$INSTALLDIR/system_monitor/remote_console/.vnc/syam.pid
	sleep 1
	DISPLAY=:10
	export DISPLAY
	VNCDESKTOP=X
	export VNCDESKTOP
	$INSTALLDIR/system_monitor/remote_console/.vnc/xstartup >> $INSTALLDIR/system_monitor/remote_console/.vnc/syam.log 2>&1 &
	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=${VNCSERVERS}
    echo -n "${display} "
    unset BASH_ENV ENV
    kill `cat $INSTALLDIR/system_monitor/remote_console/.vnc/syam.pid`
    
    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
