#!/bin/sh
#
# Copyright 2006-2009 by Purdue University.
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

#
# start/stop VNC proxy daemon.
#
### BEGIN INIT INFO
# Provides:          vncproxy
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Runs the VNC proxy.
# Description:       Starts the VNC proxy daemon so that VNC clients
#                    can be routed to backend systems.
### END INIT INFO

if ! [ -x /usr/lib/mw/vncproxy/vncproxy.py ]; then
	exit 0
fi

chkrunning() {
  pid=""
  if [[ -f /var/run/vncproxy.pid ]]
  then
    pid=$(cat /var/run/vncproxy.pid)
  fi
  if [[ "x${pid}x" != "xx" ]]
  then
    list=$(pidof python)
    for x in $list
    do
      if [[ $pid == $x ]]
      then
        echo "ERROR: VNC proxy appears to be running already."
        exit 1
      fi
    done
  fi
}

case "$1" in
    start)
        chkrunning
	echo -n "Starting VNC proxy:"
	echo -n " vncproxy" ; start-stop-daemon --start --quiet --pidfile /var/run/vncproxy.pid --exec /usr/lib/mw/vncproxy/vncproxy.py
	echo "."
	;;
    stop)
	echo -n "Stopping VNC proxy:"
	echo -n " vncproxy" ; start-stop-daemon --stop --quiet --oknodo --signal INT --pidfile /var/run/vncproxy.pid
	echo "."
	;;
    restart)
	echo -n "Restarting VNC proxy:"
	echo -n " vncproxy"
	start-stop-daemon --stop --quiet --oknodo --signal INT --pidfile /var/run/vncproxy.pid
	start-stop-daemon --start --quiet --pidfile /var/run/vncproxy.pid --exec /usr/lib/mw/vncproxy/vncproxy.py
	echo "."
	;;
    *)
	echo "Usage: /etc/init.d/vncproxy {start|stop|restart}"
	exit 1
	;;
esac

exit 0

