#!/bin/bash

function is_running() {
    state=$1
    container=$2
    error=0
    case $state in
        running)
            ;;
        initializing|starting)
            msg="Attention, serveur en cours de démarrage ($state)"
            error=1
            ;;
        stopping)
            msg="Attention, serveur en cours d'arrêt"
            error=1
            ;;
        degraded)
	    lstsrvs=$(CreoleRun 'systemctl --state=failed --no-legend' $container \
			  | awk '{print $1 " " $2 " " $3}')
            msg="Attention, serveur opérationnel mais des services ne sont pas démarrés"
			# Défaut d'indentation volontaire merci de ne pas y toucher.
			msg="${msg} :

${lstsrvs}
"
			# À partir d'ici tout défaut d'indentation est involontaire
            error=1
            ;;
        *)
            msg="Attention, état du serveur inconnu ($state)"
            error=1
    esac
    if [ $error = 1 ]; then
        . /usr/lib/eole/diagnose.sh
        if [ ! $container = 'root' ]; then
            msg="$msg dans le conteneur $container"
        fi
        EchoRouge "$msg"
    fi
}

is_running $(systemctl is-system-running) 'root'

if [[ $(CreoleGet mode_conteneur_actif) == "oui" ]]
then
    for grp in $(CreoleGet --groups); do
        if [[ ${grp} != 'root' ]] && [[ ${grp} != 'all' ]]; then
            is_running "$(CreoleRun 'systemctl is-system-running' $grp)" $grp
        fi
    done
fi
exit 0
