#!/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

#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading additionnal settings..." --weight=1

redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app=$app --key=redis_db --value=$redis_db

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

ynh_restore_file --origin_path="$install_dir"

chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:$app "$install_dir"

#=================================================
# RESTORE THE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Restoring the data directory..." --weight=1

ynh_restore_file --origin_path="$data_dir" --not_mandatory

mkdir -p "$data_dir/consume"
mkdir -p "$data_dir/data"
mkdir -p "$data_dir/media"

chmod 750 "$data_dir"
chmod -R o-rwx "$data_dir"
chown -R $app:$app "$data_dir"

#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=1

ynh_add_config --template="../settings/conf/paperless.conf.example" --destination="$install_dir/paperless.conf"

chmod 400 "$install_dir/paperless.conf"
chown $app:$app "$install_dir/paperless.conf"

#=================================================
# RESTORE FAIL2BAN CONFIGURATION
#=================================================
#ynh_script_progression --message="Restoring the Fail2Ban configuration..." --weight=1

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

#=================================================
# SPECIFIC RESTORATION
#=================================================

#=================================================
# INSTALL PYTHON DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing Python dependencies..."

pushd $install_dir
	ynh_secure_remove --file="$install_dir/venv"
	python3 -m venv venv
	chown -R "$app:" "$install_dir"
(
	source "$install_dir/venv/bin/activate"
	ynh_exec_as $app $install_dir/venv/bin/pip3 install --upgrade pip setuptools wheel
	ynh_exec_as $app $install_dir/venv/bin/pip3 install -r "$install_dir/requirements.txt"
	deactivate
)
popd

#=================================================
# 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 POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the PostgresSQL database..." --weight=1

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

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

#ynh_restore_file --origin_path="/etc/$app/"

#=================================================
# RESTORE SYSTEMD
#=================================================
ynh_script_progression --message="Restoring the systemd configuration..." --weight=1

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

#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the logrotate configuration..." --weight=1

mkdir -p "/var/log/$app"
chown -R $app: "/var/log/$app"

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

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

yunohost service add "$app" --log="/var/log/$app/$app.log"
yunohost service add "$app-consumer" --log="/var/log/$app/$app-consumer.log"
yunohost service add "$app-scheduler" --log="/var/log/$app/$app-scheduler.log"
yunohost service add "$app-task-queue" --log="/var/log/$app/$app-task-queue.log"

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1

ynh_systemd_action --service_name="$app" --action="start" --log_path="/var/log/$app/$app.log"
ynh_systemd_action --service_name="$app-consumer" --action="start" --log_path="/var/log/$app/$app-consumer.log"
ynh_systemd_action --service_name="$app-scheduler" --action="start" --log_path="/var/log/$app/$app-scheduler.log"
ynh_systemd_action --service_name="$app-task-queue" --action="start" --log_path="/var/log/$app/$app-task-queue.log"

#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX AND PHP-FPM
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1

ynh_systemd_action --service_name=nginx --action=reload

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

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