#!/bin/bash

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

source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers

#=================================================
# STANDARD RESTORATION STEPS
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the NGINX web server configuration..." --weight=1

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

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

ynh_restore_file --origin_path="$install_dir"
ynh_restore_file --origin_path="$data_dir"

ynh_script_progression --message="Set file permissions..."
myynh_fix_file_permissions

#=================================================
# PYTHON VIRTUALENV
# Maybe the backup contains a other Python version
#=================================================
ynh_script_progression --message="Create and setup Python virtualenv..." --weight=45

myynh_setup_python_venv

#=================================================
# RESTORE THE PostgreSQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the PostgreSQL database..." --weight=5

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

#=================================================
# RESTORE SYSTEMD
#=================================================
ynh_script_progression --message="Restoring the systemd $app configuration..."

ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
systemctl enable $app.service --quiet

#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."

yunohost service add $app

#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================
ynh_script_progression --message="Setup logging..."

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

#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
ynh_script_progression --message="Set file permissions..."
myynh_fix_file_permissions

#=================================================
# GENERIC FINALIZATION
#=================================================
# START PYINVENTORY
#=================================================
ynh_script_progression --message="Starting systemd service '$app'..." --weight=5

ynh_systemd_action --service_name=$app --action="start" --log_path="$log_file"

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."

ynh_systemd_action --service_name="nginx" --action="reload"

#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Restoration completed for $app" --last