#!/bin/bash

#=================================================
# GENERIC START
#=================================================

# Source YunoHost helpers
source /usr/share/yunohost/helpers

# Stop script if errors
ynh_abort_if_errors

# Import common cmd
source ../settings/scripts/experimental_helper.sh
source ../settings/scripts/_common.sh

ynh_script_progression --message="Loading settings..."

# Retrieve arguments
domain=$(ynh_app_setting_get --app $app --key domain)
path_url=$(ynh_normalize_url_path $(ynh_app_setting_get --app $app --key path))
seafile_data=/home/yunohost.app/seafile-data
db_pwd=$(ynh_app_setting_get --app $app --key mysqlpwd)

# Check domain/path availability
ynh_webpath_available $domain $path_url || ynh_die --message "$domain/$path_url is not available, please use an other domain or path."

#=================================================
# STANDARD RESTORATION STEPS
#=================================================

# Restore all config and data
ynh_script_progression --message="Restoring files..." --weight=10
ynh_restore

# Get configuration for user and final path
get_configuration

# Create user if it need
[[ $seafile_user = "seafile" ]] && ynh_system_user_create $seafile_user $final_path

# Restore dependencies
ynh_script_progression --message="Reinstalling dependencies..." --weight=5
install_dependance

# Restore mysql dump
db_user=seafile
ynh_mysql_setup_db --db_user $db_user --db_name ccnetdb --db_pwd "$db_pwd"
ynh_mysql_setup_db --db_user $db_user --db_name seafiledb --db_pwd "$db_pwd"
ynh_mysql_setup_db --db_user $db_user --db_name seahubdb --db_pwd "$db_pwd"
su -c "mysql -u ${app} -p$db_pwd ccnetdb < ${YNH_CWD}/ccnetdb.dmp"
su -c "mysql -u ${app} -p$db_pwd seafiledb < ${YNH_CWD}/seafiledb.dmp"
su -c "mysql -u ${app} -p$db_pwd seahubdb < ${YNH_CWD}/seahubdb.dmp"

#=================================================
# GENERIC FINALIZATION
#=================================================

# Restore sso persistent config
ynh_script_progression --message="Configuring permissions..."
python3 ../settings/conf/add_sso_conf.py || ynh_die --message="Your file /etc/ssowat/conf.json.persistent doesn't respect the json syntax. Please fix the syntax to install this app."

# Add logrotate
ynh_script_progression --message="Configuring log rotation..."
ynh_use_logrotate $final_path/logs
ln -s $final_path/logs /var/log/seafile

# Set all permissions
ynh_script_progression --message="Protecting directory..."
set_permission

# Enable service and start seafile
ynh_script_progression --message="Reconfiguring application..."
systemctl daemon-reload
systemctl enable seafile
systemctl enable seahub

# Add Seafile to YunoHost's monitored services
yunohost service add seafile
yunohost service add seahub

ynh_script_progression --message="Reloading services..."

# Reload nginx
systemctl reload nginx.service

# Reload fail2ban
ynh_systemd_action --service_name=fail2ban --action=reload

# Avoid the current effect
sleep 5

# Restart service
ynh_script_progression --message="Starting seafile services..." --weight=3
ynh_systemd_action --service_name seafile
ynh_systemd_action --service_name seahub

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