#!/bin/bash

grep_it() {
  value="`cat $TEMP_FILE | head -$1 | tail -1`"
  color=""
  if [ ${value:=0} -ge 100 ]
  then
    color=" bgcolor=red"
  elif [ ${value:=0} -ge 85 ]
  then
    color=" bgcolor=yellow"
  elif [ ${value:=0} -le 50 ]
  then
    color=" bgcolor=cyan"
  else
    color=" bgcolor=lightgreen"
  fi
  echo "      <td$color>$value"
}

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

export DIR=/world/mrtg/tcdegree
export MACH="`echo \"$1\" | sed -e 's/+/_/g'`"
export MAXB="$2"
export TEMP_FILE=/tmp/tcnowtemp.$$

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

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

cat "$DIR/${MACH}.html" |   \
  grep '<\!-- ' |           \
  grep "out " |              \
  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
