#!/bin/bash

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

# IMPORT GENERIC HELPERS
source /usr/share/yunohost/helpers

# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

# Load common variables and helpers
source ../settings/scripts/experimental_helper.sh
source ../settings/scripts/_common.sh

# Retrieve old app settings
domain=$(ynh_app_setting_get "$app" domain)
path_url=$(ynh_app_setting_get "$app" path)
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
admin=$(ynh_app_setting_get "$app" adminusername)
port=$(ynh_app_setting_get "$app" web_port)

# Check domain/path availability with app helper
ynh_webpath_available $domain $path_url || ynh_die "$domain is not available as domain, please use an other domain."

# Check user parameter
ynh_user_exists "$admin" \
  || ynh_die "The chosen admin user does not exist."

# Check Final Path availability
test ! -e "$final_path" || ynh_die "This path already contains a folder"

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

# Add users
# We can't use the official helper (for now) because we need to set the shell for the login
test getent passwd "$app" &>/dev/null || \
    useradd -d "$DATADIR" --system --user-group "$app" --shell /bin/bash || \
        ynh_die "Unable to create $app system account"
# Should be replaced by this when getops is available in the helper ynh_system_user_create (probably in Yunohost 3.5)
# ynh_system_user_create --username= $app --home_dir=/opt/yunohost/$app --use_shell


#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================

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

#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================

ynh_restore_file "/etc/logrotate.d/$app"

#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_restore_file "$final_path"

#=================================================
# RESTORE DATADIR
#=================================================
# Restore all files
datadir="/home/${app}"

# The data directory will be restored only if it exists in the backup archive
# So only if it was backup previously.
if [ -d "$YNH_BACKUP_DIR/apps/$app/backup/home/$app" ]
then
	ynh_restore_file "$datadir"
else
	# Create app folders
	mkdir -p "$datadir"
fi
# Remove the option backup_core_only if it's in the settings.yml file
ynh_app_setting_delete $app backup_core_only


#=================================================
# RESTORE DATABASE
#=================================================
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./db.sql

# Restore systemd files
systemctl daemon-reload
systemctl enable "$app".service

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

# Set permissions
set_permission

# Configure logrotate
ynh_use_logrotate "/var/log/$app"

# Add gitea to YunoHost's monitored services
yunohost service add "$app" --log /var/log/"$app"/"$app".log

# Reload services
systemctl reload nginx.service
ynh_systemd_action -l "Serving \[::\]:$port with pid" -p "systemd"
sleep 1