#!/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
#

DIR_NETOFFICE=/usr/local/NetOffice
SCRIPT_ARRANQUE=/etc/init.d/netoffice

SERVER_PORT=`cat $DIR_NETOFFICE/conf.no | grep serverPort | sed 's/serverPort:\([0-9]*\).*/\1/'`
if [ -z "$SERVER_PORT" ]; then
        SERVER_PORT=20000
fi



# Source function library.
. /etc/init.d/functions
DAEMON=netoffice
prog=netoffice

## 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 $SERVER_PORT`; 
    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 $DIR_NETOFFICE
RETVAL=1

case "${1:-''}" in
  'start')
	# Start daemon
	echo -n "Starting NetOffice"
	if netoffice_status check_alive nowarn; then
	   echo ". NetOffice is already running"
	   RETVAL=0
	else
	    case "${2:-''}" in
		'no_hacer_sudo')
		     cd $DIR_NETOFFICE
		     java -cp $DIR_NETOFFICE NetOffice > /dev/null 2>&1 &
		    ;;    
		*)
  	             su_out=`su apache -c "$DIR_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
		echo "."
		RETVAL=1
	    else
		log_failure_msg "Please take a look at the syslog"
		RETVAL=0
	    fi
	fi
	;;
  'stop')
	echo -n $"Stopping $prog:"
	if netoffice_status check_alive nowarn; then
   	    shutdown_out=`nc 127.0.0.1 20000 < $DIR_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
	    log_failure_msg ". Please stop NetOffice manually"
	    exit -1
	else
	    RETVAL=1
	    echo 
        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: $prog {start|stop|restart|condrestart|reload|status}"
        exit 3
	;;
esac
exit $RETVAL
