#!/usr/bin/env perl 

use strict;
use Data::Dumper;
use XML::LibXML;
my $image_dir  = "./images";

print <<"END_of_Start";

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>CESM Component Models Namelist Definitions </title>
  <link rel="stylesheet" type="text/css" href="/models/cesm1.0/cam/docs/namelist/nl_style_sheet.css" />
  <script src=./showinfo.js> </script>
</head>

<body>

<h2>Search or Browse supported grid settings</h2>
<p>
This page contains the complete list of config_grid.xml variables available.  They are grouped
by categories designed to aid browsing.  Clicking on the name of a variable will display descriptive
information.  If search terms are entered in the text box below, the list will be condensed to contain
only matched variables.
</p>

<form id="filter_form" name="filter_form" style="margin: 0px; padding: 0px;" action="javascript:void(0);">
  <table border="0" cellpadding="2" cellspacing="1">
    <tbody>
      <tr>
        <td valign="top">
          <input id="filter_text" name="filter_text" size="40"
                 onkeydown="if (event.keyCode==13) applyFilter(document.getElementById('filter_text').value);"
                 type="text">
          <input id="btn_search" value="Search Variable Names"
                 onclick="applyFilter(document.getElementById('filter_text').value);"
		 type="button">
          <input id="btn_show_all" value="Show All Variable Names"
		 onclick="clearFilter();return false;"
                 type="button">
          <br>
          <label>
            <input id="logical_operator_and" name="logical_operator" value="AND" 
                   type="radio" checked> AND
          </label>
          <label>
            <input id="logical_operator_or" name="logical_operator" value="OR"
                   type="radio"> OR
          </label>
          (separate search terms with spaces)
          <br>
          <label>
            <input id="search_help_text" name="search_help_text" type="checkbox"> Also search help text
          </label>
        </td>
      </tr>
    </tbody>
  </table>
</form>

<div id="filter_matches" style="visibility: hidden; margin-bottom: 10px;">
  Found <span id="filter_matches_num"></span> standard names matching query: <span id="filter_matches_query"></span>
</div>

<p>

<center>
<input id="btn_expand_help" value="Show All Help Text" 
       onclick="expandAllHelp();return false;"
       type="button">
<input id="btn_collapse_help" value="Collapse All Help Text" 
       onclick="collapseAllHelp();return false;"
       type="button">
</center>

END_of_Start

my $parser = XML::LibXML->new;
my $list = $parser->parse_file('../../ccsm_utils/Case.template/config_grid.xml');
my $root = $list->getDocumentElement();

# Print table
print_start_table("config_grid.xml variables");

foreach my $grid ($root->findnodes('/config_compset/GRID')) { 
    my $alias = $grid->getAttribute('alias');
    if (! defined $alias) {$alias = 'NOT_DEFINED';}
    my $sname = $grid->getAttribute('sname');
    my $lname = $grid->textContent; 
    my $doc = "";
    $lname =~ /(a%)(.+)(_l%)/ ; my $atm_grid  = $2;
    $lname =~ /(l%)(.+)(_oi%)/; my $lnd_grid  = $2; my $glc_grid = $2;
    $lname =~ /(oi%)(.+)(_r%)/; my $ocn_grid  = $2; my $ice_grid = $2;
    $lname =~ /(r%)(.+)(_m%)/ ; my $rof_grid  = $2; 
    $lname =~ /(m%)(.+)(_g%)/ ; my $mask_grid = $2; 
    $lname =~ /(g%)(.+)(_w%)/ ; my $cism_grid = $2; 
    $lname =~ /(w%)(.+)$/     ; my $wav_grid  = $2; 
    foreach my $griddesc ($root->findnodes('/config_compset/gridhorz')) { 
	my $hgrid      = $griddesc->getAttribute('name');
	my $hgrid_alias= $griddesc->getAttribute('alias');
	my $gridname;
	if ($hgrid_alias) {
	    $gridname = $hgrid_alias;
	} else {
	    $gridname = $hgrid;
	}
	my $nx   = $griddesc->findnodes('./nx');
	my $ny   = $griddesc->findnodes('./ny');
	my $desc = $griddesc->findnodes('./desc');
	if ($atm_grid eq $hgrid) {
	    $doc = $doc . "\nATM_GRID $gridname \n";
	    $doc = $doc . "  ATM_NX   $nx \n";
	    $doc = $doc . "  ATM_NY   $ny \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($lnd_grid eq $hgrid) {
	    $doc = $doc . "\nLND_GRID $gridname \n";
	    $doc = $doc . "  LND_NX   $nx \n";
	    $doc = $doc . "  LND_NY   $ny \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($ice_grid eq $hgrid) {
	    $doc = $doc . "\nICE_GRID $gridname \n";
	    $doc = $doc . "  ICE_NX   $nx \n";
	    $doc = $doc . "  ICE_NY   $ny \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($ocn_grid eq $hgrid) {
	    $doc = $doc . "\nOCN_GRID $gridname \n";
	    $doc = $doc . "  OCN_NX   $nx \n";
	    $doc = $doc . "  OCN_NY   $ny \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($rof_grid eq $hgrid) {
	    $doc = $doc . "\nROF_GRID $gridname \n";
	    $doc = $doc . "  ROF_NX   $nx \n";
	    $doc = $doc . "  ROF_NY   $ny \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($glc_grid eq $hgrid) {
	    $doc = $doc . "\nGLC_GRID $gridname \n";
	    $doc = $doc . "  GLC_NX   $nx \n";
	    $doc = $doc . "  GLC_NY   $ny \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($wav_grid eq $hgrid) {
	    $doc = $doc . "\nWAV_GRID $gridname \n";
	    $doc = $doc . "  WAV_NX   $nx \n";
	    $doc = $doc . "  WAV_NY   $ny \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($mask_grid eq $hgrid) {
	    $doc = $doc . "\nMASK $gridname \n";
	    $doc = $doc . "  $desc \n";
	}
	if ($cism_grid eq $hgrid) {
	    $doc = $doc . "\nCISM_GRID $gridname \n";
	    $doc = $doc . "  $desc \n";
	}
    }
    print_row($lname, $doc, $sname, $alias);
}
print_end_table();

# Finish
print <<"END_of_html";
</body>
</html>
END_of_html

#--------------------------------------------------------------------------------------------

sub print_start_table {
    my $hdr = shift;

print <<"START_table";
<h3><span style="background-color: #00FFFF" font color="purple">$hdr</h3></span>
<table id="${hdr}_table" border="1" width="100%" cellpadding="2" cellspacing="0">
  <th width="70%">Compset Longname</th>
  <th width="10%">Short Name</th>
  <th width="10%">Alias</th>
START_table
}

#--------------------------------------------------------------------------------------------

sub print_row {

    my $name = shift;
    my $doc  = shift;
    my $grp  = shift;
    my $alias = shift;
    
print <<"END_of_row";
  <tr id="${name}_tr">
    <td><a name="$name"></a>
        <img id="${name}_arrow" src="$image_dir/arrow_right.gif">
        <code class="varname">
          <a href="javascript:void(0)"
             onclick="toggleHelp('${name}')">$name</a>
        </code>
        <div id="${name}_help" style="display: none;
             padding-left: 16px; margin-top: 4px; border-top: 1px dashed
             #cccccc;">
	     <pre>$doc</pre>
        </div>
    </td>
    <td>$grp</td>
    <td>$alias</td>
  </tr>
END_of_row
}

#--------------------------------------------------------------------------------------------

sub print_end_table {

print <<"END_table";
</table>
END_table
}


