#!/bin/ksh 
#
# main-page: Reformat and build a main page that allows for frames
#
# version 1.0 - originally written for NetworkCommerce
# version 1.1 - completely hacked and modified to work at Pershing
# version 1.2 - changed to a table format
# version 1.3 - added home link
# version 1.4 - modified BBWEB items to get new BBWEB w/o restart of BB
#
# BIG BROTHER / XXXXXXXXXXXXXXXX status
#
# Written by Paul A. Luzzi
#  on September 18, 2000
#

########################################
# NOTE
# This has been tested with BB 1.2b, 1.4h2, 1.5, 1.5a
# 
# Tested on :
#   Sun E3000, E3500, E3501, E4500, E4501
#   HP  B132L
########################################

########################################
# INSTALLATION
#  step 1  - update the EXT section of the runbb.sh script to include this
#  ...
########################################

##################################
# CONFIGURE IT HERE
##################################
## BBTOPULLFROM=bb-netcom.html
BBTOPULLFROM=bb.html

##################################
# Start of script
##################################

if test ! "$BBHOME"
then
	echo "template: BBHOME is not set"
	exit 1
fi

if test ! -d "$BBHOME"
then
	echo "template: BBHOME is invalid"
	exit 1
fi

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
	echo "template: The BB environment is not set"
	exit 1
fi

#####
#####  Start of custom sections added by Paul A. Luzzi
#####
export BBWEB=`$GREP ^BBWEB= $BBHOME/etc/bbdef.sh | $AWK -F= '{print $2}' | $SED -e s/\"//g `

#####
#####  Routine to grab the background color off of the bb.html main page.
#####
PAGE_BGCOLOR ()
{
  ## BGCOLOR_LINE=`$GREP "BODY BGCOLOR" ${BBHOME}/www/$BBTOPULLFROM | $AWK -F"TEXT" '{print $1">"}' `
  BGCOLOR_LINE=`$GREP "BODY BGCOLOR" ${BBHOME}/www/$BBTOPULLFROM  `
}

#####
#####  Build the initial page buttons, header, color, etc ....
#####
PAGE_HEADER ()
{
  #####
  #####  Begin by setting up the frame header ....
  #####
  ## echo "Content-type: text/html"
  echo "<HTML>"
  echo "<HEAD>"
  echo "  <TITLE>Table of Contents</TITLE>"
  echo "  <!-- IF YOU DON'T WANT UNDERLINED LINKS DELETE THIS COMMENT LINE --> "
  echo "  <STYLE TYPE=\"text/css\"><!-- A:link {text-decoration: none}A:visited{text-decoration:none}A:active{text-decoration:none}--></STYLE>"
  echo "  <META HTTP-EQUIV=\"REFRESH\" CONTENT=\"60\">"
  echo "</HEAD>"
  echo "$BGCOLOR_LINE"
  echo "<a href=\"${BBWEB}/help/bb-ack.html\" target="main"><img src=\"${BBWEB}/gifs/bbnav1.gif\" border=0></a>"
  echo "<a href=\"${BBWEB}/help/bb-rep.html\" target="main"><img src=\"${BBWEB}/gifs/bbnav4.gif\" border=0></a>"
  echo "<a href=\"${BBWEB}/help/bb-help.html\" target="main"><img src=\"${BBWEB}/gifs/bbnav3.gif\" border=0></a>"
  echo "<br>"
  echo "<P>&nbsp;<P>"
  echo "<FONT COLOR=\"#FFFFCC\" FACE=\"Tahoma, Arial, Helvetica\">"
  echo "<TABLE><TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD><br>"
}

#####
#####  Close the page for use in the browser
#####
PAGE_CLOSE ()
{
  echo "</TD></TR></TABLE>"
  echo "</font>"
  echo "<br>"
  echo "</body>"
  echo "</html>"
}

#####
#####  Very small reset routine for the color value
#####
RESET_COLOR ()
{
  WORST_VALUE=0
}

