#!/bin/bash

grep_it() {
  value="`cat $TEMP_FILE | head -$1 | tail -1`"
  perc="`expr \"$value\" \* 100 / \"${MAXB:=1}\"`"
  color=""
  if [ ${perc:=0} -ge 90 ]
  then
    color=" bgcolor=red"
  elif [ ${perc:=0} -ge 80 ]
  then
    color=" bgcolor=yellow"
  elif [ ${perc:=0} -le 25 ]
  then
    color=" bgcolor=cyan"
  else
    color=" bgcolor=lightgreen"
  fi
  echo "       $value<td$color>${perc}%"
}

###############################################################################

export DIR=/usr/local/mrtg-2.8.12/local-config/www/tcusercount
export MACH="`echo \"$1\" | sed -e 's/+/_/g'`"
export MAXB="$2"
export TEMP_FILE=/tmp/tcnowstat.$$

###############################################################################

if [ ! -r "$DIR/${MACH}.html" ]
then
  exit
fi

cat "$DIR/${MACH}.html" |   \
  grep '<\!-- ' |           \
  grep "in " |              \
  cut -d' ' -f4 >$TEMP_FILE
trap "rm -f $TEMP_FILE ; exit" 1 2 15

echo ""
echo "  
"
echo "    "
echo "       "
echo "       Max"
echo "       Avg"
echo "       Now"
echo "    
"
echo "    "
echo "       Day"
grep_it 1
grep_it 2
grep_it 3
echo "    
"
echo "    "
echo "       Week"
grep_it 4
grep_it 5
grep_it 6
echo "    
"
echo "    "
echo "       Month"
grep_it 7
grep_it 8
grep_it 9
echo "    
"
echo "    "
echo "       Year"
grep_it 10
grep_it 11
grep_it 12
echo "    
"
echo "  
"
echo ""

trap - 1 2 15
rm -f $TEMP_FILE 2>/dev/null
