#!/bin/bash

. /usr/lib/eole/ihm.sh
. /usr/lib/eole/utils.sh

activer_haute_dispo=$(CreoleGet activer_haute_dispo)
nom_machine=$(CreoleGet nom_machine)
option="$1"

if [ "${activer_haute_dispo}" == "non" ]
then
# Si haute dispo désactivée, on stoppe corosync
    /etc/init.d/corosync stop
    /usr/bin/pgrep corosync >/dev/null
    if [ $? == 0 ]
    then
        killall corosync 2>&1 >/dev/null
        /usr/bin/pgrep corosync >/dev/null
        if [ $? == 0 ]
        then
            killall -9 corosync 2>&1 >/dev/null
        fi
    fi
    rm -f /var/lib/heartbeat/crm/*
else
    corosync_dial_if=$(CreoleGet corosync_dial_if)
    id eole &>/dev/null
    if [ $? -eq 0 ]
    then
        groups eole |grep haclient >/dev/null
        if [ $? -ne 0 ]
        then
            usermod -G haclient -a eole
        fi
    fi
    # On genere un bi-clef rsa sur chaque machine si il n'existe pas
    if [ ! -e /root/.ssh/id_rsa.pub ] &&
            [ ! -e /root/.ssh/id_rsa ]
        then
            ssh-keygen -t rsa -b 2048 -f /root/.ssh/id_rsa -N "" -q
    fi

    # on renseigne le noeud distant
    if [ "${activer_haute_dispo}" == "maitre" ]
    then
        remote_node=$(CreoleGet nom_machine_esclave)
    elif [ "${activer_haute_dispo}" == "esclave" ]
    then
        remote_node=$(CreoleGet nom_machine_maitre)
    fi

    if [ "$option" = "instance" ]
    then
        ## Suppression de l'ancienne conf pacemaker
        if [ -e /var/lib/heartbeat/crm/cib.xml ]
        then
            /etc/init.d/corosync stop
            /usr/bin/pgrep corosync >/dev/null
            if [ $? == 0 ]
            then
                killall corosync 2>&1 >/dev/null
                /usr/bin/pgrep corosync >/dev/null
                if [ $? == 0 ]
                then
                    killall -9 corosync 2>&1 >/dev/null
                fi
            fi
            rm -f /var/lib/heartbeat/crm/*
        fi
    fi

    # On lance corosync si il n'est pas lancé
    crm status > /dev/null 2>&1
    if [ ! $? -eq 0 ]
    then
        /etc/init.d/corosync start
        wait_true_retcode "Attente démarrage corosync" "crm status > /dev/null 2>&1"
    fi
    /usr/share/eole/sbin/haute_dispo_maintenance deactivate

    if [ "${activer_haute_dispo}" = "maitre" ]
    then
        cmd="crm_mon -1|grep -s Online|grep -q -s -E \" ${nom_machine} \""
        wait_true_retcode "Attente noeud Online" "${cmd}"
        /usr/share/eole/sbin/appliquer_hautedispo
        # On attend que les ressources soient montées
        wait_true_retcode "Attente ressources montées" "crm resource show VIP_src_addr 2>&1|grep -s -q 'is running'"
    fi

    if [ ! "$option" = "reconfigure" ]; then
        EchoOrange "Les serveurs maître et esclave doivent pouvoir dialoguer en ssh sur l'interface $corosync_dial_if"
        Question_ouinon "Voulez-vous synchroniser les noeuds ?" 'True' "non"
        rep=$?
    else
        rep=1
    fi
    if [ $rep -eq 0 ]
    then
        # on attend que les 2 noeuds soient Online
        crm node online ${nom_machine} > /dev/null 2>&1
        cmd="crm_mon -1|grep -s Online|grep -q -s -E \" ${nom_machine} \""
        wait_true_retcode "Attente noeud $nom_machine Online" "${cmd}"
        crm resource show VIPCluster > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
            # On attend que les ressources soient montées si elles sont déclarées
            wait_true_retcode "Attente ressources montées" "crm resource show VIP_src_addr 2>&1|grep -s -q 'is running'"
        fi
        /usr/share/eole/sbin/synchro-nodes.sh
    else
        if [ ! "$option" = "reconfigure" ]; then
            EchoOrange "Pour forcer la synchronisation, vous pouvez lancer le script 'synchro-nodes.sh' manuellement une fois les deux noeuds 'Online'"
            EchoOrange "Vous pouvez vérifier leur status à l'aide de la commande 'crm_mon -1'"
        fi
    fi
    exit 0
fi
