#!/bin/bash

#=================================================
# IMPORT GENERIC HELPERS
#=================================================

# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers

#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_script_progression --message="Restoring the app main directory..."

chmod -R o-rwx "/usr/share/zabbix"
chown -R "$app:www-data" "/usr/share/zabbix"

#=================================================
# RESTORE THE MYSQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the MySQL database..." --weight=1

ynh_mysql_connect_as --user="$db_user" --password="$db_pwd" --database="$db_name" < ./db.sql

export mysqlconn="mysql --user=$db_user --password=$db_pwd --database=$db_name"
convert_ZabbixDB

#=================================================
# RESTORE VARIOUS FILES
#=================================================
ynh_script_progression --message="Restoring various files..."

if [ -f "/etc/zabbix/web/zabbix.conf.php" ]; then
    ynh_secure_remove --file="/etc/zabbix/web/zabbix.conf.php"
fi
ynh_restore_file --origin_path="/etc/zabbix/web/zabbix.conf.php"

chmod 400 "/etc/zabbix/web/zabbix.conf.php"
chown "$app:www-data" "/etc/zabbix/web/zabbix.conf.php"

ynh_restore_file --origin_path="/etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config"

ynh_restore_file --origin_path="/etc/zabbix/zabbix_server.conf"
ynh_restore_file --origin_path="/etc/zabbix/zabbix_agentd.conf"

if [ ! -L /etc/zabbix/zabbix_agentd.d ]; then
    ln -s /etc/zabbix/zabbix_agentd.conf.d /etc/zabbix/zabbix_agentd.d
fi

# Restore sudo file
ynh_restore_file --origin_path="/etc/sudoers.d/zabbix"

# Restore zabbix-cli
ynh_restore_file --origin_path="/usr/share/zabbix-cli"

#=================================================
# RESTORE SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression --message="Restoring system configurations related to $app..." --weight=1

ynh_replace_string --match_string="# $language.UTF-8 UTF-8" --replace_string="$language.UTF-8 UTF-8" --target_file=/etc/locale.gen
locale-gen

ynh_restore_file --origin_path="/etc/php/$phpversion/fpm/pool.d/$app.conf"

ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"

ynh_restore_file --origin_path="/etc/apt/preferences.d/zabbix_repo"

change_timeoutAgent

systemctl enable zabbix-agent --quiet
systemctl enable zabbix-server --quiet
yunohost service add snmpd --description="Management of SNMP Daemon"
yunohost service add zabbix-server --description="Management Zabbix server daemon : collect, agregate, compute and notify" --log="/var/log/$app/${app}_server.log"
yunohost service add zabbix-agent --description="Management Zabbix agent daemon : send informations about this host to the server" --log="/var/log/$app/${app}_agent.log"

#=================================================
# RELOAD NGINX AND PHP-FPM OR THE APP SERVICE
#=================================================
ynh_script_progression --message="Reloading NGINX web server and $app's service..." --weight=1

# Start a systemd service
ynh_systemd_action --service_name="$app-server" --action="restart" --log_path="/var/log/$app/${app}_server.log"
ynh_systemd_action --service_name="$app-agent" --action="restart" --log_path="/var/log/$app/${app}_agent.log"

ynh_systemd_action --service_name="php$phpversion-fpm" --action=reload
ynh_systemd_action --service_name=nginx --action=reload

#=================================================
# END OF SCRIPT
#=================================================

ynh_script_progression --message="Restoration completed for $app"
