#!/bin/bash

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

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

#=================================================
# CHECK VERSION
#=================================================

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."

# If admin_mail_html doesn't exist, create it
if [ -z "${admin_mail_html:-}" ]; then
	admin_mail_html=1
		ynh_app_setting_set --app=$app --key=admin_mail_html --value=$admin_mail_html
fi

# If fpm_footprint doesn't exist, create it
if [ -z "${fpm_footprint:-}" ]; then
	fpm_footprint=low
	ynh_app_setting_set --app=$app --key=fpm_footprint --value=$fpm_footprint
fi

# If fpm_free_footprint doesn't exist, create it
if [ -z "${fpm_free_footprint:-}" ]; then
	fpm_free_footprint=0
	ynh_app_setting_set --app=$app --key=fpm_free_footprint --value=$fpm_free_footprint
fi

# If fpm_usage doesn't exist, create it
if [ -z "${fpm_usage:-}" ]; then
	fpm_usage=low
	ynh_app_setting_set --app=$app --key=fpm_usage --value=$fpm_usage
fi

# If with_sftp doesn't exist, create it
if [ -z "${with_sftp:-}" ]; then
	with_sftp=1
	ynh_app_setting_set --app=$app --key=with_sftp --value=$with_sftp
fi

# If phpversion doesn't exist, create it. We assume it is the default system one.
if [ -z "$phpversion" ]; then
	phpversion=$YNH_DEFAULT_PHP_VERSION
	ynh_app_setting_set --app=$app --key=phpversion --value=$phpversion
fi

echo Init the custom error variable
# If custom_error_file doesn't exist, create it. We assume it is the default system one.
if [ -n "$(ynh_app_setting_get --app=$app --key=custom_error_file)" ]; then
  echo Variable not existing, create-it
	custom_error_file=$(ynh_app_setting_get --app=$app --key=custom_error_file)
else
  echo The variable exist
fi
echo Test a condition
echo ynh_app_setting_get --app=$app --key=custom_error_file
echo Value return

# Delete old user
if [ -n "$(ynh_app_setting_get --app=$app --key=user)" ]
then
    ynh_systemd_action --service_name=php${phpversion}-fpm --action=stop
    ynh_system_user_delete --username="$(ynh_app_setting_get --app=$app --key=user)"
    ynh_app_setting_delete --app=$app --key=user
fi

# Ensure password is a setting even if empty, for the config panel
ynh_app_setting_set --app=$app --key=password --value="$password"

#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================

ynh_maintenance_mode_ON

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

# Prepare nginx.conf
nginx_extra_conf_dir="/etc/nginx/conf.d/$domain.d/$app.d"

mkdir -p "$nginx_extra_conf_dir"

# Prepare nginx extra conf
if [ $phpversion != "none" ]
then
    ynh_add_config --template="nginx-php.conf" --destination="$nginx_extra_conf_dir/php.conf"
    YNH_PHP_VERSION="$phpversion"
fi

echo call this variable $custom_error_file
# Add the config error code
if [ ! $custom_error_file ]
then
    ynh_add_config --template="nginx-code-error.conf" --destination="$nginx_extra_conf_dir/error-code.conf"
fi

# Create a dedicated NGINX config
ynh_add_nginx_config
ynh_add_config --template="example-custom-nginx-config.conf" --destination="$nginx_extra_conf_dir/sample.conf"

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

if [ $with_sftp -eq 1 ]
then
    groups="sftp.app"
else
    groups=""
fi

ynh_system_user_create --username=$app --home_dir="$install_dir" --groups="$groups"

if [ -n "$password" ]
then
    # Add the password to this user
    chpasswd <<< "${app}:${password}"
fi

# Change the user group for previous my_webapp install script
groupadd -f "$app"
usermod -g "$app" "$app"

#=================================================
# PHP-FPM CONFIGURATION
#=================================================

if [ $phpversion != "none" ]
then
    ynh_script_progression --message="Upgrading PHP-FPM configuration..." --weight=2

    # Create a dedicated PHP-FPM config
    ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --phpversion=$phpversion
fi

#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================

chown -R $app:www-data "$install_dir"
# Home directory of the user needs to be owned by root to allow
# SFTP connections
chown root:root "$install_dir"
setfacl -m g:$app:r-x "$install_dir"
setfacl -m g:www-data:r-x "$install_dir"
chmod 750 "$install_dir"

#=================================================
# DEACTIVE MAINTENANCE MODE
#=================================================

ynh_maintenance_mode_OFF

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

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