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

#-------------- archive data --------------------------------
#----- exported variables:
#-----    $new_week = is this the start of a new week
#-----       used by week and update
#-----    $new_month = is this the start of a new month
#-----       used by month
#-----    $stime =  the date of the last stats file
#-----       used by stats, month, and week
#-----    $statsfile = the route statistics file
#-----       used by stats and html
#-----    $dtime =  the date of the last route table output
#-----       used by stats, month, week, hour, update, and util
#-----    $rtemontable = the route table output
#-----       used by update and rtemon
#-----    $archivedir = the archive directory
#-----       used by preamble
#----- internal variables:
#-----    $opt_a = the archive directory command line option
#-----    $opt_W = weekly archive command line option
#-----    $opt_M = monthly archive command line option
#-----    $opt_T = hourly archive command line option
#-----    $weekarchive = the weekly archive name
#-----    $montharchive = the monthly archive name
#-----    tablearchive = the table archive name
#
#----- fatal errors:
#-----    none
#----- warnings:
#-----    copy = failed to copy data for archive



sub init_archive {
    # determine the archive directory and archive rules
    if (defined($opt_a)) {
	$archivedir = $opt_a;
    }
    if (defined($opt_W)) {
	$weekarchive = "$archivedir/weekly_stats";
    } else {
	$weekarchive = "none";
    }
    if (defined($opt_M)) {
	$montharchive = "$archivedir/monthly_stats";
    } else {
	$montharchive = "none";
    }
    if (defined($opt_T)) {
	$tablearchive = "$archivedir/route_table";
    } else {
	$tablearchive = "none";
    }
}

sub archive_data {
    my($ret);
    my($sc,$mn,$hr,$dy,$mo,$yr,$wd,$yd,$isd) = gmtime($stime);
    $mo = $mo + 1;
    # okay, let's get ready for year 2000...
    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 ($new_month == 1) {
	if ($montharchive ne "none") {
	    if ($mo < 10) {$mo = "0$mo";}
	    $ret = system("cp $statsfile $montharchive.$yr.$mo");
	    if ($ret != 0) {
		&log_error(copy,$montharchive.$yr.$mo);
	    }
	}
    }
    if ($new_week == 1) {
	if ($weekarchive ne "none") {
	    # rough estimate of the week number
	    my($myweek) = int($yd/7) + 1;
	    if ($myweek < 10) {$myweek = "0$myweek";}
	    $ret = system("cp $statsfile $weekarchive.$yr.$myweek");
	    if ($ret != 0) {
		&log_error(copy,$weekarchive.$yr.$myweek);
	    }
	}
    }
    if ($tablearchive ne "none") {
	($sc,$mn,$hr,$dy,$mo,$yr,$wd,$yd,$isd) = gmtime($dtime);
	if ($hr < 10) { $hr = "0$hr"; }
	$yd = $yd + 1;
	if ($yd < 10) { 
	    $yd = "00$yd"; 
	} elsif ($yd < 100) {
	    $yd = "0$yd";
	}
	# okay, let's get ready for year 2000...
	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";
	}
	$ret = system("cp $rtemontable $tablearchive.$yr.$yd.$hr"); 
	if ($ret != 0) {
	    &log_error(copy,$tablearchive.$yr.$yd.$hr);
	}
    }
    return(1);
}

