#!/bin/bash

# Note: each files and directories you've saved using the ynh_backup helper
# will be located in the current directory, regarding the last argument.

# Exit on command errors and treat unset variables as an error
set -eu

# See comsudo yunohost app ssowatconfments in install script
app=$YNH_APP_INSTANCE_NAME

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

# Retrieve old app settings
domain=$(ynh_app_setting_get "$app" domain)
path=$(ynh_app_setting_get "$app" path)
is_public=$(ynh_app_setting_get "$app" is_public)

# Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
    || ynh_die "Path not available: ${domain}${path}"

# Restore sources & data
final_path="/var/www/${app}"
if [ -d $final_path ]; then
	ynh_die "There is already a directory: $final_path"
fi

sudo cp -a ./sources "${final_path}"

# Restore permissions to app files
# you may need to make some file and/or directory writeable by www-data (nginx user)
sudo chown -R root: "${final_path}"

### MySQL (remove if not used) ###
# If a MySQL database is used:
# # Create and restore the database
dbname=$app
dbuser=$app
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./dump.sql
### MySQL end ###

# Check configuration files nginx
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
if [ -f $nginx_conf ]; then
	ynh_die "The NGINX configuration already exists at '${nginx_conf}'. You should safely delete it before restoring this app."
fi

# Restore NGINX configuration
sudo cp -a ./nginx.conf "${nginx_conf}"

### PHP (remove if not used) ###
# If a dedicated php-fpm process is used:
# # Copy PHP-FPM pool configuration and reload the service
# sudo cp -a ./php-fpm.conf "/etc/php5/fpm/pool.d/${app}.conf"
# sudo service php5-fpm reload
### PHP end ###

# If app is public, add url to SSOWat conf as skipped_uris
if [[ $is_public -eq 1 ]]; then
  # See install script
  ynh_app_setting_set "$app" unprotected_uris "/"
fi

sudo chmod -R 777 $final_path/data

# Restart webserver
sudo service nginx reload