#!/bin/bash

container_path_web=$(CreoleGet container_path_web)
PWD_OK=$container_path_web/etc/bareos-webui/password.ok
apache_path=$container_path_web/etc/apache2

if [ ! -f $apache_path/conf-available/zend-framework2.conf ];then
    cp -f $apache_path/conf.d/zend-framework2.conf $apache_path/conf-available/zend-framework2.conf
fi

if [ "$(CreoleGet activer_bareoswebui non)" = "oui" ];then
    CreoleRun "a2enconf zend-framework2 >/dev/null" web
    if [ ! -f $PWD_OK ]; then
        # droits en lecture pour l'utilisateur bareos_webui
        set -e
        path_mysql=$(CreoleGet container_path_mysql)
        ip_web=$(CreoleGet container_ip_web)
        DB_USER=bareos_webui
        DB_PASS=$(CreoleGet bareoswebui_db_mysql_password)
        /usr/lib/bareos/scripts/bareos-config get_database_grant_privileges mysql $DB_USER $DB_PASS readonly | sed "s/127.0.0.1/$ip_web/g" > $path_mysql/tmp/database_grant_privileges.sql
        CreoleService mysql start
        CreoleRun "mysql --defaults-file=/etc/mysql/debian.cnf < /tmp/database_grant_privileges.sql" "mysql"
        CreoleRun "mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-privileges" "mysql"
        CreoleService mysql stop
        rm -f $path_mysql/tmp/database_grant_privileges.sql
        set +e
        touch $PWD_OK
    fi
else
    CreoleRun "a2disconf zend-framework2 >/dev/null" web
fi

exit 0
