#!/bin/sh

# (c)2003 by Jan Krupa (krupaj@mobilnews.cz)
# http://www.mobilnews.cz/honza/
# WISP compatibility by Vic
# Rate add-on by Sameleon
# CZFree DNS add-on by Woody

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

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

# If you don't want to show all ifaces, you can disable it here
#DISABLE_IFACES="wlan0,wlan1"
DISABLE_IFACES="none"

# If you are running WeWiMo as CGI script change this value to 1
CGI_SCRIPT=0

# If you want to display uptime change this value to 1 (not yet fully implemeted -
# it will show other information, too)
DISPLAY_UPTIME=0

# If you want to add automatic refresh change this value to seconds (0 = no refresh)
WWW_REFRESH=0

# If you want to add CZFree DNS add-on change this value to 1
CZF_DNS=0

# If you are running WeWiMo in WISP (Linux distribution) change this value to 1
WISP_RUN=0
WISP_PATH_ETHERS="/etc/ethers"
WISP_HOSTNAME="ComputerName"

# -- Do not change anything bellow this line if you are not sure what you are doing ;) --
# ----------------------------------------------------------------------------

# -- Language definition -----------------------------------------------------
LNG_NAME="English"

LNG_CHANNEL="Channel"
LNG_BITRATE="Bit Rate"
LNG_UNKNOWN="unknown"
LNG_MODE_UNKNOWN="Unknown"
LNG_SIGNAL="S"
LNG_SNR="SNR"
LNG_DATARX="RX"
LNG_DATATX="TX"
LNG_RATERX="RX Rate"
LNG_RATETX="TX Rate"
LNG_MASTER_CONNECTED_CLIENTS="Connected clients"
LNG_MASTER_HOST="Host"
LNG_MASTER_IP="IP"
LNG_MASTER_MAC="MAC"
LNG_MASTER_SIGNAL="Signal"
LNG_MASTER_CLIENTS_COUNT="Active / Total Clients"
# ----------------------------------------------------------------------------

# -- Colors definition -------------------------------------------------------
COL_BG="#FFFFFF"
COL_TEXT="#000000"
COL_TEXT_INACTIVE="#808080"
COL_LINK="#0000FF"
COL_SBAR1="#00C000"
COL_SBAR2="#C00000"
COL_SBAR1_INACTIVE="#80C080"
COL_SBAR2_INACTIVE="#C08080"
COL_CL_BG1="#DDDDDD"
COL_CL_BG2="#FFFFFF"
# ----------------------------------------------------------------------------

if [ $WISP_RUN = 1 ]; then
  HOSTNAME=$WISP_HOSTNAME
else
  HOSTNAME=`$PATH_HOSTNAME`
fi

UPTIME=`$PATH_UPTIME`

HOSTAP_MAXSIGNAL=92

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

if [ $CGI_SCRIPT = 1 ]; then
  echo "Content-Type: text/html"
  echo
fi

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"

echo "<html>"

echo "<head>"
if [ $WWW_REFRESH != 0 ]; then
  echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"$WWW_REFRESH\">"
fi
echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
echo "<title>$WEWIMO_TITLE</title>"
echo "</head>"

echo "<body text=$COL_TEXT bgcolor=$COL_BG link=$COL_LINK vlink=$COL_LINK>"

echo "<font size=+1><b>$WEWIMO_TITLE</b></font><br>"
if [ $DISPLAY_UPTIME = 1 ]; then
  echo "<font size=-1><b>Uptime:</b> $UPTIME</font><br>"
fi
echo "<br><hr size=1><br>"

