#!/bin/sh

# shellcheck shell=dash

# kept for debugging purpose
# logger -t "ssid net/hotplug" "ssidchanger ACTION = $ACTION INTERFACE = $INTERFACE DEVICE = $DEVICE IFUPDATE_ADDRESSES = $IFUPDATE_ADDRESSES IFUPDATE_DATA = $IFUPDATE_DATA"

. /lib/functions.sh
. /lib/lib_ssid-changer.sh

# don't run ssid_changer, if the wizard wasn't run yet.
if [ ! -f /etc/config/ffwizard ]; then
    log "ffwizard didn't run yet. Cancelling scripts run."
    exit 0
fi

# if ssid_changer wasn't configured by wizard, add it to ffwizard-file
if ! grep ssid_changer /etc/config/ffwizard; then
    log "No ssid-changer config found. Writing one to /etc/config/ffwizard."
    uci_add ffwizard ssid_changer ssid_changer
    uci_set ffwizard ssid_changer enabled 1
    uci_commit
fi

# exit early, if ssid-changer is disabled or interface doesn't matter for wifi
ENABLED=$(uci_get ffwizard ssid_changer enabled)
if [ $? = 1 ]; then ENABLED=1; fi # default to enabled, if value not set
if [ "$ENABLED" = 0 ]; then
    exit 0
fi
# We care for changes on ffuplink and tnl_.* interfaces only. For every interface not matching this,
# do an early exit
if [ "$INTERFACE" != "ffuplink" ] && ! (echo "$INTERFACE" | grep -Eq 'tnl_.*'); then
    exit 0
fi

# delay execution for up to 280 seconds, to evenly distribute the pings
if [ "$DELAY" ]; then
    sleep $(($(dd if=/dev/urandom bs=2 count=1 2>&- | hexdump | if read -r line; then echo "0x${line#* }"; fi) % 280))
fi

# check, if we are online
is_internet_reachable # 0 success - online; 1 failure - offline
NET_STATE=$?

ONLINE_SSIDS=$(get_interfaces)
CHK_SSID=$(echo "$ONLINE_SSIDS" | cut -d' ' -f 1) # makes checking easier

if [ $NET_STATE = 0 ]; then # router online: switch to online, if not present already
    # abort if SSID is "online" already
    for HOSTAPD in /var/run/hostapd-phy*; do
        [ -e "$HOSTAPD" ] || break
        CURRSSID=$(grep -e "^ssid=" "$HOSTAPD" | cut -d'=' -f 2)
        if [ "$CURRSSID" = "$CHK_SSID" ]; then
            log "SSID online already. Nothing to change."
            exit 0
        fi
    done

    # loop over hostapd configs and try to switch any matching ID.
    for HOSTAPD in /var/run/hostapd-phy*; do
        [ -e "$HOSTAPD" ] || break
        for ONLINE_SSID in $ONLINE_SSIDS; do
            log "Internet was reached. Change SSID back to online..."
            sed -i "s~^ssid=$OFFLINE_SSID~ssid=$ONLINE_SSID~" "$HOSTAPD"
        done
    done
else # router offline: adjust ssid accordingly, if needed
    # abort if SSID is "offline" already
    for HOSTAPD in /var/run/hostapd-phy*; do
        [ -e "$HOSTAPD" ] || break
        CURRSSID=$(grep -e "^ssid=" "$HOSTAPD" | cut -d'=' -f2)
        if [ "$CURRSSID" = "$OFFLINE_SSID" ]; then
            log "SSID offline already. Nothing to change."
            exit 0
        fi
    done

    # loop over hostapd configs and try to switch any matching ID.
    for HOSTAPD in /var/run/hostapd-phy*; do
        [ -e "$HOSTAPD" ] || break
        for ONLINE_SSID in $ONLINE_SSIDS; do
            log "Didn't reach the internet. Change SSID to offline..."
            sed -i "s~^ssid=$ONLINE_SSID~ssid=$OFFLINE_SSID~" "$HOSTAPD"
        done
    done
fi

# send hup to hostapd to reload ssid if we touched file
killall -HUP hostapd
