#!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";

print "This program will change the htmlmaker statistics for a source \n";
print "to anything you specify.\n";
print "Htmlmaker will believe whatever you specify here, even if that\n";
print "makes no sense or later crashes htmlmaker...\n";
print "Before proceeding, you should look carefully at the statistics\n";
print "database in: \n";
print "$statsfile\n";
print "to get a sense of what htmlmaker thinks statistics should look like.\n";
print "Unless there is a need to force a change to the statistics for\n";
print "some source, you should not be running this program.\n";
print "\n";
print "Continue with statsedit (yes/no): ";
$ans = <STDIN>;
if (($ans =~ "y") || ($ans =~ "Y")) {
    print "You have been warned!\n";
} else {
    exit(1);
}
print "\n";

# 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";

print "Enter Source CIDR Address (a.b.c.d/mask) (return to quit): ";
$ans = <STDIN>;
while ($ans ne "\n") {
    chop($ans);
    print UPDATEFILE "$ans ";
    print "AS Number (-2 to lookup, return for use current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "State: (up, down, or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Change: (time value or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "First Appeared: (time value or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Metric: (return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";   
    print "Flaps last hour (or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";   
    print "Updates last hour (or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Flaps this week (or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Uptime this week (percentage or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Hours up this week (return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "State Changes this week (return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Flaps this month (or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Uptime this month (percentage or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Hours up this month (return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "State Changes this month (return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Flaps last week (or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Uptime last week (percentage or return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "Hours up last week (return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans ";
    print "State Changes last week (return for current value): ";
    $ans = <STDIN>;
    if ($ans eq "\n") {
	$ans = "*";
    } else {
	chop($ans);
    }
    print UPDATEFILE "$ans\n";
    print "\n";
    print "Enter Source CIDR Address (a.b.c.d/mask) (return to quit): ";
    $ans = <STDIN>;
}
close(UPDATEFILE);

# 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";
}
 
