#!/usr/bin/perl
##
#
# Program: bufrdumplist.pl $ARGV[0]
#  where $ARGV[0] is the parent script's working directory, $DATA.
# Author: Jeffrey R. Stoudt
# Modifications: 10 Sep 2009 - Stoudt - New program
#
# Description: This Perl script dynamically generates the "Explanation of Data Types"
#              web page from the current bufr_dumplist file so that this web page need
#              not be manually updated and RFC'd whenever bufr_dumplist is modified.
#
##

## Declarations and initializations section
#
# $NFSUSER  = "/nco/pmb/wx12js";  # (for testing)
# $DataMon  = "${NFSUSER}/data.monitor";  # (for testing)
# $Unsorted = "${DataMon}/unsorted";  # Construct raw (unsorted) list here.
# $Sorted  = "${DataMon}/sorted";  # Save sorted list here.
# $ListHtml = "${DataMon}/bufr_dumplist.html";  # Construct final product here.  (for testing)
$DATAdir = $ARGV[0];
$Unsorted = "${DATAdir}/unsorted";  # Construct raw (unsorted) list here.
$Sorted  = "${DATAdir}/sorted";  # Save sorted list here.
$ListHtml = "${DATAdir}/bufr_dumplist.html";  # Construct final product here.  (operational)
# $BufrDumplist = "/nwprod/fix/bufr_dumplist";  # Bufr Dump List file for input.
$BufrDumplist = "${DATAdir}/bufr_dumplist_temp";  # Bufr Dump List file for input.
$Status = 0;  # Searching status: 0 = Mnemonic list beginning; 1 = list parsing.

## Recurring strings
#
$Href = "<center><h4><a href=\"./\">Return to Realtime Monitor Mainpage</a></h4></center>";
$TrGreen = "<tr valign=\"top\"> <td> <font color=\"green\">";
$TdTd = "</td> <td>";
$TdTr = "</td> </tr>";

## Retrieve information and place it unsorted in a temp file.
# Open bufr_dumplist for input.
open(BUFR, "$BufrDumplist") || die "cannot open bufr_dumplist";
# Open unsorted for output.
open(UNSORT, "> $Unsorted") || die "cannot open unsorted";
 while (<BUFR>) {
  # Look for place in file where the mnemonic list begins.
  if ( $Status == 0 ) {
   if ( /mnemonic/ && /tttsss/ && /Descriptive\ Information/ ) { $Status = 1; }
  }  # end if
  # Parse the mnemonic list part of the file and push info to unsorted output file.
  if ( $Status == 1 ) {
   if ( /^\_/ ) {  # Data type mnemonic lines always begin with an underscore character.
    @divisions = split(/\|/, $_);  # All the info needed comes before the first pipe (|).
    @subdivs = split(/\>/, $divisions[0]);  # [1] contains descriptions; [0] contaiins all else.
    $_ = $subdivs[0]; tr/a-z/A-Z/; $MNEMONICS = $_;   # We want to sort mnemonics in ALL CAPS.
    $record = join(">", $MNEMONICS, $subdivs[1]);  # Rejoin UPPER CASE mnemonics with descriptive info.
    &printflush (UNSORT, "${record}\n");  # Write it to the unsorted file.
   }  # end if
  }  # end if
 }  # end while
close(UNSORT);
close(BUFR);

## Sort the temporary file by data mnemonic, which is in the first field, and direct to a sorted file.
## The Bufr file had them sorted by bufr type/sub type (tttsss); we want it sorted by mnemonic.
$action = "cat ${Unsorted} | sort > ${Sorted}";
system ("${action}");

## Extract the sorted information parts and write to the HTML file, dressed with the HTML code.
# Open sorted for input.
open(SORTED, "$Sorted") || die "cannot open sorted";
# Open final HTML file for output.
open(HTML, "> $ListHtml") || die "cannot open final HTML file";
 # Write the preliminary fixed HTML code.
 &printflush (HTML, "<html>\n");
 &printflush (HTML, "<head>\n");
 &printflush (HTML, "<title>NWS/NCO/SIB RTDMS </title>\n");
 &printflush (HTML, "</head>\n");
 &printflush (HTML, "<body>\n");
 &printflush (HTML, "${Href}\n");
 &printflush (HTML, "<table border=\"1\" align=\"center\" cellpadding=\"2\" cellspacing=\"0\">\n");
 &printflush (HTML, "<tr valign=\"bottom\"> <td> <font color=\"green\"> <b>MNEMONIC<br>NAME</b> </td>");
 &printflush (HTML, "<td> <b>Bufr_type<br>Sub_type</b> </td> <td> <b>Descriptive Information</b> </td> </tr>\n");
 # Now construct the HTML table data cells from the data in the sorted file.
 while (<SORTED>) {
  @subdivs = split(/\>/, $_);
  $Description = substr $subdivs[1], 1;  # Descriptions come after the gt (>); exclude leading space.
  chomp $Description;  # And chop off the trailing \n character.
  $Mnemonics = $subdivs[0];  # The other info precedes the gt character.
  @Fields = split(/\s+/, $Mnemonics);  # white space
  $Mnemonic = substr $Fields[0], 1;  # Exclude the leading underscore (_) char.
# $_ = $Mnemonic; tr/a-z/A-Z/; $Mnemonic = $_;   # We want to tabulate mnemonics in ALL CAPS. Already done.
  $tttsss = $Fields[2];  # The tttsss number is the third ([2]) field.
  $Record = join(" ", $TrGreen, $Mnemonic, $TdTd, $tttsss, $TdTd, $Description, $TdTr);  # Build the record.
  &printflush (HTML, "${Record}\n");  # And write it to the HTML file.
 }  # end while
 close(SORTED);
 # Finally, write the closiing fixed HTML code.
 &printflush (HTML, "</table>\n");
 &printflush (HTML, "${Href}\n");
 &printflush (HTML, "</body>\n");
 &printflush (HTML, "</html>\n");
close(HTML);


sub printflush {
    local($old) = select(shift);
    $| = 1;
    print @_;
    $| = 0;
    select($old);
}

exit;

### End of program bufrdumplist.pl
