#!/usr/bin/env perl 

use strict;

my @dirs = ('../../ccsm_utils/Tools/per5lib', '../../ccsm_utils/Tools/perl5lib/Build');
unshift @INC, @dirs;
require XML::Lite;
use lib "../../ccsm_utils/Tools/perl5lib";

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 Machines</h2>
<p>
This page contains the complete list of machines 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. Note that the entries for each machine are placed into different xml files
in the CASEROOT directory. A summary follows:

<table border="1" style="border-collapse:collapse; width=50%">
<thead>
<tr>
<th>xml variable</th>
<th>CASEROOT file</th>
</tr>
</thead>
<tbody>
<tr><td>  COMPILERS            </td><td> env_build.xml   </td></tr>
<tr><td>  COMPILER             </td><td> env_build.xml   </td></tr>
<tr><td>  MPILIBS              </td><td> env_build.xml   </td></tr>
<tr><td>  OS                   </td><td> env_build.xml   </td></tr>
<tr><td>  GMAKE_J              </td><td> env_build.xml   </td></tr>
<tr><td>  EXEROOT              </td><td> env_build.xml   </td></tr>
<tr><td>  RUNDIR               </td><td> env_run.xml     </td></tr>
<tr><td>  DIN_LOC_ROOT         </td><td> env_run.xml     </td></tr>
<tr><td>  DIN_LOC_ROOT_CLMFORC </td><td> env_run.xml     </td></tr>
<tr><td>  DOUT_S_ROOT          </td><td> env_run.xml     </td></tr>
<tr><td>  DOUT_L_MSROOT        </td><td> env_run.xml     </td></tr>
<tr><td>  CCSM_BASELINE        </td><td> env_run.xml     </td></tr>
<tr><td>  CCSM_CPRNC           </td><td> env_run.xml     </td></tr>
<tr><td>  BATCHQUERY           </td><td> env_run.xml     </td></tr>
<tr><td>  BATCHSUBMIT          </td><td> env_run.xml     </td></tr>
<tr><td>  MAX_TASKS_PER_NODE   </td><td> env_pesetup.xml </td></tr>
<tr><td>  PES_PER_NODE         </td><td> env_pesetup.xml </td></tr>
<tr><td>  SUPPORTED_BY         </td><td> documentation only</td></tr>
</tbody>
</table>
</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 ($file) = "../../ccsm_utils/Machines/config_machines.xml";
my $xml = XML::Lite->new( $file );
my $root = $xml->root_element();

# Check for valid root node
my $name = $root->get_name();
$name eq "config_machines" or die
	"file $file is not a machines description file\n";

# Print table
print_start_table("config_machines.xml variables");
my @e = $xml->elements_by_name( "machine" );
my %a = ();
my $compilers;
my $mpilibs;
my $os;
while ( my $e = shift @e ) {
    %a = $e->get_attributes();
    my $var = $a{'MACH'};
    my @children = $e->get_children();
    my $doc = " All xml variables are in env_run.xml OTHER than \n MAX_TASKS_PER_NODE (env_pesetup.xml),\n EXEROOT,GMAKE_J,COMPILERS,OS,MPILIBS  (env_build.xml) \n\n";
    foreach my $child (@children) {
	my $name = $child->get_name() ;
	my $text = $child->get_text() ;
	if ($name =~ m/COMPILERS/) {
	    $compilers = $text;
	} 
	if ($name =~ m/MPILIBS/) {
	    $mpilibs = $text;
	} 
	if ($name =~ m/OS/) {
	    $os = $text;
	} 
	$doc = $doc . " $name: $text \n";
    }	
    print_row($var, $doc, $os, $compilers, $mpilibs);
}
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="55%">Machine Name</th>
  <th width="15%">OS</th>
  <th width="15%">Compilers</th>
  <th width="15%">MPI Libs</th>
START_table
}

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

sub print_row {

    my $name = shift;
    my $doc  = shift;
    my $os = shift;
    my $compilers = shift;
    my $mpilibs = 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>$os</td>
    <td>$compilers</td>
    <td>$mpilibs</td>
  </tr>
END_of_row
}

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

sub print_end_table {

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

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

