#!/bin/bash

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

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

ynh_abort_if_errors

#=================================================
# LOAD SETTINGS
#=================================================

ynh_script_progression --message="Loading installation settings..." --time --weight=1

app=$YNH_APP_INSTANCE_NAME

final_path=$(ynh_app_setting_get "$app" final_path)
domain=$(ynh_app_setting_get "$app" domain)
db_name=$(ynh_app_setting_get "$app" db_name)

#=================================================
# STANDARD BACKUP STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================

ynh_script_progression --message="Stopping systemd services..." --time --weight=1

ynh_systemd_action --service_name="$app" --action="stop"
ynh_systemd_action --service_name="$app-celery" --action="stop"

#=================================================
# BACKUP THE APP MAIN DIR
#=================================================

ynh_script_progression --message="Backing up the main app directory..." --time --weight=1

ynh_backup "$final_path"

#=================================================
# BACKUP THE NGINX CONFIGURATION
#=================================================

ynh_script_progression --message="Backing up nginx web server configuration..." --time --weight=1

ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf"

#=================================================
# BACKUP THE PostgreSQL DATABASE
#=================================================

ynh_script_progression --message="Backing up the PostgreSQL database..." --time --weight=1

ynh_psql_dump_db "$db_name" > db.sql
ynh_backup "db.sql"

#=================================================
# SPECIFIC BACKUP
#=================================================
# BACKUP LOGROTATE
#=================================================
ynh_script_progression --message="Backing up logrotate configuration..." --time --weight=1

ynh_backup --src_path="/etc/logrotate.d/$app"

#=================================================
# BACKUP THE CRON FILE
#=================================================

ynh_script_progression --message="Backing up systemd configuration..." --time --weight=1

ynh_backup "/etc/cron.d/$app"

#=================================================
# BACKUP THE uwsgi files
#=================================================

ynh_script_progression --message="Backing up uwsgi configuration..." --time --weight=1

ynh_backup "/etc/uwsgi/apps-available/$app.ini"
ynh_backup "/etc/systemd/system/$app.service"
ynh_backup "/etc/systemd/system/$app-celery.service"

#=================================================
# BACKUP THE celery files
#=================================================

# nothing to do because it is inside $finalpath

#=================================================
# BACKUP THE hub binary file
#=================================================

ynh_script_progression --message="Backing up uwsgi configuration..." --time --weight=1

ynh_backup /usr/bin/hub

#=================================================
# START SYSTEMD SERVICE
#=================================================

ynh_script_progression --message="Starting systemd services..." --time --weight=1

ynh_systemd_action --service_name="$app" --action="start"
ynh_systemd_action --service_name="$app-celery" --action="start"

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

ynh_script_progression --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)." --time --last