#!/bin/bash

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

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

#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin_user=$(ynh_app_setting_get --app=$app --key=admin_user)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
datadir=$(ynh_app_setting_get --app=$app --key=datadir)

#=================================================
# CHECK VERSION
#=================================================
ynh_script_progression --message="Checking version..."

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=3

# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
	ynh_clean_check_starting
	# Restore it if the upgrade fails
	ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1

# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
	ynh_legacy_permissions_delete_all

	ynh_app_setting_delete --app=$app --key=is_public
fi

# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
	final_path="/var/www/$app"
	ynh_app_setting_set --app="$app" --key=final_path --value="$final_path"
fi
# If datadir doesn't exist, create it
if [ -z "$datadir" ]; then
	datadir="/home/yunohost.app/$app"
	ynh_app_setting_set --app="$app" --key=datadir --value="$datadir"
fi

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..."

# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir="$final_path"

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	# download & unpack bozon
	ynh_script_progression --message="Setting up source files..." --weight=2
	## download source
	tmpdir=$(mktemp -d)
	ynh_setup_source --dest_dir="$tmpdir"
	## clean & copy files needed to final folder
	myynh_clean_source
	[ -e "$tmpdir/config.php" ] && ynh_secure_remove "$tmpdir/config.php"
	cp -a "$tmpdir/." "$final_path"
	ynh_secure_remove "$tmpdir"
fi

## set permissions
myynh_set_permissions

#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=1

ynh_install_app_dependencies $pkg_dependencies

#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading PHP-FPM configuration..."

# Create a dedicated PHP-FPM config
ynh_add_fpm_config

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading nginx web server configuration..."

if [ "$path_url" != "/" ]; then
	ynh_replace_string --match_string="^#sub_path_only" --replace_string="" --target_file="../conf/nginx.conf"
fi
ynh_add_nginx_config

#=================================================
# SPECIFIC UPGRADE
#=================================================
# CLEAN PHP SESSIONS STORED IN /var/lib/phpx/sessions
#=================================================
ynh_script_progression --message="Cleaning php sessions stored..."

if [ -d "/usr/lib/php5" ]; then
	[ -x /usr/lib/php5/sessionclean ] && /usr/lib/php5/sessionclean
elif [ -d "/usr/lib/php" ]; then
	[ -x /usr/lib/php/sessionclean ] && /usr/lib/php/sessionclean
fi

#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..."

ynh_systemd_action --service_name=nginx --action=reload

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

ynh_script_progression --message="Upgrade of $app completed" --last