#####
#####  Routine to convert the color number into a name, for use in getting the gifs
#####
COLOR_CHECK ()
{
  case "$WORST_VALUE" in 
    5 )
      WORST_COLOR="red"
      ;;
    4 )
      WORST_COLOR="purple"
      ;;
    3 )
      WORST_COLOR="yellow"
      ;;
    2 )
      WORST_COLOR="green"
      ;;
    1 )
      WORST_COLOR="clear"
      ;;
    0 )
      WORST_COLOR="clear"
      ;;
  esac

  #####
  #####  Also use this oportunity to build the line for the frame
  #####
  echo "<img src=\"${BBWEB}/gifs/${WORST_COLOR}.gif\" BORDER=0>"
  echo "&nbsp;"
  echo "<a target=main href=\"${BBWEB}/bb.html#${GROUP_LINK}\"></a>"
  echo "<br>"
}

#####
#####  Used to get the group name from the bb.html page.
#####
TITLE_LINE () 
{
  ## GROUP=` echo $line | $AWK -F"<" '{printf $1}' `
  GROUP_PP=` echo $line | $AWK -F">" '{printf $2}' | $AWK -F"<" '{print $1}' `
  GROUP_LINK=` echo $line | $AWK -F">" '{printf $2}' | $AWK -F"<" '{print $1}' `
}

#####
#####  Routine to check if the color has progressed to worse.  Check agains a 
#####    predefined value called worst-value.  Pull out the color from the bb.html
#####    with the first GIF_COLOR line, adn compare that to the known value.
#####
GIF_LINE ()
{
  GIF_COLOR=`echo $line | $AWK -F"ALT=" '{print $2}' | $AWK -F">" '{print $1}' | $SED s/\"//g`
  ## echo "  $GIF_COLOR"
  case $GIF_COLOR in 
    red )
      WEIGHT_VALUE=5
      if [ "$WEIGHT_VALUE" -gt "$WORST_VALUE" ]
        then
          WORST_VALUE="$WEIGHT_VALUE"
        fi
      ;;
    purple )
      WEIGHT_VALUE=4
      if [ "$WEIGHT_VALUE" -gt "$WORST_VALUE" ]
        then
          WORST_VALUE="$WEIGHT_VALUE"
        fi
      ;;
    yellow )
      WEIGHT_VALUE=3
      if [ "$WEIGHT_VALUE" -gt "$WORST_VALUE" ]
        then
          WORST_VALUE="$WEIGHT_VALUE"
        fi
      ;;
    green )
      WEIGHT_VALUE=2
      if [ "$WEIGHT_VALUE" -gt "$WORST_VALUE" ]
        then
          WORST_VALUE="$WEIGHT_VALUE"
        fi
      ;;
    clear )
      WEIGHT_VALUE=1
      if [ "$WEIGHT_VALUE" -gt "$WORST_VALUE" ]
        then
          WORST_VALUE="$WEIGHT_VALUE"
        fi
      ;;
    * )
      WEIGHT_VALUE=0
      if [ "$WEIGHT_VALUE" -gt "$WORST_VALUE" ]
        then
          WORST_VALUE="$WEIGHT_VALUE"
        fi
      ;;
  esac
}

