#!/usr/bin/ash

check_net_device () {
    ip link show $1 > /dev/null 2>&1
}

poll_net_device () {
    local device=$1 seconds=${2//[!0-9]}

    [ "${seconds:-x}" = x ] && seconds=10

    check_net_device $device && return 0

    msg "Waiting $seconds seconds for network device $device ..." >&2
    while ! check_net_device $device && [ "$seconds" -gt 0 ]; do
        sleep 1
        seconds=$(( seconds - 1 ))
    done

    check_net_device $device
}

check_wpa_supplicant_done () {
    grep "CTRL-EVENT-CONNECTED" $1 > /dev/null
}

poll_wpa_completion () {
    local logfile=$1 seconds=${2//[!0-9]}

    [ "${seconds:-x}" = x ] && seconds=10

    check_wpa_supplicant_done $logfile && return 0

    msg "Waiting $seconds seconds for wpa_supplicant ..." >&2
    while ! check_wpa_supplicant_done $logfile && [ "$seconds" -gt 0 ]; do
        sleep 1
        seconds=$(( seconds - 1 ))
    done

    check_wpa_supplicant_done $logfile
}

run_hook ()
{
    local device="wlan0"
    local logfile="/tmp-wpa-supplicant-log"

    # wait for wlan-device
    poll_net_device $device 15

    msg "Starting wifi"

    # set wlan-device to up
    ip link set $device up || return 1

    # assocciate with wifi network
    wpa_supplicant -B -i $device -c /etc/wpa_supplicant/initcpio.conf -f $logfile

    # wait for wpa_supplicant
    poll_wpa_completion $logfile 15

    # wlan-device should now be connected and ready to be assigned an ip by the net hook
}

run_cleanuphook ()
{
    local device="wlan0"
    local logfile="/tmp-wpa-supplicant-log"

    # kill wpa_supplicant running in the background
    killall wpa_supplicant

    # set wlan-device link down
    ip link set $device down

    # wlan-device should now be fully disconnected from the wifi network
    rm $logfile
}
