#! /bin/sh
### BEGIN INIT INFO
# Provides:          z_stats
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: z_stats initscript
# Description:       moniteur zephir  demarrage/arret de l'outil
#		     de surveillance à distance de zephir
### END INIT INFO
#
# Author:	Equipe Eole <eole@ac-dijon.fr>.
#
# Version:	@(#)z_stats  1.0  13-Nov-2006  eole@ac-dijon.fr
#

# Do NOT "set -e"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PYTHONPATH=$PYTHONPATH:/usr/share/eole/
DESC="statistiques zephir"
NAME=z_stats
MONITOR_DIR=/usr/share/zephir/monitor
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
DAEMON=/usr/bin/twistd
#LOG_FILE=/var/log/zephir/agent.log
DAEMON_ARGS="--syslog --pidfile $PIDFILE -no zephiragents --config=configs --tmp=data --data=stats --archive=/tmp --static=static --actions=actions"
ModeTxt="yes"
export PYTHONPATH

. /lib/lsb/init-functions

# Gracefully exit if the package has been removed.
test -d $MONITOR_DIR || exit 0
cd /usr/share/zephir/monitor

do_kill() {
	local sig pid killretcode
	sig=$1
	pid=$2
	killretcode=$3
	[ -n "$pid" ] || return 1
	kill -$sig $pid 2>/dev/null
	[ $? -eq $killretcode ]
}

do_wait() {
	local n sig pid killretcode waitmsg
	if [ "$1" = 'start' ]; then
		killretcode=0
		waitmsg='start'
	elif [ "$1" = 'stop' ]; then
		killretcode=1
		waitmsg="die"
	fi
	if [ -f "$PIDFILE" ]; then
	    pid=$(cat $PIDFILE)
	elif [ "$1" = 'stop' ]; then
		return 0
	fi
	sig=0
	n=1
	while ! do_kill "$sig" "$pid" "$killretcode" ; do
		if [ $n -eq 1 ]; then
			echo "waiting for process to $waitmsg" >&2
		fi
		if [ "$action" = 'stop' -a $n -eq 11 ]; then
			echo "giving up on pid $pid with kill -0; trying -9"
			sig=9
		fi
		if [ $n -gt 20 ]; then
			echo "giving up on pid $pid"
			break
		fi
		n=$(($n+1))
		sleep .1
		if [ -z "$pid" -a -f "$PIDFILE" ]; then
		    pid=$(cat "$PIDFILE")
		fi
	done
}

#
#	Function that starts the daemon/service.
#
do_start() {
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	log_begin_msg "Starting $DESC: $NAME"
	start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON --test > /dev/null \
		|| return 1
	rm -f zephiragents*.tap
	rm -rf data/* >/dev/null 2>&1
	# compilation des templates Cheetah
	# démarrage du service
	start-stop-daemon -b -d $MONITOR_DIR --start --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS
	do_wait 'start'
	if [ $? -eq 0 ]; then
		touch /var/lock/z_stats
		return 0
	else
		return 2
	fi
}

#
#	Function that stops the daemon/service.
#
do_stop() {
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	log_begin_msg "Stopping $DESC: $NAME"
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	# Wait for children to finish too if this is a daemon that forks
	# and if the daemon is only ever run from this initscript.
	# If the above conditions are not satisfied then add some other code
	# that waits for the process to drop all resources that could be
	# needed by services started subsequently.  A last resort is to
	# sleep for some time.
	do_wait 'stop'
	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --pidfile $PIDFILE
	[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	rm -f $PIDFILE
	return "$RETVAL"
}

case "$1" in
  start)
	do_start
	case "$?" in
		0|1) log_end_msg 0 ;;
		2) log_end_msg 1 ;;
	esac
	;;
  stop)
	do_stop
	case "$?" in
		0|1) log_end_msg 0 ;;
		2) log_end_msg 1 ;;
	esac
	;;
  status)
	status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
	;;
  restart|force-reload)
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
		# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

exit 0
