#
# 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.
#
#----- main program --------------------
#----- exported variables:
#-----    $errorfile = the name of the htmlmaker errorfile
#-----       used by error
#-----    $rpt_interval = the report interval
#-----       used by update
#-----    $web_copy = the file used to copy html output to the server
#-----       used by html
#-----    $hmakerdir = the directory for htmlmaker misc output
#-----       used by preamble
#----- internal variables:
#-----    $opt_h = the command line option for $hmakerdir
#-----    $pidfile = the process id file file for htmlmaker
#-----    $need_update = indicates if a stats update file should be read
#
#----- fatal errors:
#-----    pid = unable to write pidfile
#----- warnings:
#-----    start = indicate htmlmaker has started
#-----    webcopy = remote copy to web server failed
#-----    updatesignal = indicates the update signal was received


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


# check the command line args are correct
if (!getopts('xr:x:e:l:L:h:w:a:TWME:R:s:')  || (@ARGV > 0)) {
    print "usage: htmlmaker [-xTWM] [-e rtewatch] [-E rtemon] [-r rtr]\n";
    print "                 [-d rtemondir] [-h hmakerdir] [-w webdir]\n";
    print "                 [-a archivedir] [-R remote_cp_script] [-s href]\n";
    print "                 [-l aslookupdir] [-L aslookup]\n";
    exit(-1);
}
# determine the html maker output directory
if (defined($opt_h)) {
    $hmakerdir = $opt_h;
}

# write my pid file		
$pidfile = "$hmakerdir/html_maker.pid";
open(PIDFILE, ">$pidfile") || &log_quit(pid,$pidfile);
print PIDFILE "$$\n";
close(PIDFILE);   
# indicate start in the error log
$errorfile = "$hmakerdir/html_maker.err";
log_error(start,none);
# initialize components
&declare_fields;
&init_routemonitor;
&init_update;
&init_archive;
&init_as;
&init_html;
# read existing stats file
&read_current_stats;
# set up a signal handler so the user can force us to re-read the stats
# file.  this allows the users to make manual changes to the stats file
# and then have htmlmaker accept those changes
$SIG{USR1} = 'trigger_stats_update';
$need_update = "no";
#schedule first alarm
#alarms should always occur exactly at start of hour
$SIG{ALRM} = 'get_table';
$curtime = time;
# truncate off the extra minutes and seconds, add one hour
$exacthour = int($curtime/3600) * 3600 + 3600;
# schedule the next alarm for the next exact hour from now
$nextalarm = $exacthour - $curtime;
alarm ($nextalarm);
while(1) {
    # wait for an alarm
    sleep;
}

sub get_table {
    # signal routemonitor for a new route table
    $curtime = time;
    # truncate off the extra minutes and seconds, add one hour
    $exacthour = int($curtime/3600) * 3600 + 3600;
    # schedule the next alarm for the next exact hour from now
    $nextalarm = $exacthour - $curtime;
    alarm ($nextalarm);
    # if we got a signal, we should re-read the stats file and
    # and incorporate any changes someone made to the stats file
    if ($need_update eq "yes") {
	&read_stats_update;
	$need_update = "no";
    }
    # signal the routemonitor we want a new table written
    if (&signal_routemonitor == -1) {
	return(-1);
    }
    # read the new table, update stats and generate new html files
    if (&update_stats ==  -1) {
	return(-1);
    }
    # archive the old statistics if requested by command line opts
    &archive_data;
    # write the updates stats to file for use by cgi search scripts
    &write_current_stats;
    # if the web server is not local, run the locally defined $web_copy 
    # script to copy the html and stats file to the web server 
    # to the server
    if ($web_copy ne "no") {
	my($ret) = system("$web_copy > /dev/null");
	if ($ret !=0) {
	    &log_error(webcopy,$web_copy);
	}
    }
    return(1);
}

sub trigger_stats_update {
# set a flag indicating we should re-read the stats file at start
# of next hour
   $need_update = "yes";
   &log_error(updatesignal, none);
} 
