#!/bin/bash

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

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

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

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get --app=$app --key=domain)
port=$(ynh_app_setting_get --app=$app --key=port)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name

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

# Remove the service from the list of services known by YunoHost (added from `yunohost service add`)
yunohost service remove snmpd
yunohost service remove zabbix-server
yunohost service remove zabbix-agent

#=================================================
# STOP AND REMOVE SERVICE
#=================================================
ynh_script_progression --message="Stopping and removing the systemd service..."

# Remove the dedicated systemd config
timeout 5 systemctl stop zabbix-server || killall zabbix-server
systemctl disable zabbix-server --quiet
killall zabbix-server

timeout 5 systemctl stop zabbix-agent || killall zabbix-agentd
systemctl disable zabbix-agent --quiet
killall zabbix-agentd

#=================================================
# REMOVE THE MYSQL DATABASE
#=================================================
ynh_script_progression --message="Removing the MySQL database..."

# Remove a database if it exists, along with the associated user
ynh_mysql_remove_db --db_user=$db_user --db_name=$db_name

#=================================================
# REMOVE APP MAIN DIR
#=================================================
ynh_script_progression --message="Removing app main directory..."

# Remove the app directory securely
ynh_secure_remove --file="$final_path"

#=================================================
# REMOVE NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Removing NGINX web server configuration..."

# Remove the dedicated NGINX config
ynh_remove_nginx_config

#=================================================
# REMOVE PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Removing PHP-FPM configuration..."

# Remove the dedicated PHP-FPM config
ynh_remove_fpm_config

#=================================================
# REMOVE DEPENDENCIES
#=================================================
ynh_script_progression --message="Removing dependencies..."

# Remove config file detection
delete_initZabbixConf

DEBIAN_FRONTEND=noninteractive apt-get purge zabbix-release -y

# Remove metapackage and its dependencies
ynh_remove_app_dependencies

# Force removing if ynh_remove_app_dependencies not work (old zabbix version)
for zabbix_pkg in $(apt list --installed | grep -Po "\K(zabbix-.*)(?=/)")
do
	DEBIAN_FRONTEND=noninteractive apt-get purge --allow-change-held-packages "$zabbix_pkg" -y
done

remove_zabbix_repo

#=================================================
# SPECIFIC REMOVE
#=================================================
# REMOVE VARIOUS FILES
#=================================================
ynh_script_progression --message="Removing various files..."

# Remove a directory securely
if [ -d /etc/zabbix ] ;then ynh_secure_remove --file="/etc/$app";fi

# Remove the log files
if [ -d /var/log/zabbix ] ;then ynh_secure_remove --file="/var/log/$app";fi

# Remove the pid/socket files
if [ -d /run/zabbix ] ;then ynh_secure_remove --file="/run/zabbix";fi

# Remove the sudoers file
if [ -f /etc/sudoers.d/zabbix ] ;then ynh_secure_remove --file="/etc/sudoers.d/zabbix";fi

#=================================================
# GENERIC FINALIZATION
#=================================================
# REMOVE DEDICATED USER
#=================================================
ynh_script_progression --message="Removing the dedicated system user..."

# Delete a system user
ynh_system_user_delete --username=$app

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

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