#!/bin/sh

# (c)2003 by Jan Krupa (krupaj@mobilnews.cz)
# http://www.mobilnews.cz/honza/

# -- Set your variables here -------------------------------------------------
PATH_IWCONFIG="/usr/local/sbin/iwconfig"
PATH_IWLIST="/usr/local/sbin/iwlist"
PATH_IFCONFIG="/sbin/ifconfig"
PATH_ARP="/sbin/arp"
PATH_HOST="/usr/bin/host"
PATH_HOSTNAME="/bin/hostname"

PATH_HOSTAP_WLANS="/proc/net/hostap"
PATH_WIRELESS="/proc/net/wireless"

# Path to images for status line. If you are running WeWiMo as web script,
# you'll probably need to add direct http location here
# e.g. http://somewhere/wewimo/img/1x1g.gif
IMG_1x1G="1x1g.gif"
IMG_1x1R="1x1r.gif"

# -- Do not change anything bellow this line if you are not sure what you are doing ;) --
HOSTNAME=`$PATH_HOSTNAME`;

HOSTAP_MAXSIGNAL=92
HOSTAP_MAXSIGNAL_NEWHOSTAP=276

WEWIMO_VERSION="0.0.4"
WEWIMO_TITLE="$HOSTNAME wlan stats"

echo "<html>";
echo "<title>$WEWIMO_TITLE</title>";
echo "<body>";

echo "<font size=+1><b>$WEWIMO_TITLE</b></font><br>";
echo "<br><hr size=1><br>";

