$SERVER_HOME = $ENV{SERVER_HOME};
$BASE_HREF   = $ENV{SERVER_WWW_URL};
$BASE_DIR    = $ENV{SERVER_WWW_HOME};
$ENV{BACKGROUND}="BGCOLOR=\"$ENV{SERVER_WWW_BACKGROUND}\""; 
$BACKGROUND  = $ENV{BACKGROUND};
$SERVER_NAME = $ENV{SERVER_NAME};


#
# Read in solver list and generate list
#

$TEXT = `cat  $ENV{SERVER_LIB}/html/server-solver-types_1.html`;
$TEXT =~ s/<BASE_HREF>/$BASE_HREF\//g;
$TEXT =~ s/<SERVER_NAME>/$SERVER_NAME/g;
$TEXT =~ s/<BACKGROUND_COLOR>/$BACKGROUND/g;
if($ENV{SERVER_SOLVER_GROUPS}) {
  $TEXT =~ s/.*<SERVER_SOLVER_GROUPS>.*//g;
}
print $TEXT;

open(OUT, ">$BASE_DIR/server-solvers.html") or die "Could not write to $BASE_DIR/server-solvers.html";
$TEXT = `cat  $ENV{SERVER_LIB}/html/server-solvers_1.html`;
$TEXT =~ s/<BASE_HREF>/$BASE_HREF\//g;
$TEXT =~ s/<SERVER_NAME>/$SERVER_NAME/g;
$TEXT =~ s/<BACKGROUND_COLOR>/$BACKGROUND/g;
if($ENV{SERVER_SOLVER_GROUPS}) {
  $TEXT =~ s/.*<SERVER_SOLVER_GROUPS>.*//g;
}
print OUT $TEXT;

chomp(@TREE = `cat $ENV{SERVER_VAR}/lib/solver_tree`); 
chomp(@TREE = (@TREE, `cat $ENV{SERVER_LIB}/admin_tree`));
chomp(@PACKAGES = `cat $ENV{SERVER_VAR}/lib/solver_list`);
chomp(@PACKAGES = (@PACKAGES, `cat $ENV{SERVER_LIB}/admin_list`));

#
# Make an associative array using the software's token
# as the key containing the software's "TYPE"
# 

if($ENV{SERVER_SOLVER_GROUPS}) {
  if(open(IN, "$ENV{SERVER_VAR}/lib/feature_tree")) {
    while(<IN>) {
      if(/^(.+)=(\S+)/) {
        $featureName{$2} = $1;
      }
    }
    close IN;
  }

  @PACKS = ();
  foreach $package (@PACKAGES) {
    $package =~ s/\s$//;
    $package =~ /(.+)=(.+)/;
    $TOKEN  = $2;
    $TOKEN =~ /(.+):(.+)/;
    $TYPE  = "\U$1";
    
    if(open(IN, "$ENV{SERVER_VAR}/lib/solvers/$TOKEN/GROUP")) {
      chomp($group = <IN>);
      close IN;
      $group =~ s/\s//g;
      if($group =~ /\S/) {
        $members = "";
        $members = $grpmem{"$TYPE:GROUP-$group"} if(defined($grpmem{"$TYPE:GROUP-$group"}));
        if($members =~ /\S+/) {
          $package = "";
        } else {
          $package = " =$TYPE:GROUP-$group";
        }
        $members = "$members<MEM>$TOKEN";
        $grpmem{"$TYPE:GROUP-$group"} = $members;
      }
    }
    if($package =~ /\S+/) {
      @PACKS = (@PACKS, $package);
    }
  }
  @PACKAGES = @PACKS;
}

print "<UL>\n";
foreach $type (@TREE) {		
  $type =~ s/\s+$//;		 
  if($type =~ /=kestrel_/i) {
    next;
  }
  $type =~ /(.+)=(.+)/;
  $type = "\U$2";
  @subtypes = split(/\:/, $type);
  $length = @subtypes;
  for($i=1;$i<$length;$i++) {
    print "<UL>\n";
    print OUT "<UL>\n";
  }
  print OUT "<UL>\n";
  print "<LI><b><a href=\"$BASE_HREF/server-solvers.html#$type\">$1</a></b>\n";
  print OUT "<a name=\"$type\"><LI><b>$1</b>\n";
  if(-s "$ENV{SERVER_VAR}/lib/types/$type.html") {
    if(open(IN, "$ENV{SERVER_VAR}/lib/types/$type.html")) {
      print "<br>\n";
      print OUT "<br>\n";
      while(<IN>) {
        print;
        print OUT;
      }
      close IN;
    }
  }
  for($i=1;$i<$length;$i++) {
    print "</UL>\n";
    print OUT "</UL>\n";
  }
  print OUT "</UL>\n";

  $has_entries = 0;
  foreach $package (@PACKAGES) {
    $package =~ s/\s+$//;
    $package =~ /(.+)=(.+)/;
    $TITLE = $1;
    $TYPE  = $2;
    $TOKEN = $2;
    $TYPE =~ /(.*):(.+)/;
    $TYPE = "\U$1";
    $GROUP = $2;
    if ($TYPE eq $type) {
      if($has_entries==0) {
        for($i=0;$i<$length;$i++) {
          print OUT "<UL class=blank>\n";
        }
        $has_entries = 1;
      }
      if($TITLE =~ /\S/) {
        print OUT "<LI><A HREF=\"$BASE_HREF/solvers/$TOKEN\"><b>$TITLE</b></A><br>\n";
      } else {
        if(defined($grpmem{"$TYPE:$GROUP"})) {
          $GROUP =~ s/GROUP-//;
          print OUT "<LI><b>$GROUP</b>";
          $grpmem = $grpmem{"$TYPE:GROUP-$GROUP"};
          @grpmem = split '<MEM>', $grpmem;
          @grpmem = sort(@grpmem);
          foreach $mem (@grpmem) {
            if(open(IN, "$ENV{SERVER_VAR}/lib/solvers/$mem/ACCEPTS")) {
              $accepts = <IN>;
              close IN;
              $accepts =~ s/OTHER<ACCEPTS>//;
              @accepts = split '<ACCEPTS>', $accepts;
              foreach $feature (@accepts) {
                if(defined($featureName{$feature})) {
                  $feature = $featureName{$feature};
                }
                print OUT "  <b>[<A HREF=\"$BASE_HREF/solvers/$mem\">$feature</A>]</b>";
              }
            }
            if(open(IN, "$ENV{SERVER_VAR}/lib/solvers/$mem/OTHER_FEATURES")) {
              chomp($accepts = <IN>);
              close IN;
              print OUT "  <b>[<A HREF=\"$BASE_HREF/solvers/$mem\">$accepts</A>]</b>";
            }
          }
          print OUT "<br>\n";
        }
      }
    }
  }
  if($has_entries==1) {
    for($i=0;$i<$length;$i++) {
      print OUT "</UL>\n";
    }
  }
}	
$TEXT = `cat $ENV{SERVER_LIB}/html/server-solvers_2.html`;
$TEXT =~ s/<SERVER_NAME>/$SERVER_NAME/g;
$TEXT =~ s/<BASE_HREF>/$BASE_HREF\//g;
print $TEXT;
print OUT $TEXT;
close TEXT;