for A_IFACE in `ls -1 $PATH_HOSTAP_WLANS`; do

  if [ a`echo $DISABLE_IFACES | grep $A_IFACE` = "a" ]; then

  ifmode1=`$PATH_IWCONFIG $A_IFACE | grep "Mode" | gawk -FMode: '{ printf "%s", $2 }' | gawk -F\  '{ printf "%s", $1 }'`
  m_channel=`$PATH_IWLIST $A_IFACE channel | grep "(channel" | gawk -Fchannel\  '{ printf "%s", substr($2, 1, (length($2)-1)) }'`
  m_rx=`$PATH_IFCONFIG $A_IFACE | grep "RX bytes" | gawk -FRX\ bytes: '{ printf "%s", $2 }' | gawk -F\  '{ printf "%u", ($1/1024) }'`
  m_tx=`$PATH_IFCONFIG $A_IFACE | grep "TX bytes" | gawk -FTX\ bytes: '{ printf "%s", $2 }' | gawk -F\  '{ printf "%u", ($1/1024) }'`
  m_bitrate_tmp=`$PATH_IWCONFIG $A_IFACE | grep "Bit Rate"`
  m_bitrate=`echo $m_bitrate_tmp | gawk -FBit\ Rate '{ printf "%s", $2 }' | gawk -F\  '{ printf "%s", substr($1, 2, (length($1)-1)) }'`

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

    m_signal1=`cat $PATH_WIRELESS | grep $A_IFACE | gawk -F\  '{ if (match($3, /./) == 0) { printf "%s", $3 } else { printf "%s", substr($3, 1, (length($3)-1)) } }'`

    m_signal2=`expr $HOSTAP_MAXSIGNAL - $m_signal1`
    m_signal1_img=`echo $HOSTAP_MAXSIGNAL $m_signal1 | gawk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_signal2_img=`echo $HOSTAP_MAXSIGNAL $m_signal2 | gawk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_snr1=`cat $PATH_WIRELESS | grep $A_IFACE | gawk -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 | gawk -F\  '{ if (match($5, /./) == 0) { printf "%s", $5 } else { printf "%s", substr($5, 1, (length($5)-1)) } }'`
    m_snr=`expr $m_snr1 - $m_snr2`
    
    echo "<font size=+1><b>$A_IFACE</b></font>"
    echo "(Managed, $LNG_CHANNEL $m_channel, $LNG_BITRATE $m_bitrate)<br>"

    echo "<table width=170 cellspacing=0 cellpadding=0 border=0><tr>"
    echo "<td width=60><b>$LNG_SIGNAL:</b> $m_signal1/$HOSTAP_MAXSIGNAL</td>"
    echo "<td width=110>"

    echo "<table width=100 cellspacing=0 cellpadding=0 border=0><tr>"
    echo "<td width=$m_signal1_img height=12 bgcolor=$COL_SBAR1><font size=1>&nbsp;</font></td>"
    echo "<td width=$m_signal2_img bgcolor=$COL_SBAR2><font size=1>&nbsp;</font></td>"
    echo "</tr></table>"

    echo "</td>"
    echo "</tr></table>"
    
    echo "<b>$LNG_SNR:</b> $m_snr<br>"
    echo "<b>$LNG_DATARX:</b> $m_rx Kb"
    echo "<b>$LNG_DATATX:</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, $LNG_CHANNEL $m_channel, $LNG_BITRATE $m_bitrate)<br>"

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

    echo "<b>$LNG_MASTER_CONNECTED_CLIENTS:</b><br>"
    
    echo "<table width=100% cellspacing=0 cellpadding=2 border=0>"
    echo "<tr> <td align=left><b>$LNG_MASTER_HOST</b></td> <td align=left><b>$LNG_MASTER_IP</b></td> <td align=left><b>$LNG_MASTER_MAC</b></td> <td align=left><b>$LNG_MASTER_SIGNAL</b></td> <td align=right><b>$LNG_RATERX</b></td> <td align=right><b>$LNG_RATETX</b></td> <td align=right><b>$LNG_DATARX</b></td> <td align=right><b>$LNG_DATATX</b></td> </tr>"

    tmp_color=0
    count_clients_total=0
    count_clients_active=0
    for A_MAC in `ls -1 $PATH_HOSTAP_WLANS/$A_IFACE | grep \:`; do
      am_file=$PATH_HOSTAP_WLANS/$A_IFACE/$A_MAC
      count_clients_total=`expr $count_clients_total + 1`
      tmp_color_text=$COL_TEXT
      tmp_color_bar1=$COL_SBAR1
      tmp_color_bar2=$COL_SBAR2

      if [ $WISP_RUN = 1 ]; then
        am_ip=a`$PATH_ARP -n | grep -i $A_MAC | gawk '{ printf "%s", $1 }'`
	am_host=`cat $WISP_PATH_ETHERS | grep -i $A_MAC | gawk -F\  '{ printf "%s", $2 }'`
      else
        am_ip=a`$PATH_ARP -n | grep -m 1 -i $A_MAC | gawk '{ printf "%s", $1 }'`
	am_host=$LNG_UNKNOWN
      fi
      
      if [ $am_ip = "a" ]; then
        am_ip=" "
	tmp_color_text=$COL_TEXT_INACTIVE
        tmp_color_bar1=$COL_SBAR1_INACTIVE
        tmp_color_bar2=$COL_SBAR2_INACTIVE
      else
        count_clients_active=`expr $count_clients_active + 1`
        am_ip=`echo $am_ip | gawk -F\  '{ printf "%s", substr($1, 2, (length($1)-1)) }'`
        if [ $WISP_RUN != 1 ]; then
          am_host=`$PATH_HOST $am_ip | gawk -F\  '{ printf "%s", $5 }' | gawk -F. '{ printf "%s", $1 }'`
          if [ $CZF_DNS = 1 ]; then
 	    if [ "`echo \"$am_host\" | gawk '\$1 ~ /^wlan[0-9]/ { yes = \"1\" }; END { print yes }'`" == "1" ]; then
              am_host=`$PATH_HOST $am_ip | gawk -F\  '{ printf "%s", $5 }' | gawk -F. '{ printf "%s", $3 }'`
 	    fi
	  fi
	fi
	am_host=`echo $am_host | gawk '{ if (match($1, /\(/) == 0) { printf "%s", $1 } else { printf "unresolved" } }'`
      fi
      am_rx=`cat $am_file | grep "rx_bytes" | gawk -F= '{ printf "%u", ($2/1024) }'`
      am_tx=`cat $am_file | grep "tx_bytes" | gawk -F= '{ printf "%u", ($2/1024) }'`
      am_silence=`cat $am_file | grep "last_rx:" | gawk -F\  '{ printf "%s", $2 }' | gawk -F= '{ printf "%s", $2 }'`
      am_signal=`cat $am_file | grep "last_rx:" | gawk -F\  '{ printf "%s", $3 }' | gawk -F= '{ printf "%s", $2 }'`
      am_signal1=`expr $am_signal - $am_silence`
      am_rate_rx=`cat $am_file | grep "last_rx:" | gawk -F\  '{ printf "%s", $4 }' | gawk -F= '{ printf "%.1f", ($2/10) }'`
      am_rate_tx=`cat $am_file | grep "tx_rate" | gawk -F= '{ printf "%.1f", ($2/10) }'`

      am_signal2=`expr $HOSTAP_MAXSIGNAL - $am_signal1`
      am_signal1_img=`echo $HOSTAP_MAXSIGNAL $am_signal1 | gawk -F\  '{ printf "%u", (100 / $1 * $2) }'`
      am_signal2_img=`echo $HOSTAP_MAXSIGNAL $am_signal2 | gawk -F\  '{ printf "%u", (100 / $1 * $2) }'`

      if [ $tmp_color = 0 ]; then
        echo "<tr bgcolor=$COL_CL_BG1>"
      else
        echo "<tr bgcolor=$COL_CL_BG2>"
      fi
      echo "<td><font color=$tmp_color_text><b>$am_host</b></font></td>"
      echo "<td><font color=$tmp_color_text>$am_ip</font></td>"
      echo "<td><font color=$tmp_color_text>$A_MAC</font></td>"

      echo "<td>"
      echo "<table width=170 cellspacing=0 cellpadding=0 border=0><tr>"
      echo "<td width=60><font color=$tmp_color_text>$am_signal1/$HOSTAP_MAXSIGNAL</font></td>"
      echo "<td width=110>"

      echo "<table width=100 cellspacing=0 cellpadding=0 border=0><tr>"
      echo "<td width=$am_signal1_img height=12 bgcolor=$tmp_color_bar1><font size=1>&nbsp;</font></td>"
      echo "<td width=$am_signal2_img bgcolor=$tmp_color_bar2><font size=1>&nbsp;</font></td>"
      echo "</tr></table>"
      
      echo "</td>"
      echo "</tr></table>"
      
      echo "</td>"

      echo "<td align=right><font color=$tmp_color_text>$am_rate_tx Mb/s</font></td>"
      echo "<td align=right><font color=$tmp_color_text>$am_rate_rx Mb/s</font></td>"

      echo "<td align=right><font color=$tmp_color_text>$am_tx Kb</font></td>" # switched - you can see it from CLIENT side
      echo "<td align=right><font color=$tmp_color_text>$am_rx Kb</font></td>"
      echo "</tr>"
      
      tmp_color=`expr $tmp_color + 1 `
      if [ 1 -lt $tmp_color ]; then
        tmp_color=0
      fi
    done

    echo "</table>"
    echo "<br>"

    echo "<b>$LNG_MASTER_CLIENTS_COUNT:</b> $count_clients_active / $count_clients_total<br>"
    
    echo "<br><hr size=1><br>"
  
  # Ad-Hoc mode
  elif [ $ifmode1 = "Ad-Hoc" ]; then

    m_signal1=`cat $PATH_WIRELESS | grep $A_IFACE | gawk -F\  '{ if (match($3, /./) == 0) { printf "%s", $3 } else { printf "%s", substr($3, 1, (length($3)-1)) } }'`

    m_signal2=`expr $HOSTAP_MAXSIGNAL - $m_signal1`
    m_signal1_img=`echo $HOSTAP_MAXSIGNAL $m_signal1 | gawk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_signal2_img=`echo $HOSTAP_MAXSIGNAL $m_signal2 | gawk -F\  '{ printf "%u", (100 / $1 * $2) }'`
    m_snr1=`cat $PATH_WIRELESS | grep $A_IFACE | gawk -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 | gawk -F\  '{ if (match($5, /./) == 0) { printf "%s", $5 } else { printf "%s", substr($5, 1, (length($5)-1)) } }'`
    m_snr=`expr $m_snr1 - $m_snr2`
    
    echo "<font size=+1><b>$A_IFACE</b></font>"
    echo "(Ad-Hoc, $LNG_CHANNEL $m_channel, $LNG_BITRATE $m_bitrate)<br>"

    echo "<table width=170 cellspacing=0 cellpadding=0 border=0><tr>"
    echo "<td width=60><b>$LNG_SIGNAL:</b> $m_signal1/$HOSTAP_MAXSIGNAL</td>"
    echo "<td width=110>"

    echo "<table width=100 cellspacing=0 cellpadding=0 border=0><tr>"
    echo "<td width=$m_signal1_img height=12 bgcolor=$COL_SBAR1><font size=1>&nbsp;</font></td>"
    echo "<td width=$m_signal2_img bgcolor=$COL_SBAR2><font size=1>&nbsp;</font></td>"
    echo "</tr></table>"

    echo "</td>"
    echo "</tr></table>"

    echo "<b>$LNG_SNR:</b> $m_snr<br>"
    echo "<b>$LNG_DATARX:</b> $m_rx Kb"
    echo "<b>$LNG_DATATX:</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 "($LNG_MODE_UNKNOWN)<br>"

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

  fi    

  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>"
