#!FOOPERL


#
# Copyright (c) Xerox Corporation 1997, 1998. All rights reserved.
#
# License is granted to copy, to use, and to make and to use derivative
# works, provided that Xerox is acknowledged in all documentation
# pertaining to any such copy or derivative work. Xerox grants no other
# licenses expressed or implied. The Xerox trade name should not be used
# in any advertising without its written permission.
#
# XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
# MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE
# FOR ANY PARTICULAR PURPOSE.  The software is provided "as is" without
# express or implied warranty of any kind.
#
# These notices must be retained in any copies of any part of this software.
#


require ("Fields.pl");
require ("Html.pl");

# distribution table entries are hyperlinks to searches
# set this to the location of the search script
$hreflocate = "FOOCGISEARCH";
# set this to the directory where the data is stored
$webdir = "FOOWEBDIR";
# set this to the name of the file where the data is stored
$datafile = "$webdir/route_stats";

#----- main program ------
&declare_fields;
&init_fields;
&html_start("Multicast Routing Data Distribution");
&parse_input;
if ($text eq "yes") { 
# only the hyperlinks use the text setting, dist wants text equal no
    $dtext = "yes";
    $text = "no";
}
&file_open;
&count_dist;
&dist_header;
&start_table;
&table_header_dist;
&table_data;
&end_table;
&dist_match_header;
&start_table;
&table_header_time(2);
&table_header_fields(2);
&table_header_range(2);
&end_table;
&html_definitions;
&html_end;


#----- subroutines that should be independent of datafile changes------


# count the number of sources in each distribution interval
sub count_dist {
   # the search should not dismiss any source based on the distribution field
   $care[$distnum] = 0;  
   # figure out how many intervals
   $numintervals = int($distmax/$interval);
   # adjust distmax to match intervals if needed
   $distmax = $numintervals * $interval;
   # initialize the counts for each interval
   $distover = 0;
   $distnone = 0;
   for($i=0; $i < $numintervals; $i++) {
      $distcount[$i] = 0;
   }
   while($line = <DATAFILE>) {
      # check to see if we include this source in the output
      @array = split(" ",$line);
      $valid = &check_match;
      # if matched search parameters print a table line
      if ($valid == 1) {
	 if ($array[$distnum] > $distmax) {
	    $distover = $distover + 1;
         } elsif ($array[$distnum] == 0) {
	    $distnone = $distnone + 1;
         } else {
	    for($i = 0; $i < $numintervals; $i++) {
	       if (($array[$distnum] > $i*$interval) && 
	        ($array[$distnum] <= ($i+1)*$interval)) {
		  $distcount[$i] = $distcount[$i] + 1;
               }
            }
         }
      }
   }
}

# fill in the distribution data
sub table_data {
   print "<tr>";
   # print the entry for the none column
   print "<td align=right>";
   &start_href;
   print "use=$data[$distnum]&max$data[$distnum]=0";
   print '">';
   print "$distnone </a> </td> \n";
   # print entries for each column
   for($i=0; $i < $numintervals; $i++) {
      $low = $i*$interval + 1;
      $high = ($i+1)*$interval;
      print "<td align=right>";
      &start_href;
      print "use=$data[$distnum]&";
      print "max$data[$distnum]=$high&min$data[$distnum]=$low";
      print '">';
      print "$distcount[$i] </a> </td>\n";
   }
   # print the entry for the over max column
   print "<td align=right>";
   &start_href;
   print "use=$data[$distnum]&min$data[$distnum]=$distmax";
   print '">';
   print "$distover </a> </td>\n";
   print "</tr>\n";
}

sub start_href {
   print '<a href="';
   print "$hreflocate?";
   print "sortnum=$data[$distnum]&";
   for($j = 0; $j < $special; $j++) {
      if ($j != $distnum) {
         if ($use[$j] == 1) {
	    print "use=$data[$j]&";
         }
         if ($care[$j] == 0) {
	    print "care=$data[$j]&";
         } else {
	    $mychange = field_num("change");
	    $myage = field_num("age");
	    if (($mychange < 0) || ($myage < 0)) {
	       print "<p> error, could not find field change or age <br>";
            } elsif (($j == $mychange) || ($j == $myage)) {
	       print "min$data[$j]=$min[$j]&";
	       print "max$data[$j]=$max[$j]&";
	   } else {
	       print "$data[$j]=$max[$j]&";
            }
         }
      }
   }
   for($j = $special; $j < $total; $j++) {
      if ($j != $distnum) {
         if ($use[$j] == 1) {
	    print "use=$data[$j]&";
         }
         if ($care[$j] == 0) {
	    print "care=$data[$j]&";
         } else {
	    print "max$data[$j]=$max[$j]&";
	    print "min$data[$j]=$min[$j]&";
         }
      }
   }
   if ($dtext eq "yes") {
       print "text=yes&";
   }
}
