#!/bin/bash

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

	# Update GLPI database
	CreoleRun "yes | /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 2>&1" web

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

	# Update connexion parameters for plugin ocsinventoryng
	if [ "$(CreoleGet activer_ocsinventory non)" = "oui" ]
	then
		container_ip_mysql=$(CreoleGet container_ip_mysql)
		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

    chown www-data:www-data -R $container_path_web/var/www/html/glpi/config
    chown www-data:www-data -R $container_path_web/var/www/html/glpi/files    
    CreoleRun "/var/www/html/glpi/bin/console glpi:system:clear_cache" web
fi

exit 0
