#!/bin/sh
#
# Copyright (c) 2008 ICS Solution
# Authors: Sergi Miranda <sergi@icssolution.com>, 2008
#
#
# /etc/init.d/netoffice
#
### BEGIN INIT INFO
# Provides:          NetOffice
# Required-Start:    $local_fs $remote_fs $network 
# Should-Start:      $network $named $time
# Should-Stop:       $network $named $time
# Required-Stop:     $local_fs $remote_fs $network 
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the NetOffice daemon
# Description:       Controls the NetOffice daemon 
### END INIT INFO
#
set -e
set -u
${DEBIAN_SCRIPT_DEBUG:+ set -v -x}

#test -x java -cp /usr/local/NetOffice/ NetOffice || exit 0
export REDIRECT=/dev/null
. /lib/lsb/init-functions

SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)

# priority can be overriden and "-s" adds output to stderr
ERR_LOGGER="logger -p daemon.err -t /etc/init.d/netoffice -i"

# Safeguard (relative paths, core dumps..)
cd /
umask 077


## Checks if there is a server running and if so if it is accessible.
#
#
# Usage: boolean netoffice_status [check_alive|check_dead] [warn|nowarn]
netoffice_status () {
    ping_output=`nc -z 127.0.0.1 20000`; 
    ping_alive=`expr match "$ping_output" 'Connection'`

    if [ "$1" = "check_alive"  -a  $ping_alive = 10 ] ||
       [ "$1" = "check_dead"   -a  $ping_alive = 0 ]; then
  		return 0 # EXIT_SUCCESS
    else
		return 1 # EXIT_FAILURE
    fi
}

#
# main()
#

cd /usr/local/NetOffice

case "${1:-''}" in
  'start')
	# Start daemon
	echo -n "Starting NetOffice"
	if netoffice_status check_alive nowarn; then
	   echo ". NetOffice is already running"
	   rc_status -v
	else
	    case "${2:-''}" in
		'no_hacer_sudo')
		     cd /usr/local/NetOffice
		     java -cp /usr/local/NetOffice/ NetOffice > /dev/null 2>&1 &
		    ;;    
		*)
  	             su_out=`su www-data -c "/usr/local/NetOffice/start_netoffice"`
		     ;;
	    esac
		
	    # 6s was reported in #352070 to be too few when using ndbcluster
	    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do
                sleep 1
	        if netoffice_status check_alive nowarn ; then break; fi
		echo -n "."
	    done
	    if netoffice_status check_alive warn; then
                rc_status -v
	    else
	        rc_status -v1
		log_failure_msg "Please take a look at the syslog"
	    fi
	fi
	;;
  'stop')
	echo -n "Stopping NetOffice"
	if netoffice_status check_alive nowarn; then
   	    shutdown_out=`nc 127.0.0.1 20000 < /usr/local/NetOffice/apagar.txt`
	    for i in 1 2 3 4 5 6 7 8 9 10; do
               sleep 1
               if netoffice_status check_dead nowarn; then 
		  server_down=1;
		  break; 
	        fi
            done
	fi
        if ! netoffice_status check_dead warn; then
		  rc_status -v1
		  log_failure_msg ". Please stop NetOffice manually"
		  exit -1
	else
		echo "."
		rc_status -v
        fi
	;;

  'restart')
	set +e; $SELF stop; set -e
	$SELF start 
	;;

  'status')
	if netoffice_status check_alive nowarn; then
	  log_action_msg "NetOffice is running."
	else
	  log_action_msg "NetOffice is stopped."
	  exit 3
	fi
  	;;

  *)
	echo "Usage: $SELF start|stop|restart|status"
	exit 1
	;;
esac


# Inform the caller not only verbosely and set an exit status.
rc_exit
