#!/bin/bash

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

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

#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
port_web=$(ynh_app_setting_get --app=$app --key=port_web)
port_rend=$(ynh_app_setting_get --app=$app --key=port_rend)
upstream_version=$(ynh_app_upstream_version)

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

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=10

# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
	# Restore it if the upgrade fails
	ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

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

#move home folder from previous /home/app to /home/yunohost.app/$app
#has to be after backup, otherwise backup will not find home folder and will fail
if [ ! $(getent passwd $app | cut -d: -f6 | grep yunohost.app) ]
then
	ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log"
	usermod -m -d /home/yunohost.app/$app $app
fi

#10.17.0, 10.17.1 & 10.16.0 had a bug that overwrite the media path at startup of the service. 
#https://github.com/UniversalMediaServer/UniversalMediaServer/issues/2864
#This will reinitialize it to /home/yunohost.multimedia/share
current_version=$(ynh_app_upstream_version --manifest="/etc/yunohost/apps/$app/manifest.json")
if [ $current_version == "10.17.0" ] || [ $current_version == "10.16.0" ]
then
	ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log"
	ynh_replace_string --match_string="folders =/opt/yunohost/$app,/home/yunohost.app/$app" \
		--replace_string="folders=/home/yunohost.multimedia/share" \
		--target_file="/home/yunohost.app/$app/.config/UMS/UMS.conf"
	ynh_replace_string --match_string="folders_monitored =/opt/yunohost/$app,/home/yunohost.app/$app" \
		--replace_string="folders_monitored=/home/yunohost.multimedia/share" \
		--target_file="/home/yunohost.app/$app/.config/UMS/UMS.conf"
fi

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=2

ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log"

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

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	ynh_script_progression --message="Upgrading source files..." --weight=5
	
	# Download, check integrity, uncompress and patch the source from app.src
	ynh_add_config --template="../conf/app.src.default" --destination="../conf/app.src"
	ynh_setup_source --dest_dir="$final_path"
	tar xfvz $final_path/app.tgz --strip-component=1 --directory=$final_path/
	ynh_secure_remove $final_path/app.tgz
fi

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

# Create a dedicated NGINX config
ynh_add_nginx_config "port_web"

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

ynh_install_app_dependencies $pkg_dependencies

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

# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir=/home/yunohost.app/$app

#=================================================
# SPECIFIC UPGRADE
#=================================================
# ...
#=================================================

#Close unwanted open port in firewall
if yunohost firewall list | grep -q "\- $port_web$"
then
	ynh_script_progression --message="Closing port $port_web as it shouldn't be open..."
	yunohost firewall disallow TCP $port_web 2>&1
fi

#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1

# Create a dedicated systemd config
ynh_add_systemd_config

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

# Set permissions on app files
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R root:$app "$final_path"
chown root:$app "$final_path/UMS.sh"

#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1

# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append

#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=2

yunohost service add $app --description="A DLNA, UPnP and HTTP(S) Media Server." --log=/var/log/$app/$app.log --needs_exposed_ports $port_rend

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

ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"

#=================================================
# UPGRADE FAIL2BAN
#=================================================
#ynh_script_progression --message="Reconfiguring Fail2Ban..." --weight=1

# Create a dedicated Fail2Ban config
#ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="Regex to match into the log for a failed login"

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1

ynh_systemd_action --service_name=nginx --action=reload

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

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