#!/bin/bash

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

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

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

upstream_version=$(ynh_app_upstream_version)

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

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# 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 $install_dir $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,$install_dir" \
		--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,$install_dir" \
		--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
	ynh_setup_source --dest_dir="$install_dir" 
fi

chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:$app "$install_dir"
chown $app:$app "$install_dir/UMS.sh"

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

# Create a dedicated NGINX config
ynh_add_nginx_config

# Create a dedicated systemd config
ynh_add_systemd_config

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

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"

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

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