#####################################################################
#
#  gdascount_combine.sh, merges 4 html files into a single file.
#  The files contain non-sat data counts and 3 sat data counts
#  (received, selected and assimilated).  The resulting html file
#  is sent to the rzdm for display in the GDAS Observational Data
#  counts website.        
#
#  History
#  Ralph Jones   10/04/2005  -  Original version.
#  L. Sager      09/30/2007  -  Modified for operations
#  V. Krishna Kumar 11/15/2008 - Modified to adjust the index html file
#                                for new years
#
#####################################################################

set -x
  file0=FILE_NONSAT
  file1=received_counts.html
  file2=selected_counts.html
  file3=assimilated_counts.html

 
# Remove satellite counts from the first file

  line1=`fgrep -n "Satellite Soundings"  $file0 | cut -c1-3` 
  line2=`fgrep -n "BR WP" $file0 | cut -c1-3 | head -1`
  let line2=line2-1


    echo "\n line1 = ${line1}   line2 = ${line2} \n"

  sed ${line1},${line2}d $file0 > temp


# Append 3 satellite data count files

  cat ${file1} >> temp
  cat ${file2} >> temp
  cat ${file3} >> temp

  cp temp temp.sav
  mv temp ${Month}_${YEAR}.html

  typeset -L3 last

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


  HTML="          <a href=\"${Month}_${YEAR}.html\">${Month}</a>"
    echo $HTML


# Backup the index file and make a temporary working copy

  cp $DATCOM1/index.shtml $DATCOM1/index_backup.shtml
  cp $DATCOM1/index.shtml temp


# If this is January, load an empty calendar in the front of the index file 

# Insert below in index.shtml after </table> and then change as counts are added 
# for a given month.  Always save old index before modifying.  This will help to 
# eliminate the shoot in foot syndrome.  Eliminate these comments in new index.
#
  if [ $MON = JAN ]; then

cat << EOF  > newyear
    <center>
    <table width="600" border="1" cellpadding="6" cellspacing="0">
      <tr align="center" bgcolor="#ffcccc">
        <td colspan="6">
          <font size="+1"><b>YEAR</b></font>
        </td>
      </tr>
      <tr>
        <td width="17%">January</td>
        <td width="17%">February</td>
        <td width="16%">March</td>
        <td width="16%">April</td>
        <td width="17%">May</td>
        <td width="17%">June</td>
      </tr>
      <tr>
        <td width="17%">July</td>
        <td width="17%">August</td>
        <td width="16%">September</td>
        <td width="16%">October</td>
        <td width="17%">November</td>
        <td width="17%">December</td>
      </tr>
    </table>

EOF

#    sed 1,3d newyear > new1
#    sed s/YEAR/$YEAR/ new1 > new2

    sed s/YEAR/$YEAR/ newyear > new2

    head -14 temp > new1

    cat new2 >> new1

    sed -n 15,999p temp >> new1

    cp new1 temp

  fi

  
  line=`fgrep -n $Month temp | head -1`
  echo $line | IFS=":" read line_num dum

    echo "\n line = $line  line_num = ${line_num}"

  sed ${line_num}s/"$Month<\/td>"/""/ temp > temp1

  sed -n 1,${line_num}p temp1 > temp2

  echo "$HTML" >> temp2

  echo "        </td>" >> temp2

  let next=line_num+1

  last=`wc -l < temp1`

  sed -n ${next},${last}p temp1 >> temp2


set +x 
  exit

