#!/bin/bash

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

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

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=1

ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd"

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

ynh_change_url_nginx_config

#=================================================
# UPDATING A CONFIGURATION
#=================================================
ynh_script_progression --message="Updating the configuration file..." --weight=1

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

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

#=================================================
# RUN DB MIGRATION
#=================================================
ynh_script_progression --message="Running DB initial migration..." --weight=3

pushd "$install_dir"
	ynh_use_nodejs
	ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH yarn db:migrate
popd

#=================================================
# GENERIC FINALISATION
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1

ynh_systemd_action --service_name=$app --action="start" --log_path="systemd"

#=================================================
# SLACK WARNING
#=================================================
ynh_print_info --message="Don't forget to add a new redirction url in your Slack app settings !"


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

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