#! /bin/bash
###########################################################################
# Eole NG - 2007
# Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon)
# Licence CeCill  cf /root/LicenceEole.txt
# eole@ac-dijon.fr
#
# eole-sso
#
# script d'initialisation du service eole sso
#
###########################################################################
#
### BEGIN INIT INFO
# Provides:          eole-sso
# 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: eole-sso initscript
# Description:       demarrage/arret du SSO Eole
### END INIT INFO
#
# Author:	Equipe Eole <eole@ac-dijon.fr>.
#

set -e

NAME=eole-sso
SCRIPTNAME=/etc/init.d/$NAME
PIDFILE=/var/run/authform.pid
DESC="Serveur d'authentification"
DAEMON=/usr/bin/eole-sso
LOGFILE=/var/log/eole-sso.log
DAEMON_OPTIONS="-oy /usr/share/sso/authform.py --syslog --pidfile $PIDFILE"
RUN_PATH=/usr/share/sso
# variables pour l'internationalisation
if [ -r /etc/default/locale ]; then
        . /etc/default/locale
        export LANG
        LC_ALL=$LANG
        export LC_ALL
fi

# encoding pour PYTHON
export PYTHONIOENCODING=UTF-8

. /lib/lsb/init-functions

# Gracefully exit if the package has been removed.
if [ ! -f /usr/bin/eole-sso ];then
    gprintf "le service d'authenfication n'est pas installé\n"
    exit -1
fi

cd $RUN_PATH

#
#	Function that starts the daemon/service.
#
d_start() {

    log_daemon_msg "Starting $DESC" "$NAME"
    if start-stop-daemon --start -d $RUN_PATH --oknodo --exec $DAEMON -- $DAEMON_OPTIONS >>$LOGFILE 2>&1; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
}

#
#	Function that reloads service configuration
#
d_reload() {
	if [ -f $PIDFILE ]
	then
		log_begin_msg "Rechargement de la configuration ..."
		/usr/bin/eole-sso --reload >>/var/log/eole-sso.log 2>&1
		log_end_msg $?
	else
		log_warning_msg "le service $DESC est arrêté"
		exit 1
	fi
}

#
#	Function that stops the daemon/service.
#
d_stop() {
    log_daemon_msg "Stopping $DESC" "$NAME"
    if start-stop-daemon --stop --quiet --oknodo -d $RUN_PATH --pidfile $PIDFILE; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
}

case "$1" in
  start)
	d_start
	;;
  stop)
	d_stop
	;;
  reload)
	d_reload
	;;
  restart|force-reload)
	d_stop
	sleep 2
	d_start
	;;
  status)
	status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|reload|restart|force-reload}" >&2
	exit 3
	;;
esac

exit 0
