#!/bin/bash
###########################################################################
### FILE:       essidscan
### PURPOSE:    Scan for ESSID corresponding to available WLAN access points
###
### USAGE:      Normally called by ifup(8) based on a "mapping" stanza in
###             /etc/network/interfaces.  The network interface should be
###             supplied as the first and only argument; each known SSID
###             should be supplied on standard input, optionally followed
###             by a mapping.
###
###             Based on the result of the scan, one of the following is
###             printed on standard output:
###             - If at least one of the supplied ESSIDs is discovered,
###               its corresponding mapping (or the ESSID itself is no
###               mapping is provided).
###
###             - If none of the supplied ESSIDs are discovered, the
###               word "DEFAULT".
###
###             - If the interface does not support WLAN extensions,
###               an empty line is printed.
###
###             A sample /etc/network/interfaces may look like this:
###
###                  mapping hotplug
###                      script /etc/network/essidscan
###                      map SSIDONE  wlan-work
###                      map SSIDTWO  wlan-work
###                      map HOME
###
###                  iface wlan-work inet dhcp
###                      wireless-key     1234-5678-9ABC-DEF0-1234-5678-9A
###                      wireless-keymode restricted
###
###                  iface HOME inet static
###                      address          192.168.2.4
###                      netmask          255.255.255.0
###                      gateway          192.168.2.1
###                      wireless-essid   HOME
###                      wireless-key     s:somepassword
###                      wireless-keymode open
###
###                  iface DEFAULT inet dhcp
###########################################################################


### Some settings
    iwlist=/sbin/iwlist
    ifconf=/sbin/ifconfig
    grep=/bin/grep

### Sanity checks
    if [ -z "$1" ]
    then
        echo "Usage: $0 <interface>"   >&2
        exit 1

    elif [ ! -x "$iwlist" ]
    then
        echo "Missing \`$iwlist' -- install wireless-tools first." >&2
        exit 1
    fi


### Save the current state of the interface, before bringing it up
### to perform a SSID scan.  If it was down, bring it down again
### after the scan.

    $ifconf "$1" | $grep -qs " UP " && wasup=true || wasup=false

    $wasup || $ifconf $1 up
    cells=$(iwlist $1 scanning 2>&1)
    $wasup || $ifconf $1 down


### See if we find any of the interfaces given on the standard input
    if [ "${cells//Scan completed}" != "${cells}" ]
    then
        iface=DEFAULT
    else
        iface=
    fi

    while read essid mapping
    do
        if [ "${cells//ESSID:\"$essid\"}" != "$cells" ]
        then
            iface=${mapping:-$essid}
            break
        fi
    done

    echo $iface
