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

set -e
set -u
${DEBIAN_SCRIPT_DEBUG:+ set -v -x}

#test -x java -cp $DIR_NETOFFICE/ NetOffice || exit 0

. /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 $SCRIPT_ARRANQUE -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 () {
    `nc -z -v -w2 127.0.0.1 $SERVER_PORT >> /tmp/netoffice_status 2>&1`

    ping_output=`tail -n 1 /tmp/netoffice_status` 
    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

case "${1:-''}" in
  'start')
	# Start daemon
	log_daemon_msg "Starting NetOffice"
	if netoffice_status check_alive nowarn; then
	   echo ". NetOffice is already running"
	   log_progress_msg "already running"
	   log_end_msg 0
	else
	    case "${2:-''}" in
		'no_hacer_sudo')
		     cd $DIR_NETOFFICE
		     java -cp $DIR_NETOFFICE/ NetOffice > /dev/null 2>&1 &
		    ;;    
		*)
  	             su_out=`su www-data -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
		log_progress_msg "."
	    done
	    if netoffice_status check_alive warn; then
                log_end_msg 0
	    else
	        log_end_msg 1
		log_failure_msg "Please take a look at the syslog"
	    fi
	fi
	;;
  'stop')
	log_daemon_msg "Stopping NetOffice"
	if netoffice_status check_alive nowarn; then
   	  shutdown_out=`nc 127.0.0.1 $SERVER_PORT < $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
        if ! netoffice_status check_dead warn; then
		  log_end_msg 1
		  log_failure_msg "Please stop NetOffice manually"
		  exit -1
		fi
	else
		echo ". NetOffice already stopped!"
		log_end_msg 0
    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

