#!/bin/bash

# Wifi Hotspot app for YunoHost
# Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
# Contribute at https://github.com/labriqueinternet/hotspot_ynh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#=================================================
# 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)
firmware_nonfree=$(ynh_app_setting_get --app=$app --key=firmware_nonfree)
service_name=$(ynh_app_setting_get --app=$app --key=service_name)
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 yunohost service status $service_name >/dev/null 2>&1
then
	ynh_script_progression --message="Removing $app service"
	yunohost service stop $service_name
	yunohost service remove $service_name
fi

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

# Remove the dedicated systemd config
ynh_remove_systemd_config --service=$service_name

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

# Remove metapackage and its dependencies
ynh_remove_app_dependencies

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

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

ynh_secure_remove --file="/usr/local/bin/$service_name"

ynh_secure_remove --file="/usr/local/bin/iw_multissid"
ynh_secure_remove --file="/usr/local/bin/iw_devices"
ynh_secure_remove --file="/usr/local/bin/iw_ssids"
ynh_secure_remove --file="/usr/local/bin/ipv6_expanded"
ynh_secure_remove --file="/usr/local/bin/ipv6_compressed"

ynh_secure_remove --file="/etc/sudoers.d/${app}_ynh"

for FILE in $(ls /tmp/.ynh-hotspot-* 2>/dev/null)
do
    ynh_secure_remove --file="$FILE"
done

# Remove confs
ynh_secure_remove --file="/etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl"
ynh_secure_remove --file="/etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl"
for FILE in $(ls /etc/hostapd/hostapd.conf{.tpl?,} 2>/dev/null)
do
    ynh_secure_remove --file="$FILE"
done

# Remove packages
if [[ $firmware_nonfree -eq 0 ]]; then
  # Remove free firmwares
  if ! dpkg -l firmware-atheros &> /dev/null; then
    ynh_secure_remove --file="/lib/firmware/htc_7010.fw"
    ynh_secure_remove --file="/lib/firmware/htc_9271.fw"
  fi
fi

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

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

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


if yunohost firewall list | grep -q "\- 67$"
then
	ynh_script_progression --message="Closing port 67"
	ynh_exec_warn_less yunohost firewall disallow TCP 67
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"