for A_IFACE in `ls -1 $PATH_HOSTAP_WLANS`; do
  ifmode1=`$PATH_IWCONFIG $A_IFACE | grep "Mode" | awk -FMode: '{ printf "%s", $2; }' | awk -F\  '{ printf "%s", $1; }'`;
  m_channel=`$PATH_IWLIST $A_IFACE channel | grep "(channel" | awk -Fchannel\  '{ printf "%s", substr($2, 1, (length($2)-1)) }'`
  m_rx=`$PATH_IFCONFIG $A_IFACE | grep "RX bytes" | awk -FRX\ bytes: '{ printf "%s", $2 }' | awk -F\  '{ printf "%u", ($1/1024) }'`;
  m_tx=`$PATH_IFCONFIG $A_IFACE | grep "TX bytes" | awk -FTX\ bytes: '{ printf "%s", $2 }' | awk -F\  '{ printf "%u", ($1/1024) }'`;

  # Managed mode
  if [ $ifmode1 = "Managed" ]; then

    m_signal1=`cat $PATH_WIRELESS | grep $A_IFACE | awk -F\  '{ if (match($3, /./) == 0) { printf "%s", $3; } else { printf "%s", substr($3, 1, (length($3)-1)); } }'`
    # this should be checked before
    if [ 92 -lt $m_signal1 ]; then
      HOSTAP_MAXSIGNAL=$HOSTAP_MAXSIGNAL_NEWHOSTAP
    fi
    m_signal2=$[$HOSTAP_MAXSIGNAL - $m_signal1];
    m_signal1_img=`echo $HOSTAP_MAXSIGNAL $m_signal1 | awk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_signal2_img=`echo $HOSTAP_MAXSIGNAL $m_signal2 | awk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_snr1=`cat $PATH_WIRELESS | grep $A_IFACE | awk -F\  '{ if (match($4, /./) == 0) { printf "%s", $4; } else { printf "%s", substr($4, 1, (length($4)-1)); } }'`
    m_snr2=`cat $PATH_WIRELESS | grep $A_IFACE | awk -F\  '{ if (match($5, /./) == 0) { printf "%s", $5; } else { printf "%s", substr($5, 1, (length($5)-1)); } }'`
    m_snr=$[$m_snr1 - $m_snr2];
    
    echo "<font size=+1><b>$A_IFACE</b></font>";
    echo "(Managed, Channel $m_channel)<br>";

    echo "<b>S:</b> $m_signal1/$HOSTAP_MAXSIGNAL <img src=$IMG_1x1G border=0 width=$m_signal1_img height=10><img src=$IMG_1x1R border=0 width=$m_signal2_img height=10>";
    echo "<b>SNR:</b> $m_snr<br>";
    echo "<b>RX:</b> $m_rx Kb";
    echo "<b>TX:</b> $m_tx Kb";
    echo "<br><br>";
    
    echo "<hr size=1><br>";

  # Master mode
  elif [ $ifmode1 = "Master" ]; then
    echo "<font size=+1><b>$A_IFACE</b></font>";
    echo "(Master, Channel $m_channel)<br>";

    echo "<b>RX:</b> $m_tx Kb"; # switched - you can see it from CLIENT side
    echo "<b>TX:</b> $m_rx Kb<br>";
    echo "<br>";

    echo "<b>Connected clients:</b><br>";

    echo "<table cols=5 width=100% cellspacing=0 cellpadding=1 border=0>";
    echo "<tr> <td><b>Host</b></td> <td><b>MAC</b></td> <td><b>Signal</b></td> <td><b>RX</b></td> <td><b>TX</b></td> </tr>";

    tmp_color=0
    for A_MAC in `ls -1 $PATH_HOSTAP_WLANS/$A_IFACE | grep \:`; do
      am_file=$PATH_HOSTAP_WLANS/$A_IFACE/$A_MAC
      am_ip=a`$PATH_ARP -n | grep -i $A_MAC | awk '{ printf "%s", $1 }'`;
      if [ $am_ip = "a" ]; then
        am_ip="0.0.0.0";
	am_host="unknown";
      else
        am_ip=`echo $am_ip | awk -F\  '{ printf "%s", substr($1, 2, (length($1)-1)); }'`;
        am_host=`$PATH_HOST $am_ip | awk -F\  '{ printf "%s", $5 }' | awk -F. '{ printf "%s", $1 }'`;
	am_host=`echo $am_host | awk '{ if (match($1, /\(/) == 0) { printf "%s", $1; } else { printf "unresolved"; } }'`;
      fi
      am_rx=`cat $am_file | grep "rx_bytes" | awk -F= '{ printf "%u", ($2/1024) }'`
      am_tx=`cat $am_file | grep "tx_bytes" | awk -F= '{ printf "%u", ($2/1024) }'`
      am_signal1=`cat $am_file | grep "last_rx" | awk -F\  '{ printf "%s", $3 }' | awk -F= '{ printf "%s", $2 }'`
      # this should be checked before
      if [ 92 -lt $am_signal1 ]; then
        HOSTAP_MAXSIGNAL=$HOSTAP_MAXSIGNAL_NEWHOSTAP
      fi
      am_signal2=$[$HOSTAP_MAXSIGNAL - $am_signal1];
      am_signal1_img=`echo $HOSTAP_MAXSIGNAL $am_signal1 | awk -F\  '{ printf "%u", (100 / $1 * $2) }'`
      am_signal2_img=`echo $HOSTAP_MAXSIGNAL $am_signal2 | awk -F\  '{ printf "%u", (100 / $1 * $2) }'`

      if [ $tmp_color = 0 ]; then
        echo "<tr bgcolor=#DDDDDD>";
      else
        echo "<tr>";
      fi
      echo "<td>$am_host ($am_ip)</td>";
      echo "<td>$A_MAC</td>";
      echo "<td>$am_signal1/$HOSTAP_MAXSIGNAL <img src=$IMG_1x1G border=0 width=$am_signal1_img height=10><img src=$IMG_1x1R border=0 width=$am_signal2_img height=10></td>";
      echo "<td>$am_tx Kb</td>"; # switched - you can see it from CLIENT side
      echo "<td>$am_rx Kb</td>";
      echo "</tr>";
      
      tmp_color=$[ $tmp_color + 1 ]
      if [ 1 -lt $tmp_color ]; then
        tmp_color=0
      fi
    done

    echo "</table>";
    
    echo "<br><hr size=1><br>";
  
  # Ad-Hoc mode
  elif [ $ifmode1 = "Ad-Hoc" ]; then

    m_signal1=`cat $PATH_WIRELESS | grep $A_IFACE | awk -F\  '{ if (match($3, /./) == 0) { printf "%s", $3; } else { printf "%s", substr($3, 1, (length($3)-1)); } }'`
    # this should be checked before
    if [ 92 -lt $m_signal1 ]; then
      HOSTAP_MAXSIGNAL=$HOSTAP_MAXSIGNAL_NEWHOSTAP
    fi
    m_signal2=$[$HOSTAP_MAXSIGNAL - $m_signal1];
    m_signal1_img=`echo $HOSTAP_MAXSIGNAL $m_signal1 | awk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_signal2_img=`echo $HOSTAP_MAXSIGNAL $m_signal2 | awk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_snr1=`cat $PATH_WIRELESS | grep $A_IFACE | awk -F\  '{ if (match($4, /./) == 0) { printf "%s", $4; } else { printf "%s", substr($4, 1, (length($4)-1)); } }'`
    m_snr2=`cat $PATH_WIRELESS | grep $A_IFACE | awk -F\  '{ if (match($5, /./) == 0) { printf "%s", $5; } else { printf "%s", substr($5, 1, (length($5)-1)); } }'`
    m_snr=$[$m_snr1 - $m_snr2];
    
    echo "<font size=+1><b>$A_IFACE</b></font>";
    echo "(Ad-Hoc, Channel $m_channel)<br>";

    echo "<b>S:</b> $m_signal1/$HOSTAP_MAXSIGNAL <img src=$IMG_1x1G border=0 width=$m_signal1_img height=10><img src=$IMG_1x1R border=0 width=$m_signal2_img height=10>";
    echo "<b>SNR:</b> $m_snr<br>";
    echo "<b>RX:</b> $m_rx Kb";
    echo "<b>TX:</b> $m_tx Kb";
    echo "<br><br>";
    
    echo "<hr size=1><br>";

  # Unknown mode
  else
  
    echo "<font size=+1><b>$A_IFACE</b></font>";
    echo "(Unknown)<br>";

    echo "<br><hr size=1><br>";

  fi    

done

echo "<font size=-1>";
date
echo "<br>";
echo "Generated by <a href=\"http://www.mobilnews.cz/honza/en_prog_linux_wewimo.php\">WeWiMo</a> v$WEWIMO_VERSION.";
echo "</font><br>";

echo "</body>";
echo "</html>";
