#!/bin/bash
# Met à jour la clé utilisée pour authentifier les appels à l'API
# Génère les configurations depuis la base de données

if [ -f /usr/bin/ParseDico ];then
    #EOLE 2.3
    . ParseDico
    . /etc/eole/containers.conf
	. /usr/share/eole/FonctionsEoleNg
	RunCmd=RunCmd
else
    #EOLE 2.4
    activer_envole=$(CreoleGet activer_envole)
    container_path_web=$(CreoleGet container_path_web)
    container_ip_mysql=$(CreoleGet container_ip_mysql)
    mode_conteneur_actif=$(CreoleGet mode_conteneur_actif)
    container_ip_web=$(CreoleGet container_ip_web)
    adresse_ip_eth0=$(CreoleGet adresse_ip_eth0)
    RunCmd=CreoleRun
fi

POSHPASS=keeliexo
if [ "$activer_envole" == "oui" ]
then
    DEFPAGE=$container_path_web/var/www/html/posh/indisponible.php
    function resetposhindex(){
        if [ -f $container_path_web/var/www/html/posh/index.php.sav ]
        then
            /bin/mv -f $container_path_web/var/www/html/posh/index.php.sav $container_path_web/var/www/html/posh/index.php
            $RunCmd "chown root:www-data /var/www/html/posh/index.php" web
            chmod 640 $container_path_web/var/www/html/posh/index.php
        fi
    }
    function defpage() {
        if [ -f $DEFPAGE ]
        then
            if [ ! -f $container_path_web/var/www/html/posh/index.php.sav ]
            then
                # on écrase pas le fichier qui est déjà sauvé (risque d'écraser avec le defpage.php)
                /bin/cp -f $container_path_web/var/www/html/posh/index.php $container_path_web/var/www/html/posh/index.php.sav
            fi
            /bin/cp -f $DEFPAGE $container_path_web/var/www/html/posh/index.php
            $RunCmd "chown root:www-data /var/www/html/posh/index.php" web
            chmod 640 $container_path_web/var/www/html/posh/index.php
        fi
    }
    resetposhindex


    POSHKEY=`pwgen --num-passwords=1 20`
    sed -i "s/define(\"__KEY\",\"\([^)]\+\)\");/define(\"__KEY\",\"$POSHKEY\");/" $container_path_web/var/www/html/posh/includes/config.inc.php

    
    if [ $? != 0 ]
    then
        echo "Envole :  Erreur à la modification de la clé d'authentification pour l'API posh."
        defpage
        exit 0
    fi
    echo "UPDATE posh.adm_config SET value='$POSHKEY' WHERE parameter='KEY' LIMIT 1;" | mysql -uposh -p$POSHPASS -h$container_ip_mysql posh
    
    #serveur de widgets
    COW_KEY=`cat $container_path_web/var/www/html/posh/includes/config.inc.php | grep "\"__COW_KEY\"" |cut -d "\"" -f4`
    if [[ "$COW_KEY" == "" ]]
    then
	COW_KEY=`pwgen --num-passwords=1 20`
	echo "INSERT IGNORE INTO posh.adm_config (parameter, value, datatype, desttype, category, editable) VALUES ('COW_KEY', '$COW_KEY', 'str', 'P', NULL, '1');" | mysql -uposh -p$POSHPASS -h$container_ip_mysql 
    fi
    
    mkdir -p /tmp/posh/

    # Récupération de l'IP du seveur web selon la présence de conteneurs
    if [[ ${mode_conteneur_actif} == "oui" ]]
    then
        IP_SERVEUR_WEB=${container_ip_web}
    else
        IP_SERVEUR_WEB=${adresse_ip_eth0}
    fi

    # Mise en place de la configuration (génération des fichiers) NB : le prereconf place un fichier minimal
    wget "https://${IP_SERVEUR_WEB}/envole/api/scr_config_generate_configfiles.php?adminkey=$POSHKEY" -o/tmp/posh/scr_config_generate_configfiles.php.log -O/tmp/posh/scr_config_generate_configfiles.php.txt --no-check-certificate
    WGET_FILE=$(cat /tmp/posh/scr_config_generate_configfiles.php.txt)
    if [[ ! "$WGET_FILE" == *"<status>1</status>"* ]]
    then
        echo "Envole : Erreur à la génération des fichiers de configuration."
        defpage
        exit 0
    fi

    # Mise en place des plugins
    wget "https://${IP_SERVEUR_WEB}/envole/api/scr_config_plugins.php?adminkey=$POSHKEY" -o/tmp/posh/scr_config_plugins.php.log -O/tmp/posh/scr_config_plugins.php.txt --no-check-certificate
    WGET_FILE=$(cat /tmp/posh/scr_config_plugins.php.txt)
    if [[ ! "$WGET_FILE" == *"<status>1</status>"* ]]
    then
        echo "Envole : Erreur à la configuration des plugins du portail."
        defpage
        exit 0
    fi

    # Rafraichissement du cache
    wget "https://${IP_SERVEUR_WEB}/envole/api/scr_cache_generate_all.php?adminkey=$POSHKEY" -o/tmp/posh/scr_cache_generate_all.php.log -O/tmp/posh/scr_cache_generate_all.php.txt --no-check-certificate
    WGET_FILE=$(cat /tmp/posh/scr_cache_generate_all.php.txt)
    if [[ ! "$WGET_FILE" == *"<status>1</status>"* ]]
    then
        echo "Envole : Erreur à la regénération du cache du portail."
    fi

    # Mise en place du thème eole (si nécessaire)
    if [ ! -s $container_path_web/var/www/html/posh/styles/main1.css ]
    then
        wget --post-data 'theme0=eole' "https://${IP_SERVEUR_WEB}/envole/api/scr_config_theme.php?adminkey=$POSHKEY" -o/tmp/posh/scr_config_theme.php.log -O/tmp/posh/scr_config_theme.php.txt --no-check-certificate
        WGET_FILE=$(cat /tmp/posh/scr_config_theme.php.txt)
        if [[ ! "$WGET_FILE" == *"<status>1</status>"* ]]
        then
            echo "Envole : Erreur à la mise en place du thème par défaut."
        fi
    fi
    
    # update du numéro de version de l'application
    echo "UPDATE posh.adm_config SET value='3.1.2' WHERE parameter='POSHVERSION';" | mysql -uposh -p$POSHPASS -h$container_ip_mysql posh
    
    # Correction 4964
    # Passage du __LOCALFOLDER en https
    sed -i "s/define(\"__LOCALFOLDER\",\"http:\/\//define(\"__LOCALFOLDER\",\"https:\/\//" $container_path_web/var/www/html/posh/includes/config.inc.php
    # Fin correction #4964
	
	#Plugin cow
	if [ "$activer_plugin_cow" == "oui" ]
	then
		if ! grep "# PLUGIN COW" $container_path_web/etc/apache2/sites-available/Vhost > /dev/null ; then 
			#modules apache requis
			a2enmod proxy
			a2enmod proxy_http
			#ajout ligne include dans Vhost
			sed -i "s/<\/VirtualHost>/# PLUGIN COW\\nInclude cow.*\\n\\n<\/VirtualHost>/" $container_path_web/etc/apache2/sites-available/Vhost
		fi
		if ! grep "# PLUGIN COW" $container_path_web/etc/apache2/sites-available/Vhost-ssl > /dev/null ; then 
			#ajout ligne include dans Vhost-ssl
			sed -i "s/<\/VirtualHost>/# PLUGIN COW\\nInclude cow.*\\n\\n<\/VirtualHost>/" $container_path_web/etc/apache2/sites-available/Vhost-ssl
		fi
	fi
	
fi
exit 0
