#
# 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.
#
#--------- statistics file operations-------------
#----- exported variables:
#-----    $fieldtotal = the total number of fields per stats file line
#-----       used by util
#-----    $totalw = hours reported this week
#-----       used by week
#-----    $totalm = hours reported this month
#-----       used by month
#-----    $statsfile = the route statistics file
#-----       used by archive and html
#-----    $updatefile = the route statistics update file
#-----       used by html
#-----    $stime = the date of the last stats file
#-----       used by month, week, and archive
#-----    $dtime = the date of the last route table output
#-----       used by update, month, week, hour, archive, and util
#-----    %stats = the array of route statistics, indexed by source
#-----       used by as, update, and util
#-----    %active = the array indicating if a source appeared this hour
#-----       used by hour, week, month, as, update, and util
#----- internal variables:
#-----    none
#
#----- fatal errors:
#-----    statsopen = unable to open existing stats file
#-----    statswrite = unable to write new stats file
#----- warnings:
#-----    nofile = no route stats file, stats initialized to null values
#-----    startline = invalid start of stats file, file backed up, data to null
#-----    statsline = invalid line in stats file, line ignored
#-----    updatebegin = a route update has started
#-----    updateend = a route update has completed
#-----    noupdate = no stats update file exists, stats update ignored
#-----    updateopen = unable to open stats update file, update ignored
#-----    updatestart = invalid start of stats update file, update ignored
#-----    updateline = invalid line in update file, line ignored

sub read_current_stats {
    # read in the current stats database
    my($src, $a, $line);
    # check if a datafile exists
    if (!(-f $statsfile)) {
	&log_error(nofile,none);
	# initialize the number of dumps for this week and month
	$totalw = 0; $totalm = 0; $stime = time;
	return(-1);
    }
    open(STATSFILE, "$statsfile") || &log_quit(statsopen,$statsfile);
    # verify the correct first line of the file
    $line = <STATSFILE>;
    my($Route,$Data,$From, $Time);
    ($Route,$Data,$From, $stime, $Time, $totalw, $totalm) = split(" ",$line);
    if (($Route ne "Route") || (split(" ",$line) != 7)) {
	&log_error(startline,$line);
	system("mv $statsfile $statsfile.bak");
	# initialize the number of dumps for this week and month
	$totalw = 0; $totalm = 0; $stime = time;
	return(-1);
    }
    # read in the stats for each known source and store it in an array
    while($line = <STATSFILE>) {
       chop($line);
       # check that the line looks valid
       if (split(" ",$line) != $fieldtotal) {
	   &log_error(statsline,$line); 
       } else {
	   @array = split(" ",$line);
	   $a = &field_num("addr");
    	   if ($a != -1) {
	       # store the stats line for this address
	       $stats{$array[$a]} = $line;
	       $active{$array[$a]} = "no";
	   }
       }
   }
   close(STATSFILE);
}

sub write_current_stats {
    open(STATSFILE, "> $statsfile") || &log_quit(statswrite,$statsfile);
    $stime = $dtime;
    my($sc,$mn,$hr,$dy,$mo,$yr,$wd,$yd,$isd) = gmtime($stime);
    $mo = $mo + 1;
    if ($yr < 100) { 
        $yr = "19$yr"; 
    } else {
    # this assumes gmtime really returns 108 for 2008 like it
    # should, but if different times implement different gmtime 
    # the we have trouble
        $yr = $yr -100;
        $yr = "20$yr";
    }
    if ($hr < 10) { $hr = "0$hr"; }
    if ($mn < 10) { $mn = "0$mn"; }
    if ($sc < 10) { $sc = "0$sc"; }
    my($Time) = "$mo/$dy/$yr,$hr:$mn:$sc-GMT";
    print STATSFILE "Route Data From $stime ($Time) $totalw $totalm\n";
    my($src);
    foreach $src (keys %stats) {
	print STATSFILE "$stats{$src}\n";
    }
    close(STATSFILE);
}

sub read_stats_update {
    &log_error(updatebegin,none);
    # read in a stats update database
    my($src, $a, $line);
    # check if a datafile exists
    if (!(-f $updatefile)) {
	&log_error(noupdate,$updatefile);
	return(-1);
    }
    open(UPDATEFILE, "$updatefile") || &log_error(updateopen,$updatefile);
    # verify the correct first line of the file
    $line = <UPDATEFILE>;
    my($Route,$Stats,$Update);
    ($Route,$Stats,$Update) = split(" ",$line);
    if (($Route ne "Route") || (split(" ",$line) != 3)) {
	&log_error(updatestart,$line);
	return(-1);
    }
    # read in the stats for each known source and store it in an array
    while($line = <UPDATEFILE>) {
       chop($line);
       # check that the line looks valid
       if (split(" ",$line) != $fieldtotal) {
	   &log_error(updateline,$line); 
       } else {
	   # get the new stats line for a source from update file
	   @uparray = split(" ",$line);
	   $a = &field_num("addr");
    	   if ($a != -1) {
	       #get the old stats line for a source from memory
	       $src = $uparray[$a];
	       @array = split(" ",$stats{$src});
	       for($i=0; $i<$fieldtotal; $i++) {
		   # unless new value is *, replace old value with new value
		   if ($uparray[$i] ne "*") {
		       $array[$i] = $uparray[$i];
		   }
	       }
	       # save the updated source
	       &save_src;
	       # mark the updated source as inactive (in case its new)
	       $active{$array[$a]} = "no";
	   }
       }
   }
    close(UPDATEFILE);
    &log_error(updateend, none);
}


