#!/bin/bash

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

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

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=2

ynh_setup_source --source_id="main" --dest_dir="$install_dir" --full_replace=1 --keep="config.conf www/static/client.json"

ynh_add_config --template="config.conf" --destination="$install_dir/config.conf"
chmod 400 "$install_dir/config.conf"

ynh_add_config --template="client.json" --destination="$install_dir/www/static/client.json"
chmod 400 "$install_dir/www/static/client.json"

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

#=================================================
# REAPPLY SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression --message="Upgrading system configurations related to $app..." --weight=1

# Create a dedicated NGINX config
ynh_add_nginx_config

ynh_add_systemd_config
yunohost service add "$app" --description="Versatile web based messenger using IRC" --log="/var/log/$app/$app.log"

# Use logrotate to manage application logfile(s)
ynh_use_logrotate

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

# Start a systemd service
ynh_systemd_action --service_name="$app" --action="restart" --log_path="systemd"

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

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