#!/bin/bash

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

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

#=================================================
# REMOVE SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression --message="Removing system configurations related to $app..." --weight=1

# 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
    yunohost service remove "pihole-FTL"
fi

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 the dedicated NGINX config
ynh_remove_nginx_config

# Remove the dedicated PHP-FPM config
ynh_remove_fpm_config

#=================================================
# 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

#=================================================
# 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 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

#=================================================
# REMOVE DEDICATED USER
#=================================================

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

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

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