#!/bin/bash

#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

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

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# MODIFY URL IN NGINX CONF
#=================================================
ynh_script_progression --message="Updating NGINX web server configuration..." --weight=1

ynh_change_url_nginx_config

#=================================================
# SPECIFIC MODIFICATIONS
#=================================================
# SET NEW URL
#=================================================
ynh_script_progression --message="Setting the new URL..." --weight=1

if [ $change_domain -eq 1 ]
then
    ynh_replace_string --match_string="\$wgServer = \"https://$old_domain\";" --replace_string="\$wgServer = \"https://$new_domain\";" --target_file="$install_dir/LocalSettings.php"
fi

if [ $change_path -eq 1 ]
then
    if [ "$old_path" = "/" ]; then
        old_scriptpath=""
    else
        old_scriptpath=$old_path
    fi

    if [ "$new_path" = "/" ]; then
        new_scriptpath=""
    else
        new_scriptpath=$new_path
    fi

    ynh_replace_string --match_string="\$wgScriptPath = \"$old_scriptpath\";" --replace_string="\$wgScriptPath = \"$new_scriptpath\";" --target_file="$install_dir/LocalSettings.php"
fi

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

ynh_script_progression --message="Change of URL completed for $app" --last