#!/bin/bash

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

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

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

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICES
#=================================================
ynh_script_progression --message="Stopping systemd services..." --weight=1

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

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

# Create dedicated users (if not existing)
ynh_system_user_create --username="$app-guacd"
ynh_system_user_create --username="$app-tomcat"

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

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	ynh_script_progression --message="Upgrading source files..." --weight=1

	# Download, check integrity, uncompress and patch the source from app.src
	setup_sources
fi

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

# Create a dedicated NGINX config
ynh_add_nginx_config

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

# Guacamole depends on Apache Tomcat.
# But installing Tomcat from the Debian repos automatically enables and starts it.
# So we stop and disable it, unless Tomcat was already enabled or started by the user beforehand

tomcat_enabled=""
tomcat_active=""
if [ "$(systemctl cat $tomcat_version --quiet)" ]; then
	if (systemctl is-enabled $tomcat_version --quiet); then
		tomcat_enabled=1
	fi
	if (systemctl is-active $tomcat_version --quiet); then
		tomcat_active=1
	fi
fi

if [ ! $tomcat_enabled ]; then
	systemctl disable $tomcat_version --quiet
fi
if [ ! $tomcat_active ]; then
	systemctl stop $tomcat_version --quiet
fi

#=================================================
# SPECIFIC UPGRADE
#=================================================
# COMPILE GUACD
#=================================================
ynh_script_progression --message="Compiling guacd..." --weight=30

pushd "$install_dir/.guacd-src"
	LDFLAGS="-lrt" ./configure --enable-allow-freerdp-snapshots --prefix="$install_dir" --datadir="$install_dir" --with-freerdp-plugin-dir="$install_dir/lib/x86_64-linux-gnu/freerdp2"
	ynh_exec_warn_less env LDFLAGS="-lrt" make
	ynh_exec_warn_less env LDFLAGS="-lrt" make install
popd

#=================================================
# SETUP TOMCAT
#=================================================

mkdir -p "$install_dir/etc/"
ln -s -f "$install_dir/etc/$tomcat_version" "$install_dir/var/lib/$tomcat_version/conf"
ln -s -f "/var/log/$app/tomcat/" "$install_dir/var/lib/$tomcat_version/logs"
ln -s -f "/var/cache/$app-$tomcat_version" "$install_dir/var/lib/$tomcat_version/work"

#=================================================
# CREATE LOG DIRS
#=================================================

mkdir -p "/var/log/$app/tomcat/" "/var/log/$app/guacd/" "/var/log/$app/tomcat/"

#=================================================
# UPDATE CONFIG FILES
#=================================================

ynh_add_config --template="guacamole.properties" --destination="$install_dir/etc/guacamole/guacamole.properties"

ynh_replace_string --match_string="8080" --replace_string="$port" --target_file="$install_dir/etc/$tomcat_version/server.xml"

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

# Create dedicated systemd configs
ynh_add_systemd_config --service="$app-guacd" --template="guacd.service"
ynh_add_systemd_config --service="$app-tomcat" --template="tomcat.service"

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

_set_permissions

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

# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --logfile="/var/log/$app/tomcat/catalina.out" --non-append --specific_user="$app-tomcat/$app-tomcat"

#=================================================
# INTEGRATE SERVICES IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating services in YunoHost..." --weight=1

yunohost service add "$app-guacd" --description="Guacamole server" --log="/var/log/$app/guacd.log"
yunohost service add "$app-tomcat" --description="Guacamole client" --log="/var/log/$app/tomcat.log"

#=================================================
# START SYSTEMD SERVICES
#=================================================
ynh_script_progression --message="Starting systemd services..." --weight=1

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

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

# Create a dedicated Fail2Ban config
ynh_add_fail2ban_config --logpath="/var/log/syslog" --failregex='o.a.g.r.auth.AuthenticationService - Authentication attempt from <HOST> for user "[^"]*" failed\.$'

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

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