#!/bin/sh

# Copyright (c) 2014, AllSeen Alliance. All rights reserved.
#
#    Permission to use, copy, modify, and/or distribute this software for any
#    purpose with or without fee is hereby granted, provided that the above
#    copyright notice and this permission notice appear in all copies.
#
#    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

device=`iw dev | grep Interface | cut -f 2 -s -d" "`
# Sometimes right after boot, the device is not ready yet
if [ -n $device ]; then
    iw $device scan |
    grep -e "^BSS" -e "signal:" -e "SSID:" -e "capability:" -e "RSN:" -e "WPA:" -e "Pairwise ciphers" -e "Authentication suites" |
    {
    while read -r line; do
        case "$line" in
            BSS* )
                ## Handling a new SSID. Print out previously processed SSID only if it is defined.
                ## Note the different syntax based on WPA/WPA2 VS Open or WEP
                if [ "$enc" == "Open" ] || [ "$enc" == "WEP" ]; then
                    printf "%s\t%s\t%s\n" "$quality" "$enc" "$ssid"
                elif [ -n "$ssid" ]; then
                    printf "%s\t%s-%s-%s-%s\t%s\n" "$quality" "$enc" "$ccmp" "$tkip" "$psk" "$ssid"
                fi

                ## Initialize the variables
                ssid=""
                enc="Open"
                ccmp=""
                tkip=""
                psk=""
                quality=""
                ;;
            SSID:* )
                ## Set the ssid variable
                ssid=$(echo "$line" | sed s/"SSID: "//)
                ;;
            signal:* )
                ## Set signal quality variable
                quality=$(echo "$line" | sed s/"signal: "//)
                ;;
            capability:* )
                ## Encryption is on. We assume it's WEP by default, if we later find out it's WPA or WPA2 we override it
                if [ $(echo $line | grep "Privacy" | wc -l) -ne 0 ]; then
                    enc="WEP"
                fi
                ;;
            RSN:* )
                ## Set Encryption to be WPA2
                enc="WPA2"
                ;;
            WPA* )
                enc="WPA"
                ;;
            "* Pairwise ciphers:"* )
                ## Set the appropriate cipher variables
                if [ $(echo $line | grep "CCMP" | wc -l) -ne 0 ]; then
                    ccmp="CCMP"
                fi
                if [ $(echo $line | grep "TKIP" | wc -l) -ne 0 ]; then
                    tkip="TKIP"
                fi
                ;;
            "* Authentication suites:"* )
                ## Set the appropriate Authentication Suite variable
                if [ $(echo $line | grep "PSK" | wc -l) -ne 0 ]; then
                    psk="PSK";
                fi
                ;;
            * ) ;;
       esac
    done

    if [ "$enc" == "Open" ] || [ "$enc" == "WEP" ]; then
        printf "%s\t%s\t%s\n" "$quality" "$enc" "$ssid"
    elif [ -n "$ssid" ]; then
        printf "%s\t%s-%s-%s-%s\t%s\n" "$quality" "$enc" "$ccmp" "$tkip" "$psk" "$ssid"
    fi
    }
fi