#!/bin/sh
#
# /etc/network/if-pre-up.d/wireless-tools-snooze
#
# By Stefan Tomanek <stefan.tomanek@wertarbyte.de>
# http://wertarbyte.de/debian/
#
# This script can be placed in /etc/network/if-pre-up.d/ to buy
# some more time for access point association. Some wifi chipsets
# also give up after missing one association, so the script does
# trigger a configurable number of reassociations.

IWCONFIG=/sbin/iwconfig
IFCONFIG=/sbin/ifconfig

if [ ! -x $IWCONFIG ]; then
  exit 0
fi

if [ "$IF_WIRELESS_SNOOZE" != "yes" ]; then
  exit 0
fi

if [ "$IF_WIRELESS_MODE" != "managed" ]; then
  echo "Not a managed interface, snooze disabled" >&2
  exit 0
fi

is_associated() {
  [ "$(/sbin/iwgetid --raw --ap "$IFACE")" != "00:00:00:00:00:00" ]
}

trigger_association() {
  $IWCONFIG "$IFACE" \
    ap "${IF_WIRELESS_AP:-any}" \
    essid "${IF_WIRELESS_ESSID:-any}" \
    channel "${IF_WIRELESS_CHANNEL:-auto}"
}

# bring up the interface to start association
$IFCONFIG "$IFACE" up

COUNTDOWN=${IF_WIRELESS_SNOOZE_COUNTDOWN:-10}
while ! is_associated && [ "$COUNTDOWN" -ge 0 ]; do
    echo "Device $IFACE is not yet associated... ($COUNTDOWN tries left)" >&2
    if [ "${IF_WIRELESS_WAKEUP:-no}" = "yes" ]; then
        echo "Waking it up..." >&2
        trigger_association
    fi
    COUNTDOWN=$(($COUNTDOWN-1))
    sleep 1
done

if is_associated; then
    echo "Wireless interface $IFACE successfully associated" >&2
else
    echo "Giving up on interface $IFACE, good luck." >&2
fi
