#!/usr/bin/perl
# Generates a webpage for the parameters
#
# Author: Daniel Graves
# Date:   2004/11/16

use strict;
use DB_File;
use CGI qw(:standard);

###############################################################
# Global Variables
my %descriptions;
my $filename;
my $database_path="/home/e614/public_html/private/TWIST_2004_2007_Analysis/2004_pass1/parameters";
my $param_name;
my $param_value;
my $desc;
my @important=('PBSPARAM','QSUB_PATH','PBS_EMAIL','NO_EMAIL','GEANT_ONLY','MOFIA_ONLY','CHAIN_NUM', 'USERSCRIPT','EXECUTABLE','COPY_DATA','DATA_OUTPATH','DATA_DIR','HBOOK_DIR','LOG_DIR');
my @important_anal_only=('MOFIA_EXE','MOFIA_OUTPATH','ROOT_TREE_DIR','KCM_PATH','LOCAL','GLOBAL_MOFIA_INPATH','LOCAL_MOFIA_INPATH','BAD_RUN_DIR','MIN_EVENTS');
my @important_gen_only=('USERSCRIPT','EXECUTABLE','COPY_DATA','DATA_OUTPATH','DATA_DIR','HBOOK_DIR','LOG_DIR');

###############################################################
# Colors
my $color;
my $bgcolor;
my $bg_norm_color = "\"#FFFFDF\"";
my $norm_color = "\"#000000\"";
my $table_header_color = "\"#221AA0\"";
my $bg_table_header_color = "\"#CCDDEE\"";

###############################################################
# Check the query string and open descriptions database file
$filename = "$database_path/".$ENV{QUERY_STRING}."-EnvFile";
if(!$ENV{QUERY_STRING} || (!-e $filename)) {
  print header,
    start_html('File Not Found!'),
    h2("File not found!"),
    p,
    h4("Could not find $filename"),
    end_html;
  exit(0);
}
else {
  print header,
    start_html($ENV{QUERY_STRING}." Parameters Page"),
    h2($ENV{QUERY_STRING}." Parameters Page");
}
my $name;  # Gen or Set name (eg gen2) - if set, then must be analysis as well
my $anal;  # Gen or Set Analysis (eg anal1)
if($ENV{QUERY_STRING} =~ m/(\D+)(\d+)anal(\d+)/) {
  $name = "$1"."$2";
  $anal = "anal$3";
}
elsif($ENV{QUEUR_STRING} = m/gen(\d+)/) {
  $name = "gen$1";
  # $anal is not set
}
my $mode = 0777;
dbmopen(%descriptions,"$database_path/param_desc.db", $mode) or die h2("Could not open the description database.  $!");

###############################################################
# Print html table
print "<TABLE border=\"2\" cellpading=\"2pt\"><TR ALIGN=\"center\" BGCOLOR=$bg_table_header_color>";
print "<TH><FONT COLOR=$table_header_color>Parameter</FONT>";
print "<TH><FONT COLOR=$table_header_color>Value</FONT>";
print "<TH><FONT COLOR=$table_header_color>Description</FONT>";

$color = $norm_color;
$bgcolor = $bg_norm_color;
open(PARAM_FILE,"$filename");
while(<PARAM_FILE>) {
  ($param_name,$param_value) = get_item($_);  # Get the parameter from the line
  if(!$param_name || !$param_value) {
    next;
  }
  if(!isImportant($param_name)) {
    next;
  }
  
  $desc = $descriptions{"$param_name"};
  print "<TR ALIGN=\"center\" BGCOLOR=$bgcolor>";
  print "<TH ALIGN=\"left\"><FONT COLOR=$color>".$param_name."</FONT>";
  print "<TH ALIGN=\"left\"><FONT COLOR=$color>".$param_value."</FONT>";
  print "<TH ALIGN=\"left\"><FONT COLOR=$color>".$desc."</FONT>";
}
close(PARAM_FILE);
print "</TABLE>";

###############################################################
# Close the database and html web page
print end_html;
dbmclose(%descriptions);
exit(0);

sub get_item($) {
  my $line = shift;

  if($line =~ m/^#/) {
    return ('','');
  }
  elsif($line =~ m/\s*(\S+)\s+(.+)/) {
    return ($1,$2);
  }
  else {  
    return ('','');
  }
}

sub isImportant($) {
  my $n = shift;
  foreach my $v (@important) {
    if($n =~ m/$v/) {
      return 1;  # 1=TRUE (ie display this parameter)
    }
  }
  if($anal) {
    foreach my $v (@important_anal_only) {
      if($n =~ m/$v/) {
	return 1;  # 1=TRUE (ie display this parameter)
      }
    }
  }
  elsif($name =~ m/^gen/) {
    foreach my $v (@important_gen_only) {
      if($n =~ m/$v/) {
	return 1;  # 1=TRUE (ie display this parameter)
      }
    }    
  }
  return 0;  # 0=FALSE (ie don't display this paramter)
}
