#!/bin/sh
############################################################################
#                                                                          #
#  Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com.           #
#                                                                          #
#  NXSERVER, NX protocol compression and NX extensions to this software    #
#  are copyright of NoMachine. Redistribution and use of the present       #
#  software is allowed according to terms specified in the file LICENSE    #
#  which comes in the source distribution.                                 #
#                                                                          #
#  Check http://www.nomachine.com/licensing.html for applicability.        #
#                                                                          #
#  NX and NoMachine are trademarks of Medialogic S.p.A.                    #
#                                                                          #
#  All rights reserved.                                                    #
#                                                                          #
############################################################################

# Basic support for RedHat style chkconfig
#
# chkconfig: - 99 01
# description: Starts and stops the NoMachine NX Server.
#
### BEGIN INIT INFO
# Provides:       nxserver
# Required-Start: $remote_fs $all
# Should-Start:
# Required-Stop:
# Default-Start:  2 3 5
# Default-Stop:   0 6
# Description:    Starts and stops the NoMachine NX Server.
### END INIT INFO

NODE_ROOT='/usr/NX'

[ -f $NODE_ROOT/bin/nxserver ] || { echo "ERROR: $NODE_ROOT/bin/nxserver doesn't exist" ; exit 1; }

start()
{
  echo "Trying to start NX server:"
  $NODE_ROOT/bin/nxserver --start
  echo "Trying to start NX statistics:"
  $NODE_ROOT/bin/nxserver --statistics start
}

stop()
{
  echo "Trying to stop NX server:"
  $NODE_ROOT/bin/nxserver --stop
  echo "Trying to stop NX statistics:"
  $NODE_ROOT/bin/nxserver --statistics stop
}

restart()
{
  echo "Trying to restart NX server:"
  $NODE_ROOT/bin/nxserver --restart
  echo "Trying to restart NX statistics:"
  $NODE_ROOT/bin/nxserver --statistics restart
}

case "$1" in
'start')
  start
  if [ -d /var/lock/subsys ]; then
    touch /var/lock/subsys/nxserver
  elif [ -d /var/lock ]; then
    touch /var/lock/nxserver
  fi
  ;;

'stop')
  if [ ! -f $NODE_ROOT/home/nx/.ssh/authorized_keys2 ] && [ ! -f $NODE_ROOT/home/nx/.ssh/authorized_keys ];
  then
    echo "Service was already stopped"
  else
    stop
    if [ -d /var/lock/subsys ]; then
      rm -f /var/lock/subsys/nxserver
    elif [ -d /var/lock ]; then
      rm -f /var/lock/nxserver
    fi

  fi
  ;;

'restart')
  if  [ ! -f $NODE_ROOT/home/nx/.ssh/authorized_keys2 ] && [ ! -f $NODE_ROOT/home/nx/.ssh/authorized_keys ] ;
  then
    echo "WARNING: Service was already stopped, trying to start"
    start
  else
    restart
  fi  
  ;;
*)
  echo "Usage: $0 {start|stop|restart}"
  exit 1
  ;;
esac
