#!/bin/bash

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

# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers

if systemctl -q is-enabled hostapd
then
    # Disable hostapd, we'll use hostapd@$app instead (for multissid support etc)
    systemctl disable hostapd --quiet 2>&1
    systemctl stop hostapd 2>&1
    systemctl mask hostapd 2>&1
fi

#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Configuring firewall..."

# Update firewall for DHCP
ynh_exec_warn_less yunohost firewall allow --no-upnp --ipv6 UDP 547
ynh_exec_warn_less yunohost firewall allow --no-upnp UDP 67

# Meh idk where to put this ... On RPi, by default wlan is blocked
if test -e /usr/sbin/rfkill && rfkill | grep wlan | grep -q -w 'blocked'
then
    ynh_print_info "Unblocking wlan interface..."
    /usr/sbin/rfkill unblock wlan
fi

#=================================================
# STANDARD RESTORATION STEPS
#=================================================

ynh_script_progression --message="Restoring configurations ..."

ynh_restore

yunohost service add "$service_name" --description "Creates a Wi-Fi access point" --test_status "systemctl is-active hostapd@$app" --need_lock

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting the hotspot service..."

hot_reload_usb_wifi_cards
if [[ -z "${wifi_device:-}" ]] || ! grep -q -F "$wifi_device" <(unused_iw_devices); then
	wifi_device=$(unused_iw_devices | head -n 1)
	ynh_app_setting_set --app=$app --key=wifi_device --value="${wifi_device}"
fi

if [[ -z "${wifi_device:-}" ]]; then
	ynh_app_setting_set --app=$app --key=service_enabled --value=0
else
	ynh_app_setting_set --app=$app --key=service_enabled --value=1
fi

# Start a systemd service if device is present
if [[ "${wifi_device:-}" == "" ]]; then
	echo "WARNING: Wifi Hotspot is not started because no wifi device was found (please, check the web admin)" >&2
else
    yunohost service start $service_name
fi

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

ynh_script_progression --message="Restoration completed for $app"