#!/bin/bash
IFACE={{ discovered_wireless_iface }}
RASPBIAN=0
NETPLAN=0
SSID="NA"
# when we get here br0 should be available and dbus wpa_supplicant was started if enabled. None
# of the backends that use wpa_supplicant should be active yet based on the Before= After= lines
# in the iiab-wifi-test.service unit file.

# covers systemd-networkd
if [ -f /etc/wpa_supplicant/wpa_supplicant-$IFACE.conf ]; then
    SSID=`grep ssid /etc/wpa_supplicant/wpa_supplicant-$IFACE.conf | awk -F = '{print $2}' | sed -r s/\"// | sed -r s/\"//`
fi

# covers raspbian
if [ -f /etc/wpa_supplicant/wpa_supplicant.conf ]; then
    RASPBIAN=1
    if /usr/sbin/rfkill list wifi | grep -q "Soft blocked: yes" ; then
        echo "unblocking WiFi"
        rfkill unblock wifi
    fi
    SSID=`grep ssid /etc/wpa_supplicant/wpa_supplicant.conf | awk -F = '{print $2}' | sed -r s/\"// | sed -r s/\"//`
fi

# covers netplan's bugs workaround
# https://bugs.launchpad.net/ubuntu/+source/linux-firmware/+bug/1862760
# https://bugs.launchpad.net/netplan/+bug/1951586
# WiFi country code progress on arm64 OS's discussed on #3078
if [ -f /run/netplan/wpa-$IFACE.conf ] && ! $(grep country /run/netplan/wpa-$IFACE.conf); then
    NETPLAN=1
    SSID=`grep ssid /run/netplan/wpa-$IFACE.conf | awk -F = '{print $2}' | sed -r s/\"// | sed -r s/\"//`
    echo "cover netplan lack of country="
    sed -i 's|ctrl_interface=/run/wpa_supplicant|&\ncountry=US|' /run/netplan/wpa-$IFACE.conf
fi
# IIAB hint for NetworkManager
# could scrape /etc/NetworkManager/system-connections/ looking for ssid
if [ -f /etc/iiab/iiab.env ]; then
    source /etc/iiab/iiab.env
    if [ ! -z $CLIENT_SSID ]; then
       SSID=$CLIENT_SSID
    fi
fi
echo "ssid is $SSID"
if [[ $SSID == "" ]] || [[ $SSID == "NA" ]]; then
    echo "Couldn't find ssid $SSID to use exiting"
    if [ $NETPLAN -eq 1 ]; then
       echo "Netplan1"
    fi
    exit 0
fi
wpa_cli -i $IFACE scan > /dev/null
sleep 2
FREQ=`wpa_cli -i $IFACE scan_results | grep $SSID | awk '{print $2}'`
for result in $FREQ; do
    echo "frequency is $result for $SSID"
    if [ $result -lt 2485 ] && [ $result -gt 2407 ]; then
        FREQ2=$result
        break
    else
        echo "channel $result is 5Ghz - ignoring"
    fi
done
echo "Using $FREQ2 for $SSID"
if [[ $FREQ2 == "" ]]; then
    echo "Couldn't find frequency to use exiting"
    if [ $NETPLAN -eq 1 ]; then
        echo "Netplan2"
    fi
    exit 0
fi
# ubuntu on boot exits at this point timing - issue with wpa_cli and scanning
CHAN=$(($FREQ2 - 2407 ))
CHAN=$(($CHAN / 5 ))
echo "channel is $CHAN for $SSID"
HOSTAPD=`grep channel /etc/hostapd/hostapd.conf | awk -F = '{print $2}'`
echo "Hostapd set for $HOSTAPD"
if [ $CHAN -ne $HOSTAPD ]; then
    echo "Editing Hostapd for channel $CHAN"
    cp /etc/hostapd/hostapd.conf.iiab /etc/hostapd/hostapd.conf
    sed -i -e "s/^channel.*/channel=$CHAN/" /etc/hostapd/hostapd.conf
fi
systemctl stop wpa_supplicant
systemctl stop hostapd
systemctl start hostapd
systemctl start wpa_supplicant
if [ $NETPLAN -eq 1 ]; then
    echo "Netplan3"
    # This is more of a netplan workaround should go away.
    systemctl restart netplan-wpa-$IFACE.service
fi
exit 0
