#!/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=2

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get --app=$app --key=domain)
port=$(ynh_app_setting_get --app=$app --key=port)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)

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

# Remove the service from the list of services known by YunoHost (added from `yunohost service add`)
if ynh_exec_warn_less yunohost service status pihole-FTL >/dev/null
then
	ynh_script_progression --message="Removing $app service integration..." --weight=2
	yunohost service remove pihole-FTL
fi

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

ynh_systemd_action --service_name=pihole-FTL --action=stop

# Restore dnsmasq as main DNS resolver
# Move dnsmasq back to its original place
#if [ -e "/usr/sbin/dnsmasq.backup_by_pihole" ]
#then	# Remove dnsmasq only if we have its backup
#	ynh_secure_remove --file="/usr/sbin/dnsmasq"
#	mv /usr/sbin/dnsmasq.backup_by_pihole /usr/sbin/dnsmasq
#fi

# Move back the service configuration for dnsmasq
#ynh_secure_remove --file="/etc/systemd/system/dnsmasq.service"
#mv /lib/systemd/system/.dnsmasq.service.backup_by_pihole /lib/systemd/system/dnsmasq.service
#mv /etc/init.d/.dnsmasq.backup_by_pihole /etc/init.d/dnsmasq
systemctl unmask dnsmasq.service

#ynh_exec_warn_less systemctl enable dnsmasq --quiet
# Reload systemd config
systemctl daemon-reload

ynh_secure_remove --file="/etc/init.d/pihole-FTL"
ynh_secure_remove --file="/usr/bin/pihole-FTL"
ynh_secure_remove --file="/var/run/pihole-FTL.pid"
ynh_secure_remove --file="/var/run/pihole-FTL.port"

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

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

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

# Remove the dedicated NGINX config
ynh_remove_nginx_config

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

# Remove the dedicated PHP-FPM config
ynh_remove_fpm_config

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

# Remove metapackage and its dependencies
ynh_remove_app_dependencies

#=================================================
# CLOSE A PORT
#=================================================

if yunohost firewall list | grep -q "\- $port$"
then
	ynh_script_progression --message="Closing port $port..." --weight=1
	ynh_exec_warn_less yunohost firewall disallow TCP $port
fi

if yunohost firewall list | grep -q "\- 67$"
then
	ynh_script_progression --message="Closing port 67..." --weight=1
	ynh_exec_warn_less yunohost firewall disallow UDP 67
fi

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

# Remove a cron file
ynh_secure_remove --file="/etc/cron.d/$app"

# Remove the log files
ynh_secure_remove --file="/var/log/$app"

# Remove main script
ynh_secure_remove --file="$PI_HOLE_BIN_DIR/pihole"
ynh_secure_remove --file="/etc/bash_completion.d/pihole"

# Remove sudoer file
ynh_secure_remove --file="/etc/sudoers.d/pihole"

# Remove storage directory
ynh_secure_remove --file="$PI_HOLE_CONFIG_DIR"
# Remove app directory
ynh_secure_remove --file="$PI_HOLE_INSTALL_DIR"
# Remove local clone of the repository
ynh_secure_remove --file="$PI_HOLE_LOCAL_REPO"

#=================================================
# REMOVE DNSMASQ CONFIG
#=================================================
ynh_script_progression --message="Removing Dnsmasq config..." --weight=2

ynh_secure_remove --file="/etc/dnsmasq.d/03-pihole-wildcard.conf"

#=================================================
# CLEAN /etc/hosts
#=================================================
ynh_script_progression --message="Clean /etc/hosts" --weight=1

# Uncomment lines in /etc/hosts
ynh_replace_string --match_string="#Commented by pihole# " --replace_string="" --target_file=/etc/hosts

# And remove extra lines, added by PiHole
sed -i "/#Added by pihole#/d" /etc/hosts

#=================================================
# REMOVE CONF_REGEN HOOK
#=================================================
ynh_script_progression --message="Removing conf_regen hook..." --weight=1

ynh_systemd_action --service_name=dnsmasq --action=stop

ynh_secure_remove --file=/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app

ynh_exec_warn_less yunohost tools regen-conf dnsmasq

#=================================================
# RESTART DNSMASQ
#=================================================
ynh_script_progression --message="Restarting Dnsmasq..." --weight=1

ynh_systemd_action --service_name=dnsmasq --action=restart

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

# Dirty hack to remove correctly the user
killall -u $app

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

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

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