#!/bin/bash

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

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

#=================================================
# REMOVE SERVICE FROM ADMIN PANEL
#=================================================

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

#=================================================
# STANDARD REMOVE
#=================================================
# STOP AND REMOVE SERVICE
#=================================================

# Remove the dedicated systemd config
ynh_script_progression --message="Stopping and removing the systemd service..." --weight=2
ynh_remove_systemd_config


#=================================================
# REMOVE DATA DIR
#=================================================

# Remove the data directory if --purge option is used
if [ "${YNH_APP_PURGE:-0}" -eq 1 ]
then
	ynh_script_progression --message="Removing app data directory..." --weight=1
	ynh_secure_remove --file="$calibre_dir"
fi

#=================================================
# REMOVE KEPUBIFY DIR
#=================================================

# Remove the data directory if --purge option is used
ynh_script_progression --message="Removing kepubify directory..." --weight=1
ynh_secure_remove --file="/opt/kepubify/$app"

#=================================================
# REMOVE NGINX CONFIGURATION
#=================================================

# Remove the dedicated nginx config
ynh_script_progression --message="Removing nginx web server configuration..." --weight=1
ynh_remove_nginx_config

#=================================================
# REMOVE LOGROTATE CONFIGURATION
#=================================================

# Remove the app-specific logrotate config
ynh_script_progression --message="Removing logrotate configuration..." --weight=1
ynh_remove_logrotate

#=================================================
# REMOVE FAIL2BAN CONFIGURATION
#=================================================
ynh_script_progression --message="Removing Fail2Ban configuration..." --weight=8

# Remove the dedicated Fail2Ban config
ynh_remove_fail2ban_config

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

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

#Restore Imagick policy
if [ -z /etc/ImageMagick-6/policy.xml ]; then
	ynh_replace_string --match_string="<policy domain="coder" rights="read" pattern="PDF" />" \
			--replace_string="<policy domain="coder" rights="none" pattern="PDF" />" \
			--target_file="/etc/ImageMagick-6/policy.xml"
fi

# Remove the log files
ynh_script_progression --message="Removing log file..." --weight=1
ynh_secure_remove "$log_file"
ynh_secure_remove "$access_log_file"

#=================================================
# MESSAGE TO USER
#=================================================
ynh_script_progression --message="Removal of $app completed" --last
# Remove the data directory if --purge option is used
if [ "${YNH_APP_PURGE:-0}" -eq 0 ]
then
	ynh_print_info "The library folder $calibre_dir was not deleted. Delete it manually if required"
fi
