#!/bin/bash

BACKUP_DIR=/opt/backups/patch-mca

# Check if we're root and re-execute if we're not.
rootcheck () {
    if [ $(id -u) != "0" ]
    then
        sudo "$0" "$@"  # Modified as suggested below.
        exit $?
    fi
}

rootcheck "${@}"

# Welcome and Warning ?
echo "
=====================================================
=== Script de suppression de MCA pour i-Parapheur ===
====================================================="

echo "
ATTENTION : 
===========
L'exécution finale de ce script rend l'application i-Parapheur indisponible pendant quelques minutes
==========="

# Verifs
verif() {
    path=$1
    subpath=$2
    service=$3
    echo -ne "Vérification de la configuration $service..."
    if [ ! -d "$path/$subpath" ]; then
        echo "KO"
        echo "Chemin d'installation $service introuvable"
        while true; do
            read -p "Connaissez-vous le chemin d'installation $service ? (Par défaut : $path) [o/N] " yn
            case $yn in
                [Oo]* ) break;;
                "" ) exit 1;;
                [Nn]* ) echo "Toutes les informations nécéssaires n'étant pas disponibles, le script de patch va maintenant se terminer."; exit;;
                * ) echo "$yn";;
            esac
        done
        echo -ne "Merci de spécifier le  chemin d'installation de $service : "
        read path
        verif $path $subpath $service
    else
        echo "OK"
    fi
    verif_result=$path
}

do_backup() {
    if [ ! -d "$BACKUP_DIR" ]; then
        echo -ne "Backup des fichiers de configuration... "
        mkdir -p "$BACKUP_DIR"
        pushd "$BACKUP_DIR"
        cp -a "$NGINX_DIR/conf.d" .
        cp "$PARAPHEUR_DIR/tomcat/webapps/alfresco/WEB-INF/applicationAcegi.xml" .
        popd
        echo "OK"
        backup_result=0
    else 
        backup_result=1
    fi
}

do_rollback() {
    if [ -d "$BACKUP_DIR" ]; then
        echo -ne "Restauration des fichiers de configuration... "
        pushd "$BACKUP_DIR"
        cp conf.d/* "$NGINX_DIR/conf.d/"
        cp applicationAcegi.xml "$PARAPHEUR_DIR/tomcat/webapps/alfresco/WEB-INF/applicationAcegi.xml"
        popd
        echo "OK"
    fi
}

do_patch() {
    # Application 
    echo -ne "Application du patch en cours... "
    for file in "$NGINX_DIR/conf.d/*"
    do
      sed -i '/ssl_client_verify/,+3 s/^/#/' $file
      sed -i '/ssl_verify_client/s/^/#/' $file
    done
    for file in "$NGINX_DIR/sites-enabled/*"
    do
      sed -i '/ssl_client_verify/,+3 s/^/#/' $file
      sed -i '/ssl_verify_client/s/^/#/' $file
    done
    sed -i "s/channelProcessingFilter,httpSessionContextIntegrationFilter/httpSessionContextIntegrationFilter/g" "$PARAPHEUR_DIR/tomcat/webapps/alfresco/WEB-INF/applicationAcegi.xml"
    echo "OK"
}

verif "/etc/nginx" "conf.d" "NginX" # see line 12
NGINX_DIR="$verif_result"
verif "/opt/iParapheur" "" "i-Parapheur" # see line 12
PARAPHEUR_DIR="$verif_result"

do_backup # see line 38

if [[ $backup_result -eq 1 ]]; then
    echo "Répertoire de backup détecté."
    while true; do
        read -p "Voulez-vous restaurer la configuration ? [o/N] " yn
        case $yn in
            [Oo]* ) DO_ROLLBACK=1; break;;
            "" ) DO_ROLLBACK=0; break;;
            [Nn]* ) DO_ROLLBACK=0; break;;
            * ) echo "$yn";;
        esac
    done
fi

if [[ $DO_ROLLBACK -eq 0 ]]; then
    while true; do
        read -p "Installer le patch ? [o/N] " yn
        case $yn in
            [Oo]* ) break;;
            "" ) exit 1;;
            [Nn]* ) exit 1;;
            * ) echo "$yn";;
        esac
    done
fi

echo -ne "Arrêt du i-Parapheur en cours, veuillez patienter... "
/etc/init.d/alfresco stop &>1 /dev/null
echo "OK"

if [[ $DO_ROLLBACK -eq 1 ]]; then
    do_rollback # see line 53
else 
    do_patch # see line 63
fi

echo -ne "Redémarrage du service NginX et du i-Parapheur en cours... "
systemctl reload nginx || true > /dev/null 2>&1
/etc/init.d/nginx reload || true > /dev/null 2>&1
service nginx reload || true > /dev/null 2>&1
/etc/init.d/alfresco start > /dev/null 2>&1
echo "OK"

echo "Script terminé"
exit 0