build_frame() 
{
  #####
  #####  Section written by Paul A. Luzzi
  #####    for use with Big Brother
  #####
  #####  Purpose is to build a left frame for a summary of the main frame.
  #####

  #####
  #####  Sleep for 2 minutes and let the bb.html page be built in the background
  #####    before we try to operate on it.
  #####
  sleep 120

  #####
  #####  Set the initial color to null, and get the background color from bb.html
  #####    then lets call the header section to build the base header for the frame.
  #####
  RESET_COLOR
  PAGE_BGCOLOR
  PAGE_HEADER
  
  #####
  #####  Now take the bb.html page and search between the lines of :
  #####    *change_name_in_eol* thru the *&nbsp;* lines and piece them apart
  #####
  #####  The idea here is to pull out any lines that start with text which would be
  #####    the main group headings, and any lines with gifs in them, because they 
  #####    would be the color/status segments.  Then get rid of any lines that are
  #####    blank, start with whitespace, or have bblogo in them.  Finally find out
  #####    if it is a gif line or a group heading line, and perform appropriate action.
  #####
  $CAT ${BBHOME}/www/$BBTOPULLFROM | \
    $AWK ' $0 ~ /<IMG SRC.**$/ { if ($0 !~ /<IMG SRC.**>.*$/ ) {printf $0} else {print $0 } } $0 ~ /HEIGHT.*$/ {print $0"\n"} $0 !~ /<IMG.*$|HEIGHT.*$/ {print $0}' | \
    $AWK '/.change_name_in_eol./, /.&nbsp;./ {print $0 "\n"}' | \
    $EGREP "^-->|gif|html" | \
    $EGREP -v "^$|^[ |     ]|bblogo" | \
    while read line
      do
        IS_GIF=`echo $line | $GREP gif `
        IS_HTML=`echo $line | $GREP html | $GREP -v eol.html `
        IS_HDR=`echo $line | $GREP "\-\-\>" `
        if [ ! -z "$IS_GIF" ]
          then
            GIF_COLOR=`echo $line | $AWK -F"/" '{print $2}' | $AWK -F\" '{print $1}' `
            GIF_COUNTER=$(( $GIF_COUNTER + 1 ))
          fi
        if [ ! -z "$IS_HDR" ]
          then
            GROUP_PP=` echo $line | $AWK -F">" '{printf $2}' | $AWK -F"<" '{print $1}' `
          fi
        if [ ! -z "$IS_HTML" ]
          then
            GROUP_LINK=` echo $line | $AWK -F\" '{printf $2}' | $AWK -F\" '{print $1}' `
          fi
        if [ "$GIF_COUNTER" -gt "1" ]
          then
            echo "<img src=\"${BBWEB}/gifs/${GIF_COLOR}\">"
            echo "&nbsp;"
            echo "<a target=main href=\"${BBWEB}/${GROUP_LINK}\">${GROUP_PP}</a>"
            ## echo "<a target=main href=\"${BBWEB}/${GROUP_LINK}\"><img src=\"${BBWEB}/gifs/${GIF_COLOR}\">&nbsp;${GROUP_PP}</a>"
            echo "<br>"
            GIF_COUNTER=0 
          fi
      done
  
  #####
  #####  Perform the final color check, setup home link, and close the html page.
  #####
  ## COLOR_CHECK
  echo "<P>&nbsp;<P>"
  echo "<img src=\"${BBWEB}/gifs/clear.gif\">"
  echo "&nbsp;"
  echo "<a target=main href=\"${BBWEB}/bb.html\">Home</a>"
  echo "<br>"
  PAGE_CLOSE
  
#####
#####  End of build_frame proc
#####
}

#####
#####  Main body
#####
touch /tmp/$MACHINE.main-page
$GREP `uname -n` $BBHOSTS | $GREP "main-page" | 
while read line
  do
    if [ ! -z "$line" ]
      then
        ## echo "<BR><DIV ALIGN=\"CENTER\"><HR></DIV>" > /tmp/$MACHINE.main-page
        ## echo "Status of interprocess communications on $MACHINE " >> /tmp/$MACHINE.main-page
        ## echo "Captured : `$DATE` " >> /tmp/$MACHINE.main-page
        build_frame > /tmp/$MACHINE.main-page
        ## build_frame | tee /tmp/$MACHINE.main-page
        ## $BB $BBDISP "status $MACHINE.main-page $COLOR `$DATE` `$CAT /tmp/$MACHINE.main-page` "
        cp /tmp/$MACHINE.main-page ${BBHOME}/www/bbtoc.html
      ## else
        ## COLOR="clear"
        ## $BB $BBDISP "status $MACHINE.main-page $COLOR `$DATE` `$CAT /tmp/$MACHINE.main-page` "
      fi
    done

#####
#####  End of custom section added by Paul A. Luzzi
#####
  
##############################################
# end of script
##############################################
