#!/bin/bash

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

firmware_nonfree=$(ynh_app_setting_get --app=$app --key=firmware_nonfree)
service_name=$(ynh_app_setting_get --app=$app --key=service_name)

#=================================================
# CHECK VERSION
#=================================================
ynh_script_progression --message="Checking version..."

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."

# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
	# Restore it if the upgrade fails
	ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping the hotspot service ... (this may take some time)"

ynh_systemd_action --service_name=$service_name --action="stop" --log_path=systemd #--line_match="Started YunoHost Wifi Hotspot"

#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."

if [ -d /var/www/wifiadmin/ ]; then
    ynh_secure_remove /var/www/wifiadmin/
fi


if [ $firmware_nonfree = "yes" ]; then
	firmware_nonfree=1
	ynh_app_setting_set --app=$app --key=firmware_nonfree --value=$firmware_nonfree
elif [ $firmware_nonfree = "no" ]; then
	firmware_nonfree=0
	ynh_app_setting_set --app=$app --key=firmware_nonfree --value=$firmware_nonfree
fi

if [ -z $service_name ]; then
	service_name="ynh-hotspot"
	ynh_app_setting_set --app=$app --key=service_name --value=$service_name
fi

# Old stuff prior to 2.x

ip6_net=$(ynh_app_setting_get --app=$app --key=ip6_net)
if [ "$ip6_net" == "none" ]
then
    ip6_net=""
	ynh_app_setting_set --app=$app --key=ip6_net --value="$ip6_net"
fi

if [ -f /etc/nginx/conf.d/*.d/$app.conf ]; then
	ynh_secure_remove /etc/nginx/conf.d/*.d/$app.conf
    ynh_systemd_action --service_name=nginx --action=reload
fi

for php_path in $(ls /etc/php/*/fpm/pool.d/$app.conf 2> /dev/null); do
	ynh_secure_remove $php_path
done

if [ -d /var/www/$app ]; then
	ynh_secure_remove /var/www/$app
fi

[ -z "$(ynh_app_setting_get $app domain)" ] || ynh_app_setting_delete $app domain
[ -z "$(ynh_app_setting_get $app path)" ] || ynh_app_setting_delete $app path
[ -z "$(ynh_app_setting_get $app final_path)" ] || ynh_app_setting_delete $app final_path

if [ -e "/etc/sudoers.d/${app}_ynh" ]; then
  ynh_secure_remove "/etc/sudoers.d/${app}_ynh"
fi

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..."

# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app

#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..."

if [[ $firmware_nonfree -eq 1 ]]; then
	check_armbian_nonfree_conflict
	ynh_install_extra_app_dependencies --repo="deb http://deb.debian.org/debian $(ynh_get_debian_release) non-free" --package="$nonfree_firmware_packages"
else
	pkg_dependencies="$pkg_dependencies $free_firmware_packages"
fi

ynh_install_app_dependencies $pkg_dependencies

#=================================================
# SPECIFIC UPGRADE
#=================================================
# COPY CONFIGS
#=================================================
ynh_script_progression --message="Copying configuration..."

mkdir -pm 0755 /etc/dnsmasq.dhcpd/
chown root: /etc/dnsmasq.dhcpd/

install -b -o root -g root -m 0644 ../conf/hostapd.*.conf /etc/hostapd/
install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl

# Copy init script
install -o root -g root -m 0755 ../conf/$service_name /usr/local/bin/

# Copy openvpn scripts
mkdir -pm 0755 /etc/openvpn/scripts
mkdir -pm 0755 /etc/openvpn/scripts/route-up.d
mkdir -pm 0755 /etc/openvpn/scripts/route-down.d
install -o root -g root -m 0755 ../conf/openvpn_90-hotspot /etc/openvpn/scripts/route-up.d/90-hotspot
install -o root -g root -m 0755 ../conf/openvpn_90-hotspot /etc/openvpn/scripts/route-down.d/90-hotspot

#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..."

# Create a dedicated systemd config
ynh_add_systemd_config --service=$service_name

#=================================================
# GENERIC FINALIZATION
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."

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

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

hot_reload_usb_wifi_cards
wifi_device=$(iw_devices | awk -F\| '{ print $1 }')

if [[ -z $wifi_device ]]; then
	ynh_app_setting_set --app=$app --key=service_enabled --value=0
	wifi_device=""
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
	ynh_systemd_action --service_name=$service_name --action="start" --log_path=systemd #--line_match="Started YunoHost Wifi Hotspot"
fi

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

ynh_script_progression --message="Upgrade of $app completed"