#!/bin/bash

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

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

#=================================================
# STANDARD REMOVE
#=================================================
# REMOVE SERVICE INTEGRATION IN YUNOHOST
#=================================================

if [ "$domain" != "$mqtt_domain" ]; then
	ynh_script_progression --message="Removing system configurations related to Mosquitto..." --weight=1
	yunohost service remove mosquitto
	ynh_secure_remove --file="/etc/mosquitto/conf.d"
	ynh_secure_remove --file="/usr/share/yunohost/hooks/conf_regen/95-nginx_domoticz"
	ynh_secure_remove --file="/etc/nginx/conf.d/"$mqtt_domain".d/mqtt_"$app".conf"
	yunohost tools regen-conf postfix
	ynh_secure_remove --file="/var/log/mosquitto"
fi

ynh_script_progression --message="Removing system configurations related to $app..." --weight=1
if ynh_exec_warn_less yunohost service status "$app" >/dev/null
then
	ynh_script_progression --message="Removing $app service integration..."
	yunohost service remove "$app"
fi

# Remove the dedicated systemd config
ynh_remove_systemd_config

ynh_secure_remove --file="/etc/nginx/conf.d/"$domain".d/api_"$app".conf"
ynh_remove_nginx_config

ynh_remove_logrotate

ynh_remove_fail2ban_config

ynh_secure_remove --file="/var/log/$app"

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

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