#!/bin/bash

if [ "$(CreoleGet activer_glpi non)" = "oui" ]
then
    container_path_web=$(CreoleGet container_path_web)

    CreoleRun "chown www-data:www-data /var/www/html/glpi" web
    CreoleRun "chown www-data:www-data -R /var/www/html/glpi/config" web
    # Gestion du dossier files
    GLPI_VAR_DIR="/var/www/html/glpi/files"
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_cache" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_cache"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_cron" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_cron"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_dumps" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_dumps"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_graphs" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_graphs"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_locales" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_locales"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_lock" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_lock"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_log" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_log"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_pictures" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_pictures"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_plugins" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_plugins"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_rss" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_rss"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_sessions" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_sessions"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_tmp" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_tmp"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_uploads" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_uploads"; fi
    if [ ! -d "$container_path_web/$GLPI_VAR_DIR/_inventories" ]; then mkdir -p "$container_path_web/$GLPI_VAR_DIR/_inventories"; fi
    CreoleRun "chown www-data:www-data -R $GLPI_VAR_DIR" web

    # Update GLPI database
    # Voulez-vous continuer ? [Yes/no]
    # Voulez-vous transmettre vos "statistiques d'usage" ? [Yes/no]
    CreoleRun "echo -e \"yes\nno\" | /var/www/html/glpi/bin/console db:update" web

    # Update plugin ocsinventoryng database
    CreoleRun "/var/www/html/glpi/bin/console glpi:plugin:install ocsinventoryng --username glpi >/dev/null" web

    # Upgrade to innodb
    CreoleRun "echo yes | /var/www/html/glpi/bin/console glpi:migration:myisam_to_innodb" web

    # Upgrade GLPI 10
    CreoleRun "echo yes | /var/www/html/glpi/bin/console glpi:migration:timestamps" web
    CreoleRun "echo yes | /var/www/html/glpi/bin/console glpi:migration:utf8mb4" web
    CreoleRun "echo yes | /var/www/html/glpi/bin/console glpi:migration:unsigned_keys" web

    # Update connexion parameters for plugin ocsinventoryng
    if [ "$(CreoleGet activer_ocsinventory non)" = "oui" ]
    then
    	adresse_ip_mysql="$(CreoleGet adresse_ip_mysql)"
    	if [ "$adresse_ip_mysql" = "localhost" ]
    	then
    		adresse_ip_mysql="127.0.0.1"
    	fi

    	# récupération des paramètres de connexion à la base de données OCS
    	USEROCS=$(grep -R 'COMPTE_BASE' "$container_path_web"/usr/share/ocsinventory-reports/dbconfig.inc.php |cut -d'"' -f4)
    	PWDOCS=$(grep -R 'PSWD_BASE' "$container_path_web"/usr/share/ocsinventory-reports/dbconfig.inc.php |cut -d'"' -f4)

    	# récupération des paramètres de connexion à la base de données GLPI
    	PWDGLPI=$(grep -R 'dbpassword' "$container_path_web"/var/www/html/glpi/config/config_db.php |cut -d'"' -f2)

    	# mise à jour du champ TRACE_DELETED à 1 (true) de la table config de la base ocsweb pour fonctionnement du plugin ocsinventoryng
    	/usr/bin/mysql -u"$USEROCS" -p"$PWDOCS" -D ocsweb -h"$adresse_ip_mysql" -e "UPDATE config SET IVALUE = 1 WHERE config.NAME = 'TRACE_DELETED'"

    	# mise à jour de la table contenant les paramètres de connexion glpi à la base OCS
    	/usr/bin/mysql -u admin_glpi -p"$PWDGLPI" -D glpi -h"$adresse_ip_mysql" -e "UPDATE glpi_plugin_ocsinventoryng_ocsservers SET ocs_db_passwd = '$PWDOCS',ocs_db_user = '$USEROCS' WHERE glpi_plugin_ocsinventoryng_ocsservers.name = 'localhost'"
    fi

    # Delete install directory
    inst="$container_path_web"/var/www/html/glpi/install/install.php
	if [ -e "$inst" ]
	then
	    rm "$inst"
	fi

    CreoleRun "/var/www/html/glpi/bin/console glpi:system:clear_cache" web
    CreoleRun "chown root:root /var/www/html/glpi" web
    CreoleRun "chown www-data:www-data -R $GLPI_VAR_DIR" web
fi

exit 0
