#!/bin/bash

# wminfo plugin: displays RX and TX parameters for different interfaces

# command: wminfo -p 'netmon.wmi 5' -u 5 -b#000000 -f#ff00ff

# Attention: netmon.wmi plugin uses netmon script
#            the number in quotation marks should be equal the value of -u switch

if [ "`which netmon 2> /dev/null`" == "" ]
then
    echo "$0: put netmon script into PATH."
    echo -e "         \n         \n         \n         \n"
    exit
fi

if [ "$1" == "" ]
then
    scalefactor=1
else
    scalefactor=$1
fi

LO=`netmon lo $scalefactor`

for interface in eth0 eth1
do
    LOGFILE="$HOME/.wminfo/.interface.$interface.log"
    /sbin/ifconfig $interface &> $LOGFILE
    if [ "`grep 'Device not found' $LOGFILE`" == "" ]
    then
	ETH=`netmon $interface $scalefactor`
    fi
done

for interface in wlan0 wlan1
do
    LOGFILE="$HOME/.wminfo/.interface.$interface.log"
    /sbin/ifconfig $interface &> $LOGFILE
    sleep 1
    if [ "`grep 'Device not found' $LOGFILE`" == "" ]
    then
	WLN=`netmon $interface $scalefactor`
    fi
done

LO_RX="`echo $LO | awk '{print $4}'`"
# LO_TX="`echo $LO | awk '{print $7}'`"
ETH_RX="`echo $ETH | awk '{print $4}'`"
ETH_TX="`echo $ETH | awk '{print $7}'`"
WLN_RX="`echo $WLN | awk '{print $4}'`"
WLN_TX="`echo $WLN | awk '{print $7}'`"

LR="      "
# LT="      "
ER="    "
ET="    "
WR="    "
WT="    "

LR=${LR: -$((7-${#LO_RX})):5}
# LT=${LT: -$((7-${#LO_TX})):5}
ER=${ER: -$((5-${#ETH_RX})):3}
ET=${ET: -$((5-${#ETH_TX})):3}
WR=${WR: -$((5-${#WLN_RX})):3}
WT=${WT: -$((5-${#WLN_TX})):3}

echo "LO$LR$LO_RX"
# echo "LO$LT$LO_TX"
echo "ETH:$ER$ETH_RX"
echo "ETH:$ET$ETH_TX"
echo "WLN:$WR$WLN_RX"
echo "WLN:$WT$WLN_TX"

echo -e "         \n         \n         \n         \n         \n"

