$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-solvers-in_1.html`;
$TEXT =~ s/<BASE_HREF>/$BASE_HREF\//g;
$TEXT =~ s/<SERVER_NAME>/$SERVER_NAME/g;
$TEXT =~ s/<BACKGROUND_COLOR>/$BACKGROUND/g;
print $TEXT;

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

chomp(@PROBLEMS = `cat $ENV{SERVER_VAR}/lib/solver_tree`);
chomp(@ADPACKAGES = (`cat $ENV{SERVER_LIB}/admin_list`));

foreach $asolver (@ADPACKAGES) {
  $asolver =~ /(.*)=(.*)\s*$/;
  $token = $2;
  $TITLE{"$token"} = $1;
  $token =~ /(.*):(.+)/;
  $type = $1;
  $similar = $accepts{"$type"};
  $similar = "$similar<BREAK>$token";
  $accepts{"$type"} = $similar;
}

foreach $type (@PROBLEMS) {
  $type =~ /(.*)=(.*)\s*$/;
  $token = "\U$2";
  $TITLE{"$token"} = $1;
}

#
#
# Hash the solvers' features.
#

foreach $solver (@PACKAGES) {
  if($solver =~ /=KESTREL_/ or $solver =~ /^\s*$/) {
    next;
  }
  $solver =~ /(.*)=(.*)\s*$/;
  $token = $2;
  $TITLE{"$token"} = $1;
  if(open(IN,"$ENV{SERVER_VAR}/lib/solvers/$token/ACCEPTS")) {
    $takes = <IN>;
    close IN;
    if($takes =~ /^\s*$/) {  #i.e. not yet classified
      $similar = $accepts{"OTHER"};
      $similar = "$similar<BREAK>$token";
      $accepts{"OTHER"} = $similar;
    }
    while($takes =~ /^((\S*?)<ACCEPTS>)/) {
      $crop = $1;
      $input = "\U$2";
      $similar = $accepts{"$input"};
      $similar = "$similar<BREAK>$token";
      $accepts{"$input"} = $similar;
      $takes =~ s#^$crop##;
    }
  } else {
    $similar = $accepts{"OTHER"}; #i.e. not yet classified
    $similar = "$similar<BREAK>$token";
    $accepts{"OTHER"} = $similar;
  }			      
}

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

foreach $type (@TREE) {		
  $type =~ s/\s$//;		 
  if($type =~ /^\s*$/) {
    next;
  }
  $type =~ /(.+)=(.+)/;
  $type = "\U$2";
  @subtypes = split(/\:/, $type);
  $length = @subtypes;
  for($i=0;$i<$length;$i++) {
    print "<UL>\n";
  }
  print "<LI><b>$1</b>\n";
  for($i=0;$i<$length;$i++) {
    print "</UL>\n";
  }
  for($i=0;$i<$length;$i++) {
    print "<UL>\n";
  }
  #
  # starts solver categories at the same depth as the feature
  #
  $solvers = $accepts{"$type"};
  @solvers = split '<BREAK>', $solvers;
  @solvers = sort bycategory (@solvers);
  $PREVTYPE = "";
  $lenLastToken = 0;
  foreach $TOKEN (@solvers) {
    if($TOKEN =~ /^\s*$/) { next; }
    $TOKEN =~ s/\s$//;
    $TITLE = $TITLE{"$TOKEN"};
    $loopsThrough = 0;
    $TYPE = "";
    #
    # traverse the categories left to right
    #
    while($TOKEN =~ /($TYPE.+?):(.+)/) {
      $TYPE = $1;
      if(!($PREVTYPE =~ /^$TYPE/)) {
        if($lenLastToken > 0) {
          for($i=0; $i < $lenLastToken;$i++){
            print "</UL>\n";
          }
          $lenLastToken = 0;
          print "<br>\n";
        }
        $DESCR = $TITLE{"$TYPE"};
#        if($PREVTYPE =~ /\S/) {
#          print "<br>\n";
#        }
        if(defined($DESCR)) {
          print"<UL>\n";
          print "<LI><b>$DESCR</b><br>\n";
        }
        $PREVTYPE = $TYPE;
      } else {
        $lenLastToken--;
      }
      $loopsThrough++;
      if($loopsThrough > 15) {
        last;
      }
    } #end while
    $PREVTYPE = $TYPE;
    if($type eq "OTHER") {
      if(open(IN, "$ENV{SERVER_VAR}/lib/solvers/$TOKEN/OTHER_FEATURES")) {
        chomp($accepts = <IN>);
        close IN;
        print "     <b>$TITLE \[</b><A HREF=\"$BASE_HREF/solvers/$TOKEN\"><b>$accepts</b></A><b>\]</b><br>\n";       
      } else {
        print "     <A HREF=\"$BASE_HREF/solvers/$TOKEN\"><b>$TITLE</b></A><br>\n";
      }
    } else {
        print "     <A HREF=\"$BASE_HREF/solvers/$TOKEN\"><b>$TITLE</b></A><br>\n";
    }
    $lenLastToken = split ':', $TOKEN;
    $lenLastToken--;
  }
  for($i=0;$i<=$length+$lenLastToken;$i++) {
    print "</UL>\n";
  }
}				 

print "<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;


sub bycategory {

  @aline = split ':', $a;
  @bline = split ':', $b;
  if(@bline == @aline) {
    return ($a cmp $b);
  }
  $smaller = @aline;
  if(@bline < @aline) {
    $smaller = @bline;
  }
  for($i = 0; $i < $smaller-1; $i++) {
    if(($aline[$i] cmp $bline[$i])!=0) {
      return ($aline[$i] cmp $bline[$i]);
    }
  }
  if(@aline < @bline) {
    return -1;
  } else {
    return 1;
  }
  print STDERR "Shouldn't have reached end of bycategory sorting routine!\n";
}

