#!/bin/bash

#=================================================
# GENERIC START
#=================================================
# 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

#=================================================
# STOP TOMCAT
#=================================================
ynh_script_progression --message="Stopping system tomcat..." --weight=1

tomcat_enabled=""
tomcat_active=""
if [ "$(systemctl cat $tomcat_version --quiet)" ]; then
	if (systemctl is-enabled $tomcat_version --quiet); then
		tomcat_enabled=1
	fi
	if (systemctl is-active $tomcat_version --quiet); then
		tomcat_active=1
	fi
fi

if [ ! $tomcat_enabled ]; then
	systemctl disable $tomcat_version --quiet
fi
if [ ! $tomcat_active ]; then
	systemctl stop $tomcat_version --quiet
fi

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

ynh_restore_file --origin_path="$install_dir"
chown -R "$app:www-data" "$install_dir"

#=================================================
# RECREATE THE DEDICATED USER
#=================================================
ynh_script_progression --message="Recreating the dedicated system user..." --weight=1

# Create the dedicated user (if not existing)
ynh_system_user_create --username="$app-guacd"
ynh_system_user_create --username="$app-tomcat"

#=================================================
# 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

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

ynh_restore_file --origin_path="/var/log/$app"

_set_permissions

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

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

ynh_restore_file "/etc/fail2ban/jail.d/$app.conf"
ynh_restore_file "/etc/fail2ban/filter.d/$app.conf"
ynh_systemd_action --action=restart --service_name=fail2ban

ynh_restore_file --origin_path="/etc/systemd/system/$app-guacd.service"
systemctl enable "$app-guacd.service" --quiet
yunohost service add "$app-guacd" --description="Guacamole server" --log="/var/log/$app/guacd.log"

ynh_restore_file --origin_path="/etc/systemd/system/$app-tomcat.service"
systemctl enable "$app-tomcat.service" --quiet
yunohost service add "$app-tomcat" --description="Guacamole client" --log="/var/log/$app/tomcat.log"

ynh_restore_file --origin_path="/etc/logrotate.d/$app"

#=================================================
# START SYSTEMD SERVICES
#=================================================
ynh_script_progression --message="Starting NGINX and $app's systemd services..." --weight=1

ynh_systemd_action --service_name="$app-guacd" --action="start" --log_path="/var/log/$app/guacd.log"
ynh_systemd_action --service_name="$app-tomcat" --action="start" --log_path="/var/log/$app/tomcat.log"

ynh_systemd_action --service_name=nginx --action=reload

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

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