#!/bin/bash

##########################################################################################################
#
# Title:    exhrly_realtime_color.sh
#
# Author:   Kelly Malone
#
# Date:     04-29-2009
#
# Task:     Evaluate the color coded data table for the "worst" color based on a defined
#           hierarchy of severity.  Float the worst color for each hour to the table
#           on the main page for easy viewing of potential data related problems.
#
# Usage:    bash real_mainapge_color.sh $input_dir $output_dir
#           output_dir=/com/web/prod/realtime/realtime
#           input_dir=/com/web/prod/realtime/realtime
#
# Colors:   In order of severity from most severe to least severe:
#           Red:        Shortage in any one of the critical data types 
#           Orange:     Shortage in any one of the sub-critical data types
#           Magenta:    Shortage in any one of the data types of opportunity
#           Purple:     Overage in any one of the critical data types 
#           Blue:       Overage in any one of the sub-critical data types
#           Turquoise:  Overage in any one of the data types of opportunity
#           Green:      Normal counts for all critical and sub-critical data types
#           Black:      Normal counts for data types of opportunity
#
# CHANGE LOG:
#       
#    K. Malone	    MAY 2010	Modified to include magenta and darkturquoise as colors to 
#       (Kempisty)              represent overages and shortages of data of opportunity.  Modified to 
#                               change the order of severity so that all shortages take precedence over
#                               any overages.          
#    K. Kempisty    NOV 2011    Modified to replace legacy RUC with Rapid Refresh (RAP).
#
##########################################################################################################

set -ax

### Define variables and directory in which html files for the RTDM site are stored.

COMWEBIN=$1
COMWEBOUT=$2

### Find all lines in the ${model}/t${hour}z/table.html file with an "a HREF" tag 
### in it.  These lines will contain the colors that will be defined as the 
### new_color for the respective hour.

### Define an array of colors.

color=( "red" "orange" "magenta" "purple" "blue" "darkturquoise" "green" "black" )
num_color=${#color[*]}

### Open a loop that will translate the "old_color" in the realtime main page to the
### correct "new_color" based on the "worst" color in the table.html page.  The loop
### must go through each hour.  If the hour is 00, 06, 12, or 18Z, it will run through
### all of the models.  If it is any other hour, it will only run through the hourly
### and RAP models.

for hour in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
do

   if [[ $hour == "00" || $hour == "06" || $hour == "12" || $hour == "18" ]];
   then

      for model in nam gfs gds hourly rap
      do

### Find all lines contaning "HREF" in the table.html code, and print them to color.out.

         grep HREF ${COMWEBIN}/${model}/t${hour}z/table.html > color.out 

### Loop through the array of colors searching the color.out file (which is the file
### that was created from the grep of HREF in the table.html file).  The first color
### that is found in the array will signal the loop to break.  This color will be 
### defined as $new_color.

         for ((i=0 ; i < $num_color ; i++))
         do 
            grep -q ${color[i]} color.out 
            if (( ! $? ));  
            then
               break 
            fi
         done
         new_color=${color[i]}

### In the template .html file for the first page of the RTDM site: ${COMWEBOUT}/realtime_mainpage.html,
### grep for the ${model}/t${hour}z and print the output to a new file modelhour.out.

         grep ${model}/t${hour}z ${COMWEBOUT}/realtime_mainpage.html > modelhour.out

### Loop through the color array searching the modelhour.out file.  The first color
### that is found in the array will signal the loop to break.  This color will be defined
### as $old_color

         for ((i=0 ; i < $num_color ; i++))
         do
            grep -q ${color[i]} modelhour.out
            if (( ! $? ));
            then
               break
            fi
         done
         old_color=${color[i]}

         echo ${new_color}
         echo ${old_color}

### Set up a case statement to create title tags, which will be inserted into the code to improve Section  
### 508 compliance on the RTDM.

         case $new_color in
            red)           title_tag=" - Shortage in any one of the critical data types";;
            orange)        title_tag=" - Shortage in any one of the sub-critical data types";;
            magenta)       title_tag=" - Shortage in any one of the data types of opportunity";;
            purple)        title_tag=" - Overage in any one of the critical data types";;
            blue)          title_tag=" - Overage in any one of the sub-critical data types";;
            darkturquoise) title_tag=" - Overage in any one of the data types of opportunity";;
            green)         title_tag=" - Normal counts for all critical and sub-critical data types";;
            *)             ;;
         esac

### In the ${COMWEBOUT}/realtime_mainpage.html file, replace $old_color with $new_color on the line  
### indicating the appropriate model and hour.  Also, insert the appropriate title_tag based on the new_color.

         sed -e "s/\"${model}\/t${hour}z\/index.shtml\" ><font COLOR=\"${old_color}\">${hour}z/\"${model}\/t${hour}z\/index.shtml\" title=\"${hour}z${title_tag}\" ><font COLOR=\"${new_color}\">${hour}z/" ${COMWEBOUT}/realtime_mainpage.html > temp_color

### Copy the temporary file into the publishable mainpage for the RTDM.
   
         cp temp_color ${COMWEBOUT}/realtime_mainpage.html

      done

   else

### Repeat the same steps for the hourly and RAP models for all hours not including 00, 06, 12, or 18Z.

      for model in hourly rap
      do

         grep HREF ${COMWEBIN}/${model}/t${hour}z/table.html > color.out

         for ((i=0 ; i < $num_color ; i++))
         do
            grep -q ${color[i]} color.out
            if (( ! $? ));
            then
               break
            fi
         done
         new_color=${color[i]}

         grep ${model}/t${hour}z ${COMWEBOUT}/realtime_mainpage.html > modelhour.out

         for ((i=0 ; i < $num_color ; i++))
         do
            grep -q ${color[i]} modelhour.out
            if (( ! $? ));
            then
               break
            fi
         done
         old_color=${color[i]}

         echo ${new_color}
         echo ${old_color}

         case $new_color in
            red)           title_tag=" - Shortage in any one of the critical data types";;
            orange)        title_tag=" - Shortage in any one of the sub-critical data types";;
            magenta)       title_tag=" - Shortage in any one of the data types of opportunity";;
            purple)        title_tag=" - Overage in any one of the critical data types";;
            blue)          title_tag=" - Overage in any one of the sub-critical data types";;
            darkturquoise) title_tag=" - Overage in any one of the data types of opportunity";;
            green)         title_tag=" - Normal counts for all critical and sub-critical data types";;
            *)             ;;
         esac

         sed -e "s/\"${model}\/t${hour}z\/index.shtml\" ><font COLOR=\"${old_color}\">${hour}z/\"${model}\/t${hour}z\/index.shtml\" title=\"${hour}z${title_tag}\" ><font COLOR=\"${new_color}\">${hour}z/" ${COMWEBOUT}/realtime_mainpage.html > temp_color

         cp temp_color ${COMWEBOUT}/realtime_mainpage.html

      done

   fi

done

#####################################################################
# GOOD RUN
# set +x
echo "************** exhrly_realtime_color.sh COMPLETED NORMALLY ON THE IBM"
echo "************** exhrly_realtime_color.sh COMPLETED NORMALLY ON THE IBM"
echo "************** exhrly_realtime_color.sh COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################

msg="HAS COMPLETED NORMALLY!"
echo $msg
#postmsg "$jlogfile" "$msg"
############## END OF SCRIPT #######################

