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

#----- default directory names and executable paths ----------
# users can override these values using the command line options listed
# the output directory for the htmlmaker program, command line opt -h
$hmakerdir =  "FOOHMKRDIR";
# the directory to put new web pages, command line opt -w
$webdir = "FOOWEBDIR";
# no modifications should occur below this point

#-----------------------------------------------
# command line options
# option: -h dir
#   action: look for the htmlmaker pid file in directory dir
#   default: use $hmakerdir defined above as the directory for the pid file
# option: -w dir
#   action: look for the route_stats in this directory and write output here
#   default: use $webdir defined above as the route_stats and output dir
# option: -p pid
#   action: use pid as the process id for htmlmaker
#   default: read the htmlmaker pid file to obtain the pid

#------ perl standard modules and libraries -----------
use Getopt::Std;


#------ main program ------------------------------------
# check the command line args are correct
if (!getopts('p:h:w:')  || (@ARGV > 0)) {
    print "usage: asretry [-h htmlmakerdir] [-w webdir] [-p pid]\n";
}

if (defined($opt_p)) {
    # user supplied pid on command line
    $pid = $opt_p;
} else {
    if (defined($opt_h)) {
	$hmakerdir = $opt_d;
    }
    # the htmlmaker process id file
    $pidfile = "$hmakerdir/html_maker.pid";
    # get the htmlmaker process id
    open(PIDFILE, "$pidfile") || 
	die "Unable to open the htmlmaker's pid file: $pidfile\n";
    $pid = <PIDFILE>;
    chop($pid);
    close(PIDFILE);
}

if (defined($opt_w)) {
    $webdir = $opt_w;
}
$updatefile = "$webdir/route_stats.update";
$statsfile = "$webdir/route_stats";

# create an update file to be read by htmlmaker
open(UPDATEFILE, ">$updatefile") || die "Unable to create file $updatefile\n";
print UPDATEFILE "Route Stats Update\n";

#read the stats database and if anyone has AS -1, change it to -2 (= lookup)
open(STATSFILE, "$statsfile") || die "Unable to open database $statsfile\n";
$line = <STATSFILE>;
($Route,$Data,$From, $stime, $Time, $totalw, $totalm) = split(" ",$line);
if (($Route ne "Route") || (split(" ",$line) != 7)) {
    print "invalid first line in $statsfile\n";
    exit(-1);
}
while($line = <STATSFILE>) {
    chop($line);
    # check that the line looks valid
    @array = split(" ",$line);
    if ($array[1] == -1) {
	print UPDATEFILE "$array[0] -2";
	# indicate don't change other values
	for($i = 2; $i < @array; $i++) {
	    print UPDATEFILE " *";
	}
	print UPDATEFILE "\n";
    }
}
close(UPDATEFILE);
close(STATSFILE);

# send signal USR1 to make htmlmaker process the update file 
if (kill( USR1,$pid) == 1) {
    print "htmlmaker will process changes at next hourly update\n";
} else {
    print "ERROR: there was no htmlmaker process with PID=$pid to signal\n";
}
 
