#!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.
#


# 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";
# the maximum number of matches to display in a table
$maxmatch = 500;

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


#----- main program ------
&declare_fields;
&init_fields;
&html_start("Multicast Routing Report");
&parse_input;
&file_open;
&start_table;
&table_header_time(4);
&table_header_fields(4);
&table_header_range(2);
&search_data;
&end_table;
&too_many;
&html_definitions;
&html_end;


# read in the stats for each known source and see if it matches the search
sub search_data {
   $match = 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) {
         $result[$match] = $line;
         $match = $match + 1;
      }
   }
   @sorted = sort sortfield @result;
   if (($match > $maxmatch) && (text ne "yes")) {
       $stoptable = $maxmatch;
   } else {
       $stoptable = $match;
   }
   for($j=0; $j < $stoptable; $j++) {
      @array = split(" ",@sorted[$j]);
      if ($text eq "yes") {
	  $csize = $tsize[0];
	  printf "%${csize}s", $array[0];
      } else {
	  print "<tr> <td align=left> $array[0]</td>"; #src left aligned
      }
      for($i=1; $i < $special; $i++) {
         if ($use[$i] == 1) {
	    if (($i == 3) || ($i == 4)) {
		# change and age dates need special handling...
		($psc,$pmn,$phr,$pdy,$pmo,$pyr,$pwd,$pyd,$pisd) = 
	          gmtime($array[$i]);
		$pmo = $pmo +1;
		# okay, let's get ready for year 2000...
		if ($pyr < 100) { 
		    $pyr = "19$pyr"; 
		} else {
		# this assumes gmtime really returns 108 for 2008 like it
		# should, but if different times implement different gmtime 
		# the we have trouble
		    $pyr = $pyr -100;
		    $pyr = "20$pyr";
		}
		if ($phr < 10) { $phr = "0$phr"; }
		if ($pmn < 10) { $pmn = "0$pmn"; }
		if ($psc < 10) { $psc = "0$psc"; }
		$pdate = "$pmo/$pdy/$pyr,$phr:$pmn";
		if ($text eq "yes") {
		    $csize = $tsize[$i];
		    printf "%${csize}s",$pdate;
		} else {
		    print "<td align=center> $pdate </td>";
		}
	    } else {
		if ($text eq "yes") {
		    $csize = $tsize[$i];
		    printf "%${csize}s",$array[$i];
		} else {
		    print "<td align=center> $array[$i] </td>";
		}
	    }
         }
      }
      $csize = 10;
      for($i=$special; $i < $total; $i++) {
         if ($use[$i] == 1) {
            $myout = int($array[$i]);
	    if ($text eq "yes") {
		printf "%${csize}s", $myout;
	    } else {
		print "<td align=center> $myout </td>";
	    }
	}
      }
      if ($text eq "yes") {
	  print "\n";
      } else {
	  print "</tr>\n";
      }
  }
}

