#!/bin/bash
###########################################################################
# Eole NG - 2007
# Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon)
# Licence CeCill  cf /root/LicenceEole.txt
# eole@ac-dijon.fr
#
# zephir_web
#
# script de démarrage du service web Zephir
#
###########################################################################
#
### BEGIN INIT INFO
# Provides:          zephir_web
# 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: serveur web zephir
# Description:     : interface web de l'application de gestion de parc zephir
### END INIT INFO
#
# Author:	Equipe Eole <eole@ac-dijon.fr>.
#
## 28/02/2006
#

NAME=zephir_web
DESC="Application web Zéphir"
DAEMON=/usr/bin/twistd
DAEMON_ARGS="-o zephir_web"
RUNLEVEL=3
PIDFILE=/var/run/zephir_web.pid
LOCKFILE=/var/lock/zephir_web
RUNPATH=/usr/share/zephir/
##

. /lib/lsb/init-functions

[ -f /etc/eole/config.eol ] || exit 0

cd $RUNPATH

if [ ! -f /usr/lib/python2.7/dist-packages/twisted/plugins/eole_zephir.py ]
then
    echo -e "le service n'est pas installé.\n"
    log_end_msg 1
fi

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 2 ]; 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
}

do_start()
{
    log_daemon_msg "Starting $DESC" "$NAME"
	start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start -d $RUNPATH --oknodo --exec $DAEMON \
        -- --pidfile $PIDFILE --syslog $DAEMON_ARGS > /dev/null
    touch $LOCKFILE
    do_wait 'start'
    [ $? -eq 0 ] || return 2
    return 0
}

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 --oknodo --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
    rm -f /var/spool/uucp/lock &>/dev/null
	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)
        sleep 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
