#!/usr/bin/env bash

# exit if a command fails
set -o errexit

# exit if required variables are not set
set -o nounset

# check for dependencies
script_commands="iw"
missing_counter=0
for needed_command in ${script_commands}; do
  if ! hash "${needed_command}" >/dev/null 2>&1; then
    printf "Command not found in PATH: %s\n" "${needed_command}" >&2
    ((missing_counter++))
  fi
done
if ((missing_counter > 0)); then
  printf "Minimum %d commands are missing in PATH, aborting.\n" "${missing_counter}" >&2
  exit 1
fi

# create function displaying usage
usage() {
  echo -e "\nUsage: $0 <interface>"
  exit 1
}

# make sure the correct number of arguments are passed; if not, output syntax and exit
if [ "$#" -ne 1 ]; then
  usage
fi

# set interface variable
interface="$1"

# download oui database, if not present
oui_database="/tmp/oui.txt"
if [ ! -f "${oui_database}" ]; then
  curl http://standards-oui.ieee.org/oui/oui.txt > "${oui_database}"
fi

# find vendor from oui list
find_vendor () {
  local vendor_id
  vendor_id="$(echo "$1" | cut -d":" -f1-3 | tr "[:lower:]" "[:upper:]" | sed 's_:_-_g')"
  local vendor_name
  vendor_name="$(grep -m1 "${vendor_id}" "${oui_database}" | awk '{$1=$2=""; print $0}' | xargs | tr -d '[:cntrl:]')"
  if [ ! -z "${vendor_name}" ]; then
    echo "${vendor_name}"
  else
    echo "Unknown"
  fi
}

# parse out relevant information
essid="$(iw dev "${interface}" link | grep "SSID" | awk '{$1=""; print substr($0,2)}')"
bssid="$(iw dev "${interface}" link | grep "Connected" | awk '{print $3}')"
ap_vendor="$(find_vendor "${bssid}")"
rx_index="$(iw dev "${interface}" link | grep "rx bitrate" | awk '{print $5,$6}')"
tx_index="$(iw dev "${interface}" link | grep "tx bitrate" | awk '{print $5,$6}')"
rx_spacial_streams="$(iw dev "${interface}" link | grep "rx bitrate" | sed 's/short //g' | awk '{print $(NF-1),$NF}')"
tx_spacial_streams="$(iw dev "${interface}" link | grep "tx bitrate" | sed 's/short //g' | awk '{print $(NF-1),$NF}')"
client_mac="$(ip link | grep -A1 "${interface}" | grep -P "link/ether" | awk '{print $2}')"
client_vendor="$(find_vendor "${client_mac}")"
ip="$(ip -f inet addr show "${interface}" | grep -Po "inet \K[\d./,]+")"
freq="$(iw dev "${interface}" link | grep "freq" | awk '{print $2}')"
channel_width="$(iw dev "${interface}" link | grep "rx bitrate" | awk '{print $7}')"
signal="$(iw dev "${interface}" link | grep "signal" | awk '{$1=""; print substr($0,2)}')"
rx_phy_rate="$(iw dev "${interface}" link | grep "rx" | awk '{print $3}')"
rx_units="$(iw dev "${interface}" link | grep "rx" | awk '{print $4}' | sed 's/Bit/bit/')"
rx_mac_rate="$(echo "$(echo "${rx_phy_rate}" | cut -d " " -f1)*0.8" | bc)"
tx_phy_rate="$(iw dev "${interface}" link | grep "tx" | awk '{print $3}')"
tx_units="$(iw dev "${interface}" link | grep "tx" | awk '{print $4}' | sed 's/Bit/bit/')"
tx_mac_rate="$(echo "$(echo "${tx_phy_rate}" | cut -d " " -f1)*0.8" | bc)"

# identify rx modulation
if [ "${rx_index}" == "VHT-MCS 0" ]; then
  rx_modulation="BPSK"
elif [ "${rx_index}" == "VHT-MCS 1" ]; then
  rx_modulation="QPSK"
elif [ "${rx_index}" == "VHT-MCS 2" ]; then
  rx_modulation="PQSK"
elif [ "${rx_index}" == "VHT-MCS 3" ]; then
  rx_modulation="16-QAM"
elif [ "${rx_index}" == "VHT-MCS 4" ]; then
  rx_modulation="16-QAM"
elif [ "${rx_index}" == "VHT-MCS 5" ]; then
  rx_modulation="16-QAM"
elif [ "${rx_index}" == "VHT-MCS 6" ]; then
  rx_modulation="64-QAM"
elif [ "${rx_index}" == "VHT-MCS 7" ]; then
  rx_modulation="64-QAM"
elif [ "${rx_index}" == "VHT-MCS 8" ]; then
  rx_modulation="256-QAM"
elif [ "${rx_index}" == "VHT-MCS 9" ]; then
  rx_modulation="256-QAM"
else
  rx_modulation="n/a"
fi

# identify tx modulation
if [ "${tx_index}" == "VHT-MCS 0" ]; then
  tx_modulation="BPSK"
elif [ "${tx_index}" == "VHT-MCS 1" ]; then
  tx_modulation="QPSK"
elif [ "${tx_index}" == "VHT-MCS 2" ]; then
  tx_modulation="PQSK"
elif [ "${tx_index}" == "VHT-MCS 3" ]; then
  tx_modulation="16-QAM"
elif [ "${tx_index}" == "VHT-MCS 4" ]; then
  tx_modulation="16-QAM"
elif [ "${tx_index}" == "VHT-MCS 5" ]; then
  tx_modulation="16-QAM"
elif [ "${tx_index}" == "VHT-MCS 6" ]; then
  tx_modulation="64-QAM"
elif [ "${tx_index}" == "VHT-MCS 7" ]; then
  tx_modulation="64-QAM"
elif [ "${tx_index}" == "VHT-MCS 8" ]; then
  tx_modulation="256-QAM"
elif [ "${tx_index}" == "VHT-MCS 9" ]; then
  tx_modulation="256-QAM"
else
  tx_modulation="n/a"
fi

# display formatted output
echo
echo " ESSID: ${essid}"
echo " My IP: ${ip}"
echo
echo "  AP MAC: ${bssid} (${ap_vendor})"
echo " STA MAC: ${client_mac} (${client_vendor})"
echo
echo " Frequency: ${freq}MHz (${channel_width} channel)"
echo "    Signal: ${signal}"
echo
echo " PHY Rx: ${rx_phy_rate}${rx_units} / Tx: ${tx_phy_rate}${tx_units}   (Physical link speed)"
echo " MAC Rx: ${rx_mac_rate}${rx_units} / Tx: ${tx_mac_rate}${tx_units}   (Best-case throughput)"
echo
echo " Modulation:"
echo " Rx: ${rx_modulation} / Tx: ${tx_modulation}"
