#!/bin/bash

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

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

#=================================================
# LOAD SETTINGS
#=================================================

email=$(ynh_user_get_info --username=$admin --key=mail)

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

upgrade_type=$(ynh_check_app_version_changed)

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

# 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

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

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	ynh_script_progression --message="Upgrading source files..." --weight=1

	# Download, check integrity, uncompress and patch the source from app.src
	ynh_setup_source --dest_dir="$install_dir" --keep=".env"
fi

chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"

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

ynh_install_nodejs --nodejs_version=$NODEJS_VERSION

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

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

# Create a dedicated NGINX config
ynh_add_nginx_config

#=================================================
# SPECIFIC UPGRADE
#=================================================
# UPDATE PHP DEPENDENCIES
#=================================================
ynh_script_progression --message="Updating php dependencies..."

ynh_secure_remove --file="$install_dir/vendor"
ynh_exec_warn_less ynh_install_composer --phpversion="$phpversion" --workdir="$install_dir"

chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"

#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a configuration file..." --weight=1

ynh_backup_if_checksum_is_different --file="$install_dir/.env"

ynh_add_config --template=".env" --destination="$install_dir/.env"

#=================================================
# DEPLOYMENT
#=================================================
ynh_script_progression --message="Deploying..."

update-alternatives --set php /usr/bin/php$phpversion

pushd "$install_dir"
	ynh_use_nodejs
	ynh_exec_warn_less ynh_exec_as $app $ynh_node_load_PATH yarn install
	ynh_exec_warn_less ynh_exec_as $app $ynh_node_load_PATH yarn run production
	ynh_exec_warn_less ynh_exec_as $app php$phpversion artisan monica:update --force
popd

if [ -f $install_dir/storage/oauth-private.key ]; then
	mobile_id=$(ynh_app_setting_get --app=$app --key=mobile_id)
	mobile_key=$(ynh_app_setting_get --app=$app --key=mobile_key)
	ynh_replace_string --match_string="mobile_id" --replace_string="$mobile_id" --target_file="$install_dir/.env"
	ynh_replace_string --match_string="mobile_key" --replace_string="$mobile_key" --target_file="$install_dir/.env"
else
	pushd "$install_dir"
		ynh_exec_warn_less ynh_exec_as $app php$phpversion artisan passport:keys
		ynh_exec_warn_less ynh_exec_as $app php$phpversion artisan passport:client --password -n > key.txt
		mobile_id=$( tail -2 key.txt | head -1 | cut -c 12- )
		mobile_key=$( tail -1 key.txt | cut -c 16- )
		ynh_replace_string --match_string="mobile_id" --replace_string="$mobile_id"   --target_file="$install_dir/.env"
		ynh_replace_string --match_string="mobile_key" --replace_string="$mobile_key"  --target_file="$install_dir/.env"
		ynh_app_setting_set --app=$app --key=mobile_id --value=$mobile_id
		ynh_app_setting_set --app=$app --key=mobile_key --value=$mobile_key
		ynh_secure_remove --file="$install_dir/key.txt"
	popd
fi
pushd "$install_dir"
	ynh_exec_warn_less ynh_exec_as $app php$phpversion artisan config:cache
popd

#if ynh_version_gt "2.15.0" "${previous_version}" ; then
#	ynh_script_progression --message="Upgrading for 2.15.0..."
#	pushd "$install_dir"
#		ynh_exec_warn_less ynh_exec_as $app php$phpversion artisan monica:moveavatarstophotosdirectory
#	popd
#fi

# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$install_dir/.env"

chmod 400 "$install_dir/.env"
chown $app:$app "$install_dir/.env"

update-alternatives --set php /usr/bin/php${YNH_PHP_VERSION}

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

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